private void recvRfidProc() { //byte[] data = new byte[1024]; byte[] byteStream = null; while (_conRfid) { int recv = 0; try { //recv = _sockRfid.ReceiveFrom(data, ref _epRfid); byteStream = _udpClient.Receive(ref C_Point); _bConnected = true; } catch (System.Exception e) { Console.WriteLine(" recvRfidProc"+e.Message); continue; } //事件参数 udpPackage package = new udpPackage(); if (package.Pack(byteStream)) { switch(package.cmd) { case 0x01: { ProcessReCmd_query(package); break; } case 0x02: { //正常移出的应答 break; } case 0x03: { //强制清除的应答 break; } default: { break; } } } } }
private void SendReadmsg() { udpPackage package = new udpPackage(); package.addr = 1; package.cmd = (byte)RfidCmdEnum.CMD_READ; package.frameType = 0x01; package.len = 0x03; try { byte[] byteStream = package.UnPack(); //_sockRfid.SendTo(byteStream, _epRfid); _udpClient.Send(byteStream, byteStream.Length); } catch (System.Exception e) { Console.WriteLine("SendReadmsg()"+e.Message); } }
private void ProcessReCmd_query(udpPackage package) { //查询命令的应答 if (package.status != 0x00) { return; } if (package.data == null) { Console.WriteLine("rfid recv error:package.data为空"); return; } string newID = System.Text.ASCIIEncoding.UTF8.GetString(package.data); if (idRead != newID) { idRead = newID; msgRFIDEventArg e = new msgRFIDEventArg(); e.ErrorHappened = false; e.rfidInfo = newID; if (RfidReadEvent != null) { int re = RfidReadEvent(this, e);//rfid扫描到新对象, 激活入库事件 switch (re) { case 0: { break; } case 1: { SendMsgClearObj(); break; } case 2: { SendMsgClearObj(); break; } case 3: { SendMsgClearObj(); break; } default: break; } } } }
/// <summary> /// 清除线上产品 /// </summary> public void SendMsgClearObj() { udpPackage package = new udpPackage(); package.addr = 1; package.cmd = (byte)RfidCmdEnum.CMD_OBJCLEAR; package.frameType = 0x01; package.len = 0x03; try { byte[] byteStream = package.UnPack(); //_sockRfid.SendTo(byteStream, _epRfid); _udpClient.Send(byteStream, byteStream.Length); } catch (System.Exception e) { Console.WriteLine("SendClearObjMsg" + e.Message); } }
/// <summary> /// 发送查询命令 /// </summary> private void SendQuerymsg() { udpPackage package1 = new udpPackage(); package1.addr = (byte)MachineNo; package1.cmd = (byte)PlcCmdEnum.CMD_QUERY; package1.frameType = 0x01; package1.len = 0x03; byte[] byteStream = package1.UnPack(); //_sockPlc.SendTo(byteStream, _epPlc); try { _udpClient.Send(byteStream, byteStream.Length); } catch (System.Exception e) { Console.WriteLine("SendQuerymsg failed:" + e.Message); } }
/// <summary> /// 接收数据线程函数 /// </summary> private void recvPlcProc() { byte[] data = new byte[1024]; while (_conPlc) { int recv =0; byte[] byteStream = null; try { // recv= _sockPlc.ReceiveFrom(data, ref _epPlc); byteStream = _udpClient.Receive(ref C_Point); _heartCount = 0; //收到数据包,清零,标识连接建立 _bConnected = true; } catch (System.Exception e) { Console.WriteLine("plc comm:"+e.Message); continue; } //事件参数 udpPackage package = new udpPackage(); for (int i = 0; i < recv; i++) { byteStream[i] = data[i]; } if (package.Pack(byteStream)) { if ((package.frameType == 0x02) && (package.addr == this.MachineNo)) { //应答帧,地址正确 if (package.cmd == (byte)HouseCmdCode.CMD_QUERY) { MachineWorkStatus newS = (MachineWorkStatus)package.data[3]; TMCoord newPos = new TMCoord(package.data[0], this.MachineNo, (package.data[1] + (package.data[2] << 8))); if (newS != MStatus) { if(MStatus == MachineWorkStatus.PRODUCT_INHOUSE_OK) { string ss = "入库任务"+(_currentCmdIndex+1).ToString()+"完成"; Console.WriteLine(ss); } MStatus = newS; // string strS = "小车"+MachineNo.ToString()+"状态:"+MStatus.ToString(); // Console.WriteLine(strS); msgTMEventArg e = new msgTMEventArg(); e.MachineNo = this.MachineNo; e.ErrorHappened = false; e.TMStatus = newS; if (EventTMStatusUpdated != null) { EventTMStatusUpdated(this, e); } } if(newPos != this.currentCoord) { msgTMEventArg e = new msgTMEventArg(); e.MachineNo = this.MachineNo; e.ErrorHappened = false; e.TMPos = newPos; if(EventTMPosUpdated != null) { EventTMPosUpdated(this,e); } } } else if (package.cmd == (byte)HouseCmdCode.CMD_PRODUCT_INHOUSE) { } else if (package.cmd == (byte)HouseCmdCode.CMD_PRODUCT_OUTHOUSE) { } } } } }
/// <summary> /// 执行指令 /// </summary> /// <param name="cmd"></param> /// <returns></returns> public bool ExecuteCmd(HouseInOutCmd cmd) { if (cmd == null) return false; CurrentCmd = cmd; udpPackage package = new udpPackage(); package.frameType = 0x01; package.len = 0x07; package.cmd = (byte)cmd.Cmdcode; package.addr = (byte)MachineNo; package.data = new byte[4]; package.data[0] = (byte)cmd.CellTarget.L; package.data[1] = (byte)cmd.CellTarget.R; package.data[2] = (byte)(cmd.CellTarget.C&0xff); package.data[3] = (byte)((cmd.CellTarget.C>>8)&0xff); byte[] byteStream = package.UnPack(); try { _udpClient.Send(byteStream, byteStream.Length); } catch (System.Exception e) { Console.WriteLine("Send ExecuteCmd failed:" + e.Message); } // _sockPlc.SendTo(byteStream, _epPlc); return true; }