public static int CreateMsg_ModBus(MODBUS_RX_MSG MBRM, ref byte[] Buf) { int i = 0; Buf[i++] = (byte)MBRM.Addr; Buf[i++] = (byte)(MBRM.FuncCode); Buf[i++] = (byte)(MBRM.Offset >> 8); Buf[i++] = (byte)(MBRM.Offset & 0xff); Buf[i++] = (byte)(MBRM.Length >> 8); Buf[i++] = (byte)(MBRM.Length & 0xff); MBRM.CheckValue = Tools.GetCrc16(Buf, i); Buf[i + 1] = (byte)(MBRM.CheckValue >> 8); Buf[i] = (byte)(MBRM.CheckValue & 0xff); return(i + 2); }
private MODBUS_TX_MSG ThreadGetMsg(int Offset, int Count) { MODBUS_RX_MSG MBRM = new MODBUS_RX_MSG(); MODBUS_TX_MSG MBTM = new MODBUS_TX_MSG(); MBTM.Buf = new byte[100]; byte[] RxBuffer = new byte[200]; byte[] TxBuffer = new byte[200]; MBRM.Addr = Value.RSValue.RS485Addr; MBRM.FuncCode = 0x03; MBRM.Length = Count; MBRM.Offset = (byte)Offset; int Length = 标准ModBus.CreateMsg_ModBus(MBRM, ref TxBuffer); Length = Usart.SendData(serialPort1, TxBuffer, Length, ref RxBuffer, 200); 标准ModBus.Export_ModBus(ref MBTM, ref RxBuffer); return(MBTM); }
private MODBUS_TX_MSG GetMsg(int Offset, int Count) { MODBUS_RX_MSG MBRM = new MODBUS_RX_MSG(); MODBUS_TX_MSG MBTM = new MODBUS_TX_MSG(); MBTM.Buf = new byte[100]; byte[] RxBuffer = new byte[200]; byte[] TxBuffer = new byte[200]; MBRM.Addr = byte.Parse(comboBox3.Text); MBRM.FuncCode = 0x03; MBRM.Length = Count; MBRM.Offset = (byte)Offset; int Length = 标准ModBus.CreateMsg_ModBus(MBRM, ref TxBuffer); serialPort1.ReadTimeout = 500; Length = Usart.SendData(serialPort1, TxBuffer, Length, ref RxBuffer, 200); 标准ModBus.Export_ModBus(ref MBTM, ref RxBuffer); return(MBTM); }