public ModBusResponse() { ModBusResHeader = new ModBusResponseHeader(); ModBusResBody = new ModBusResponseBody(); }
/// <summary> /// 作为服务器接收PLC数据 /// </summary> /// <returns></returns> public void ServerReceive() { while (null != this.socketServerWrapper.TempClientSocket) { //接收PLC客户端发送过来的消息并回复 byte[] receiveBuf = new byte[256]; if (this.socketServerWrapper.Read(receiveBuf)) { ModBusRequestHeader tempModBusReqHeader = new ModBusRequestHeader(receiveBuf, 0); ModBusResponseHeader respHeader = new ModBusResponseHeader(tempModBusReqHeader); //将接收到的数据追加到消息队列中 PLCData data = new PLCData(receiveBuf, ModBusRequestHeader.TotalLength); lock (BasicData.PLCQueue) { BasicData.PLCQueue.Enqueue(data); } byte[] resp = new byte[ModBusResponseHeader.TotalLength]; respHeader.ToBytes(resp, 0); //默认回复PLC ModBus protocol头 this.socketServerWrapper.Write(resp); } else //客户端终端连接 { Flag.PLCConnected = false; this.socketServerWrapper.TempClientSocket.Close(); this.socketServerWrapper.TempClientSocket = null; Log.Write("Server: PLC断开了连接."); } } //重新监听 Listen(); }