加減1生不息
自動(dòng)化軟件工程師
級(jí)別: 略有小成
精華主題: 0
發(fā)帖數(shù)量: 137 個(gè)
工控威望: 382 點(diǎn)
下載積分: 510 分
在線時(shí)間: 39(小時(shí))
注冊(cè)時(shí)間: 2018-11-02
最后登錄: 2024-11-08
查看加減1生不息的 主題 / 回貼
樓主  發(fā)表于: 2023-11-05 20:38
public Int64 Read_PLC_dint(string 設(shè)備類型, 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 (設(shè)備類型)
                {
                    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;
        }
自動(dòng)化軟件工程師
雪山飛狐
級(jí)別: 網(wǎng)絡(luò)英雄
精華主題: 0
發(fā)帖數(shù)量: 11987 個(gè)
工控威望: 14395 點(diǎn)
下載積分: 35991 分
在線時(shí)間: 918(小時(shí))
注冊(cè)時(shí)間: 2019-06-05
最后登錄: 2024-11-08
查看雪山飛狐的 主題 / 回貼
1樓  發(fā)表于: 2023-12-07 13:04
學(xué)習(xí)學(xué)習(xí)。