查看: 844|回复: 1
收起左侧

[问题求助] 有没有大神能帮忙讲讲这段程序

周Z粥 2023-2-16 13:48:27 | 显示全部楼层 |阅读模式
邀请回答

马上注册,享受更多特权

您需要 登录 才可以下载或查看,没有帐号?立即注册   

x
    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;
        }





上一篇:叩响c#之门
下一篇:C#数据结构和算法

已有 0 人打赏作者

回复 邀请回答送花

使用道具 举报

mytears5 2023-2-17 11:29:09 | 显示全部楼层
意思是 通过TCP通信,将PLC里面的两个32位整形数据读出来,写到一个64位整型地址中。
回复 送花

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册   

本版积分规则

有技术问题,就上汇川技术社区

INOVANCE汇川技术 公众号

扫码下载掌上汇川APP

全国服务热线:8:30-17:30

4000-300124

苏州地址:江苏省苏州市吴中区越溪友翔路16号

深圳地址:深圳市龙华新区观澜街道高新技术产业园汇川技术总部大厦

Copyright © 2003-2100 汇川技术 Powered by Discuz! X3.4 ( 苏ICP备12002088号 )
快速回复 返回列表 返回顶部