Modbus Tcp
https://gitee.com/szwzhsz/Modbus-TCP-client-server-DotNetty.?_from=gitee_search
固定协议格式
事务标识(2byte):00 00,可变(递增)
协议标识(2byte):00 00,固定
长度(2byte):00 06,可变
单位标识(1byte):01,固定
功能码(1byte):01,可变
数据(nbyte):可变
功能码:01 读取输出线圈
单个读取 从00100开始,读取1个bool
发送:00 00 00 00 00 06 01 01 00 63 00 01 (01:功能码(1byte);00 63:起始地址(2byte);00 01:读取长度(2byte))
返回:00 00 00 00 00 04 01 01 01 01 (01:功能码(1byte);01:字节长度(1byte);01;数据:01)-- 00100: 开
返回:00 00 00 00 00 04 01 01 01 00 -- 00100: 关
批量读取 从00100开始,批量读取10个bool
发送:00 01 00 00 00 06 01 01 00 63 00 0A (十六进制转二进制,再倒序存储)
返回:00 01 00 00 00 05 01 01 02 00 00 (全关) 0000 0000
返回:00 01 00 00 00 05 01 01 02 01 00 (100) 0000 0001
返回:00 01 00 00 00 05 01 01 02 03 00 (100,101开) 0000 0011
namespace test { internal class Program { static void Main(string[] args) { byte[] coil_data = new byte[] { 03, 00 }; var d = ByteToBoolArray(coil_data, 8); foreach (bool item in d) { Console.Write(item + " "); } // 输出:True True False False False False False False Console.ReadKey(); } public static bool[] ByteToBoolArray(byte[] inBytes, int length) { if (inBytes == null) { return null; } if (length > inBytes.Length * 8) { length = inBytes.Length * 8; } bool[] array = new bool[length]; for (int i = 0; i < length; i++) { array[i] = BoolOnByteIndex(inBytes[i / 8], i % 8); } return array; } public static bool BoolOnByteIndex(byte value, int offset) { byte dataByBitIndex = GetDataByBitIndex(offset); return (value & dataByBitIndex) == dataByBitIndex; } private static byte GetDataByBitIndex(int offset) { return offset switch { 0 => 1, 1 => 2, 2 => 4, 3 => 8, 4 => 16, 5 => 32, 6 => 64, 7 => 128, _ => 0, }; } } }
功能码:03,读取保持寄存器
单个读取从40100开始,读取short(值为11)
发送:00 01 00 00 00 06 01 03 00 63 00 01
返回:00 01 00 00 00 05 01 03 02 00 0B
批量读取从40100开始,10个word(40100为11,40101为22)
发送:00 01 00 00 00 06 01 03 00 63 00 0A
返回:00 01 00 00 00 17 01 03 14 00 0B 00 16 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 (2个字节合为1个word)
批量读取从40100开始,200个word(需要分批次发送和接收,再合并字节)
发送 : 00 0C 00 00 00 06 01 03 00 63 00 78 (100开始,读取120个)
[调试] 2023-11-21 08:35:07.386 Thread [001] ModbusTcpNet[127.0.0.1:502] : 接收 : 00 0C 00 00 00 F3 01 03 F0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
[调试] 2023-11-21 08:35:07.386 Thread [001] ModbusTcpNet[127.0.0.1:502] : 发送 : 00 0D 00 00 00 06 01 03 00 DB 00 50 (219开始,读取80个)
[调试] 2023-11-21 08:35:07.386 Thread [001] ModbusTcpNet[127.0.0.1:502] : 接收 : 00 0D 00 00 00 A3 01 03 A0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
字节合并
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Modbus RTU
读线圈寄存器(01H)
发送
返回
读保持寄存器(03H)
发送
返回