private void SocketReceiveMsg()//接收socket消息 { byte[] buffer = new byte[1024 * 1024]; int n; String s; while (true) { if (isTerminating) { break; } if (mySocket == null) { break; } try { n = mySocket.Receive(buffer); s = StringByteHelper.BytesToString(buffer, 0, n); ReceiveMsg(s); deviceManager.receiveMsg(this, s); } catch (Exception ex) { Console.WriteLine("{0} Exception caught.", ex); } } }
public static String createModbusMessage(byte func, byte[] data, byte dev = 0x88)// 新建发送的消息 { byte[] cmd = new byte[6 + data.Length]; cmd[0] = 0x55; cmd[1] = 0xAA; cmd[2] = (byte)(data.Length + 3); cmd[3] = dev; cmd[4] = func; for (int i = 0; i < data.Length; i++) { cmd[i + 5] = data[i]; } byte crc = 0x00; for (int i = 0; i < data.Length + 5; i++) { crc ^= cmd[i]; } cmd[data.Length + 5] = crc; return(StringByteHelper.BytesToString(cmd, 0, cmd.Length)); }