/// <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(); }
/// <summary> /// 更新UI文本框邮件数据处理逻辑 /// </summary> private void UpdateUI() { while(true) { if (needUpdate) { if (BasicData.PLCQueue.Count > 0) { PLCData data = new PLCData(); lock (BasicData.PLCQueue) { data = BasicData.PLCQueue.Dequeue(); } if (this.InvokeRequired) { //更新UI数据 this.Invoke(UpdateUIFunction, new object[] { data.MailId, data.Volume, data.Weight }); //开启计时器 this.Invoke(UpdateTimerFunction); } else { UpdateUIAction(data.MailId, data.Volume, data.Weight); UpdateTimerAction(); } needUpdate = false; //查找对应的图像及条码信息并显示出来 bool findPic = false; while (!findPic && !needUpdate) { //查找条码 if (BasicData.OBRQueue.Count > 0) { //OBRData obrData = new OBRData(); OBRData obrData = BasicData.OBRQueue.Peek(); //匹配Mail ID // 当OBR中含有PLC未传出来的邮件ID while (obrData.MailId < data.MailId && BasicData.OBRQueue.Count > 0) { lock (BasicData.OBRQueue) { BasicData.OBRQueue.Dequeue(); } obrData = BasicData.OBRQueue.Peek(); } // 当ID等于PLC数据ID if (obrData.MailId == data.MailId) { lock (BasicData.OBRQueue) { BasicData.OBRQueue.Dequeue(); } if (this.InvokeRequired) { //更新UI数据 this.Invoke(UpdateBarcodeFunction, new object[] { obrData.BarCode }); } else { UpdateBarcodeAction(obrData.BarCode); } } } //查找图像 string fileName = data.MailId.ToString() + ".jpg"; string filePath = ConfigurationManager.AppSettings["PicDirectory"] + fileName; //d:\MailPic\1.jpg if (System.IO.File.Exists(filePath)) { if (this.InvokeRequired) { this.Invoke(UpdatePicFunction, new object[] { filePath }); } else { UpdatePicAction(filePath); } findPic = true; } } //add for test //Thread.Sleep(10); //wrapper.ClientSend(data.MailId.ToString(), OperationAction.Normal); //needUpdate = true; // } } } }