加减1生不息
自动化软件工程师
级别: 略有小成
精华主题: 0
发帖数量: 137 个
工控威望: 382 点
下载积分: 510 分
在线时间: 37(小时)
注册时间: 2018-11-02
最后登录: 2024-01-26
查看加减1生不息的 主题 / 回贴
楼主  发表于: 2023-11-05 20:38
public Int64 Read_PLC_dint(string 设备类型, string 通讯方式, string 地址类型, int 地址)
        //读取dint
        {
            Int64 return_int64 = 0;
            #region 判断是否为ModbusTCP通讯方式
            if (通讯方式.IndexOf("TCP") > -1)
            {
                int port = 0;
                int read_address = 0;
                string ip_address = "";
                switch (设备类型)
                {
                    case "汇川H5U系列":
                        port = 502;
                        switch (地址类型)
                        {
                            case "D":
                                read_address = 0;
                                break;
                            case "R":
                                read_address = 12288;
                                break;
                        }
                        break;
                }
                ip_address = 通讯方式.Substring(通讯方式.IndexOf("IP:") + 3, 通讯方式.Length - 通讯方式.IndexOf("IP:", 0) - 3);
                client = new ModbusClient(ip_address, port);

                read_address += 地址;
                try
                {
                    client.Connect();
                    int[] ints = client.ReadHoldingRegisters(read_address, 2);
                    byte[] bytes1 = BitConverter.GetBytes(ints[0]);
                    byte[] bytes2 = BitConverter.GetBytes(ints[1]);
                    byte[] bytes = new byte[8];
                    bytes[0] = bytes1[0];
                    bytes[1] = bytes1[1];


                    bytes[2] = bytes2[0];
                    bytes[3] = bytes2[1];

                    return_int64 = BitConverter.ToInt64(bytes, 0);
                    client.Disconnect();
                }
                catch { }
            }
            #endregion
            return return_int64;
        }
自动化软件工程师
hcm386
级别: 正式会员
精华主题: 0
发帖数量: 39 个
工控威望: 83 点
下载积分: 888 分
在线时间: 6(小时)
注册时间: 2023-11-24
最后登录: 2024-05-06
查看hcm386的 主题 / 回贴
1楼  发表于: 2023-12-09 14:49
谢谢。。