/// <summary> /// 解包服务端 /// </summary> /// <param name="AGV">对象</param> public List <byte> Unpack(AppSession Session, SimulationAGVInstructions SendMes) { try { d.Clear(); d.Add(Convert.ToByte(85)); //AGVID d.Add((byte)(SendMes.AGVId)); //序列号 byte[] SerialNumber = BitConverter.GetBytes(short.Parse(SendMes.SerialNumber.ToString())); //physicaSite(SerialNumber); d.AddRange(SerialNumber); //响应码 d.Add(Convert.ToByte(ResponseCode(SendMes.ResponseCode))); //运行状态 d.Add((byte)(SendMes.RunningState)); //物理站点2 byte[] sks = BitConverter.GetBytes(short.Parse(SendMes.PhysicalSite.ToString())); d.AddRange(sks); //physicaSite(sks); //里程计数4 byte[] skt = BitConverter.GetBytes(SendMes.CountMileage); //physicaSite(skt); d.AddRange(skt); //逻辑站点2 byte[] skst = BitConverter.GetBytes(short.Parse(SendMes.LogicalSite.ToString())); //physicaSite(skst); d.AddRange(skst); //传感状态 d.Add((byte)SendMes.SensingState); //当前速度 d.Add((byte)(SendMes.CurrentSpeed)); //方向代码 byte EnterRetreat = (byte)(SendMes.EnentRetreat); //进退 byte About = (byte)(SendMes.LeftRight); //左右 int[] LengthByte = { 0, 0, 0, 0, 0, 0, 0, 0 }; LengthByte[0] = EnterRetreat; LengthByte[1] = About; string LengthByteStr = "";; for (int i = 0; i < LengthByte.Length; i++) { LengthByteStr += LengthByte[i]; } var DirectionCode = Convert.ToByte(LengthByteStr, 2); d.Add(DirectionCode); //挂钩状态 d.Add((byte)(SendMes.HookStatus)); //扩展输入 d.Add((byte)(SendMes.ExtendedInput)); //扩展输出 d.Add((byte)(SendMes.ExtendedOutput)); string fq = SendMes.BatteryVoltage.ToString(); //电池电压2 BatteryVoltage(SendMes.BatteryVoltage); //填 充 扩 展 RFID 2 byte[] kg = BitConverter.GetBytes(short.Parse(SendMes.FillExtendedRFID.ToString())); //physicaSite(kg); d.AddRange(kg); byte bs = GetCheckByte(d); d.Add(bs); d.Add(0xaa); return(d); } catch (Exception ex) { throw ex; } }
/// <summary> /// 给一个客户提供服务,接受公司an【】 /// </summary> /// <param name="obj"></param> public void ServiceClient(object obj) { try { bool keepalive = true; AppSession clientsession = FindSession(obj.ToString()); if (clientsession == null) { keepalive = false; return; } try { while (keepalive) { //处理接受包 int offlinecount = 0; //离线计数 int allheadleftlengh = 26; //全部长度 int receivedlengh = 0; byte[] bufferhead = new byte[26]; // while (allheadleftlengh - receivedlengh > 0) { byte[] buffertemp = new byte[allheadleftlengh - receivedlengh]; int lengh = clientsession.ClientSocket.Receive(buffertemp); if (lengh <= 0) { if (offlinecount == 3) { throw new Exception("Socket 错误!"); } offlinecount += 1; Thread.Sleep(1000 * 2);//再次循环休眠2000毫秒 } Buffer.BlockCopy(buffertemp, 0, bufferhead, receivedlengh, lengh); receivedlengh += lengh; clientsession._lastactivedatetime = DateTime.Now; } if (Convert.ToString(bufferhead[0], 16) != "55" && Convert.ToString(bufferhead[25], 16) != "aa") { throw new Exception("Socket 错误!"); } List <byte> msg = bufferhead.ToList(); #region 手动 if (ReceiveMes != null) { if (msg[4] == 2 || msg[4] == 0 || msg[4] == 1 || msg[4] == 3 || msg[4] == 4 || msg[4] == 6 || msg[4] == 7 || msg[4] == 8 || msg[4] == 9 || msg[4] == 10 || msg[4] == 11 || msg[4] == 12 || msg[4] == 13 || msg[4] == 14 || msg[4] == 17 || msg[4] == 18 || msg[4] == 5 || msg[4] == 16) { GetInstructions(msg[4]); ResponseCode(AGVState.ResponseCode); ReceiveMes(new NetEventArgs(clientsession, msg)); } } #endregion #region 自动 if (ServerCarStates != null) { if (msg[4] == 16) { AGVState.GetRequestCommand(msg); //AGVState.delegateState(); //ServerCarStates(clientsession, AGVState, msg); } if (msg[4] == 5) { AGVState.Run(msg); //GetInstructions(msg[4]); //ServerCarStates(clientsession, AGVState, msg); } if (msg[4] == 9) { AGVState.Run(msg); //GetInstructions(msg[4]); //ServerCarStates(clientsession, AGVState, msg); } GetInstructions(msg[4]); ResponseCode(AGVState.ResponseCode); ServerCarStates(clientsession, AGVState, msg); } #endregion } } catch (Exception ex) { } } catch (Exception ex) { } }