| | |
| | | using System.Windows.Forms; |
| | | using System.Threading; |
| | | using System.Configuration; |
| | | using EasyModbus; // 引入EasyModbusTCP库 ***** |
| | | using EasyModbus; // 引入EasyModbusTCP库 |
| | | using System.IO.Ports; |
| | | using System.Net.NetworkInformation; |
| | | |
| | |
| | | ConnectServer(ipAddress, port1); |
| | | int[] data = tcpClient.ReadHoldingRegisters(startingAddress, quantity); // 读取寄存器数据 |
| | | string strData = ""; // 存储字符串数据 |
| | | //string str = ""; |
| | | //string str1 = ""; |
| | | //string str2 = ""; |
| | | string str = ""; |
| | | string str1 = ""; |
| | | string str2 = ""; |
| | | |
| | | for (int i = 0; i < quantity; i++) |
| | | { |
| | | //str= data[i].ToString("X"); |
| | | //if (str.Length == 4) |
| | | //{ |
| | | // str1 = str.Substring(2, 2); |
| | | // str2 = str.Substring(0, 2); |
| | | // strData += str1 + str2; |
| | | //} |
| | | //if (str.Length == 2) |
| | | //{ |
| | | // strData += str; |
| | | //} |
| | | strData += data[i].ToString("X"); |
| | | str = data[i].ToString("X"); |
| | | if (str.Length == 4) |
| | | { |
| | | str1 = str.Substring(2, 2); |
| | | str2 = str.Substring(0, 2); |
| | | strData += str1 + str2; |
| | | } |
| | | if (str.Length == 2) |
| | | { |
| | | strData += str; |
| | | } |
| | | } |
| | | string str66 = hexStrToStr(strData); |
| | | return hexStrToStr(strData); ; |
| | | } |
| | | |