一、读线圈状态
>二、读保持寄存器 /// <summary> /// 读保持型寄存器 /// </summary> static void Test_0x03() { http://www.easck.com ushort startAddr = 0; ushort readLen = 10; // 请求 // byte[] 需要指定长度;不支持Linq List<byte> command = new List<byte>(); command.Add(0x01);// 1号从站 command.Add(0x03);// 功能码:读保持型寄存器 // 起始地址 command.Add(BitConverter.GetBytes(startAddr)[1]); command.Add(BitConverter.GetBytes(startAddr)[0]); // 读取数量 command.Add(BitConverter.GetBytes(readLen)[1]); command.Add(BitConverter.GetBytes(readLen)[0]); // CRC command = CRC16(command); // 报文组装完成 // 发送-》SerialPort SerialPort serialPort = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One); // 打开串口 serialPort.Open(); serialPort.Write(command.ToArray(), 0, command.Count); // 进行响应报文的接收和解析 byte[] respBytes = new byte[serialPort.BytesToRead]; serialPort.Read(respBytes, 0, respBytes.Length); // respBytes -> 01 01 02 00 00 B9 FC // 检查一个校验位 List<byte> respList = new List<byte>(respBytes); respList.RemoveRange(0, 3);//截去:从站地址 功能码 字节计数 respList.RemoveRange(respList.Count - 2, 2);//截去:校验位 // 拿到实际的数据部分,进行数据解析 // 明确一点:读的是无符号单精度 //byte[] data = new byte[2]; //for (int i = 0; i < readLen; i++) //{ // // 字节序问题 小端 大端 // data[0] = respList[i * 2 + 1]; // data[1] = respList[i * 2]; // // 根据此两个字节转换成想要的实际数字 // var value = BitConverter.ToUInt16(data, 0); // Console.WriteLine(value); //} // 明确一点:读的是Float byte[] data = new byte[4]; for (int i = 0; i < readLen / 2; i++) { // 字节序问题 小端 大端 data[0] = respList[i * 4 + 3]; data[1] = respList[i * 4 + 2]; data[2] = respList[i * 4 + 1]; data[3] = respList[i * 4]; // 根据此两个字节转换成想要的实际数字 var value = BitConverter.ToSingle(data, 0); Console.WriteLine(value); } }三、写多个保持寄存器
>四、CRC校验 static List<byte> CRC16(List<byte> value, ushort poly = 0xA001, ushort crcInit = 0xFFFF) { if (value == null || !value.Any()) throw new ArgumentException(""); //运算 ushort crc = crcInit; for (int i = 0; i < value.Count; i++) { crc = (ushort)(crc ^ (value[i])); for (int j = 0; j < 8; j++) { crc = (crc & 1) != 0 ? (ushort)((crc >> 1) ^ poly) : (ushort)(crc >> 1); } } byte hi = (byte)((crc & 0xFF00) >> 8); //高位置 byte lo = (byte)(crc & 0x00FF); //低位置 List<byte> buffer = new List<byte>(); //添加校验原始值 buffer.AddRange(value); //添加校验位值 buffer.Add(lo); buffer.Add(hi); //加上原始校验值返回 return buffer; }到此这篇关于C#操作串口通信协议协议Modbus的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持我们。










