/// <summary> /// 通讯方法 /// </summary> public void Communication() { try { while (true) { CommandToValue ctov = null; if (QueueCommand.Count > 0 && QueueCommand.TryPeek(out ctov))//有动作命令 { while ((!ExeCommand(ctov))) { Thread.Sleep(90); } QueueCommand.TryDequeue(out ctov); } //else //{ // //发送查询命令 // Tcpsocket.Send(readbytelist.ToArray()); // Thread.Sleep(300); //} } } catch (Exception ex) { Clear(); } }
/// <summary> /// 执行命令 /// </summary> public bool ExeCommand(CommandToValue ctov) { try { writebytelist.Clear(); writebytelist.AddRange(BitConverter.GetBytes(NextDataIndex())); writebytelist.AddRange(new byte[] { 0x00, 0x00 }); writebytelist.AddRange(new byte[] { 0x00, 0x0B }); writebytelist.Add((byte)this.DeviceID); writebytelist.Add(0x10); writebytelist.AddRange(new byte[] { 0x01, 0x2e, 0x00, 0x02, 0x04 }); int PostID = Convert.ToInt16(ctov.CommandValue); //通过IO端子口号计算其对应的字节信息 IList <byte> cmdbytelist = GetByteListByPortState(PostID); writebytelist.AddRange(cmdbytelist); string cmd = ""; foreach (byte b in writebytelist) { cmd += Convert.ToString(b, 16).PadLeft(2, '0') + " "; } LogHelper.WriteLog("向" + this.DeviceID.ToString() + "号IO设备发送指令:" + cmd); //Tcpsocket.Send(writebytelist.ToArray());//发送字节 //{ Thread.Sleep(60); } //var Result = GetCallBack(); //return Result; return(SendData(writebytelist, FunctionCode.Write)); } catch (Exception ex) { LogHelper.WriteLog("发送AGV命令异常:" + ex.Message); return(false); } }
public void AGV_AddCommand(int agvid, CommandToValue ctov) { try { Commer.AGV_AddControl(agvid, ctov); } catch (Exception ex) { LogHelper.WriteErrorLog(ex); } }
/// <summary> /// 通讯方法 /// </summary> public void Communication() { try { while (true) { CommandToValue ctov = null; LogHelper.WriteSendAGVMessLog($"{this.DeviceID}号AGV 当前线程{Thread.CurrentThread.ManagedThreadId}"); ///因为考虑到发送指令判断返回状态,需要在发送指令前将读取指令 /// 返回的状态指令消耗调,所以应该判断一下缓存中是否有指令 if (QueueCommand.Count > 0 && Tcpsocket.Available > 0) { ///如果缓存区有数据应该将数据消耗完 /// 否则再发送执行指令返回的指令则不知道是执行指令 /// 返回的还是发送读取指令返回的 GetCallBack(); } else if (QueueCommand.Count > 0 && QueueCommand.TryPeek(out ctov))//有动作命令 { while ((!ExeCommand(ctov))) { Thread.Sleep(200); } QueueCommand.TryDequeue(out ctov); } else { //查询心跳指令告知在地标上的信息 SetBitComand(); //发送查询命令 string SenDLog = ""; foreach (byte item in readbytelist) { SenDLog += ((int)item).ToString("X") + " "; } byte[] arr = new byte[] { readbytelist[4], readbytelist[3] }; Int16 SendPackIndex = BitConverter.ToInt16(arr, 0); LogHelper.WriteSendAGVMessLog("报文序号:" + SendPackIndex.ToString() + "发送--心跳--AGV" + this.DeviceID.ToString() + "命令" + ":" + SenDLog); while (!SendExeCommand(readbytelist)) { LogHelper.WriteSendAGVMessLog("报文序号:" + SendPackIndex.ToString() + "发送--心跳--AGV" + this.DeviceID.ToString() + "未成功,等待200ms再发送"); Thread.Sleep(200); } Thread.Sleep(100); } } } catch (Exception ex) { LogHelper.WriteSendAGVMessLog($"{this.DeviceID}号AGV Communication 异常:" + ex.Message); //Clear(); //ReConnect(); } }
public bool ExeCommand(CommandToValue ctov) { try { if (ctov.Command == AGVCommandEnum.StartCharge)//开始充电 { writebytelist.Clear(); writebytelist.AddRange(new byte[] { 0x01, 0x05 }); writebytelist.AddRange(new byte[] { 0x00, 0x07 }); writebytelist.AddRange(new byte[] { 0xFF, 0x00 }); writebytelist.AddRange(new byte[] { 0x3D, 0xFB }); string cmd = ""; foreach (byte b in writebytelist) { cmd += Convert.ToString(b, 16).PadLeft(2, '0') + " "; } LogHelper.WriteSendChargeMessLog(this.DeviceID.ToString() + "号充电桩 发送指令:" + cmd); return(SendData(writebytelist)); } if (ctov.Command == AGVCommandEnum.StopCharge)//停止充电 { writebytelist.Clear(); writebytelist.AddRange(new byte[] { 0x01, 0x05 }); writebytelist.AddRange(new byte[] { 0x00, 0x07 }); writebytelist.AddRange(new byte[] { 0x00, 0x00 }); writebytelist.Add(0x7C); writebytelist.Add(0x0B); string cmd = ""; foreach (byte b in writebytelist) { cmd += Convert.ToString(b, 16).PadLeft(2, '0') + " "; } LogHelper.WriteSendChargeMessLog(this.DeviceID.ToString() + "号充电桩 发送停止充电指令:" + cmd); return(SendData(writebytelist)); } return(true); } catch (Exception ex) { LogHelper.WriteErrorLog(ex); //DelegateState.InvokeDispatchStateEvent("充电桩指令发送失败,将重新发送"); return(false); } }
/// <summary> /// 添加IO设备指令 /// </summary> public void IO_AddControl(int ioid, CommandToValue ctov) { try { IOSession_Fbell session = IOSessions.FirstOrDefault(p => p.DeviceID == ioid); if (session != null) { session.QueueCommand.Enqueue(ctov); } else { DelegateState.InvokeDispatchStateEvent("添加指令失败,未能找到" + ioid.ToString() + "号IO设备 "); } } catch (Exception ex) { DelegateState.InvokeDispatchStateEvent("添加IO设备指令异常"); } }
/// <summary> /// 添加充电桩指令 /// </summary> /// <param name="chargeid"></param> /// <param name="ctov"></param> public void Charge_AddControl(int chargeid, CommandToValue ctov) { try { AGVSessionBase session = ChargeStationSessions.FirstOrDefault(p => (p as AGVSessionBase).DeviceID == chargeid) as AGVSessionBase; if (session != null) { session.QueueCommand.Enqueue(ctov); } else { DelegateState.InvokeDispatchStateEvent("添加指令失败,未能找到" + chargeid.ToString() + "号充电桩 "); } } catch (Exception ex) { DelegateState.InvokeDispatchStateEvent("添加充电桩指令异常"); } }
public void Communication() { try { while (true) { CommandToValue ctov = null; if (QueueCommand.Count > 0 && Tcpsocket.Available > 0) { ///如果缓存区有数据应该将数据消耗完 /// 否则再发送执行指令返回的指令则不知道是执行指令 /// 返回的还是发送读取指令返回的 GetCallBack(); } else if (QueueCommand.Count > 0 && QueueCommand.TryPeek(out ctov))//有动作命令 { while ((!ExeCommand(ctov))) { Thread.Sleep(500); } QueueCommand.TryDequeue(out ctov); } else { /* * 发送查询命令 */ SetReadByteList(); Tcpsocket.Send(readbytelist.ToArray());//发送字节 Thread.Sleep(50); GetCallBack(); Thread.Sleep(500); } } } catch (Exception ex) { //LogHelper.WriteErrorLog(ex); //Clear(); //ReConnect(); } }
/// <summary> /// 添加AGV发送指令 /// </summary> public void AGV_AddControl(int AGVID, CommandToValue ctov) { try { AGVSessionBase session = AGVSessions.FirstOrDefault(p => (p as AGVSessionBase).DeviceID == AGVID) as AGVSessionBase; if (session != null) { //LogHelper.WriteCreatTaskLog("添加AGV:"+ AGVID.ToString()+"指令-->"+ ctov.Command.ToString()); session.QueueCommand.Enqueue(ctov); } else { DelegateState.InvokeDispatchStateEvent("添加指令失败,未能找到" + AGVID.ToString() + "号AGV通信管道!"); LogHelper.WriteLog("添加指令失败,未能找到" + AGVID.ToString() + "号AGV通信管道!"); } } catch (Exception ex) { DelegateState.InvokeDispatchStateEvent("添加指令异常"); LogHelper.WriteErrorLog(ex); } }
public void WriteRegister(int agvid, int opentype)//0降 1升 { List <CommandForkliftEnum> ForkliftList = new List <CommandForkliftEnum>(); CommandForkliftEnum Forklift = null; //起始地标 //开始位 Forklift = new CommandForkliftEnum(); if (opentype == 1) { Forklift.ForkAction = 2; Forklift.height = 1; } else { Forklift.ForkAction = 1; Forklift.height = 0; } ForkliftList.Add(Forklift); CommandToValue CommandTo = new CommandToValue(AGVCommandEnum.WriteRegister1); CommandTo.CommandValue = ForkliftList; AGV_AddCommand(agvid, CommandTo); }
/// <summary> /// 解析执行AGV指令 /// </summary> public bool ExeCommand(CommandToValue ctov) { try { string SenDLog = ""; //查询 if (ctov.Command == AGVCommandEnum.Start)//启动 { writebytelist.Clear(); #region RTU writebytelist.AddRange(new byte[] { 0xEB, 0x90, 0xDF }); writebytelist.AddRange(BitConverter.GetBytes(NextDataIndex())); writebytelist.AddRange(new byte[] { 0x00, 0x02, 0x04, 0x00 }); byte crc8 = CRC.CRC8(writebytelist.ToArray()); writebytelist.Add(crc8); #endregion foreach (byte item in writebytelist) { SenDLog += ((int)item).ToString("X") + " "; } byte[] arr = new byte[] { writebytelist[4], writebytelist[3] }; Int16 SendPackIndex = BitConverter.ToInt16(arr, 0); LogHelper.WriteSendAGVMessLog("报文序号:" + SendPackIndex.ToString() + "发送启动AGV" + this.DeviceID.ToString() + "命令" + ":" + SenDLog); //while (!SendExeCommand(writebytelist)) //{ // Thread.Sleep(50); //} if (!SendExeCommand(writebytelist)) { return(false); } LogHelper.WriteSendAGVMessLog("报文序号:" + SendPackIndex.ToString() + "发送启动AGV" + this.DeviceID.ToString() + "命令" + ":" + SenDLog + "成功!"); return(true); } else if (ctov.Command == AGVCommandEnum.Stop)//停止,暂时停车,不清除路线 { writebytelist.Clear(); #region RTU writebytelist.AddRange(new byte[] { 0xEB, 0x90, 0xDF }); writebytelist.AddRange(BitConverter.GetBytes(NextDataIndex())); writebytelist.AddRange(new byte[] { 0x00, 0x02, 0x03, 0x00 }); byte crc8 = CRC.CRC8(writebytelist.ToArray()); writebytelist.Add(crc8); #endregion foreach (byte item in writebytelist) { SenDLog += ((int)item).ToString("X") + " "; } byte[] arr = new byte[] { writebytelist[4], writebytelist[3] }; Int16 SendPackIndex = BitConverter.ToInt16(arr, 0); LogHelper.WriteSendAGVMessLog("报文序号:" + SendPackIndex.ToString() + "发送停止AGV" + this.DeviceID.ToString() + "命令" + ":" + SenDLog); //while (!SendExeCommand(writebytelist)) //{ // Thread.Sleep(50); //} if (!SendExeCommand(writebytelist)) { return(false); } LogHelper.WriteSendAGVMessLog("报文序号:" + SendPackIndex.ToString() + "发送停止AGV" + this.DeviceID.ToString() + "命令" + ":" + SenDLog + "成功!"); return(true); } else if (ctov.Command == AGVCommandEnum.CancelTast) { writebytelist.Clear(); #region RTU writebytelist.AddRange(new byte[] { 0xEB, 0x90, 0xDF }); writebytelist.AddRange(BitConverter.GetBytes(NextDataIndex())); writebytelist.AddRange(new byte[] { 0x00, 0x02, 0x05, 0x00 }); byte crc8 = CRC.CRC8(writebytelist.ToArray()); writebytelist.Add(crc8); #endregion foreach (byte item in writebytelist) { SenDLog += ((int)item).ToString("X") + " "; } byte[] arr = new byte[] { writebytelist[4], writebytelist[3] }; Int16 SendPackIndex = BitConverter.ToInt16(arr, 0); LogHelper.WriteSendAGVMessLog("报文序号:" + SendPackIndex.ToString() + "发送复位AGV" + this.DeviceID.ToString() + "命令" + ":" + SenDLog); //while (!SendExeCommand(writebytelist,true)) //{ // Thread.Sleep(50); //} if (!SendExeCommand(writebytelist, true)) { return(false); } LogHelper.WriteSendAGVMessLog("报文序号:" + SendPackIndex.ToString() + "发送复位AGV" + this.DeviceID.ToString() + "命令" + ":" + SenDLog + "成功!"); return(true); } else if (ctov.Command == AGVCommandEnum.ChangeRoute)//下发路径 { writebytelist.Clear(); #region RTU List <CommandLandMark> CommandLandmarkList = ctov.CommandValue as List <CommandLandMark>; int totalbytecount = CommandLandmarkList.Count * 15; writebytelist.AddRange(new byte[] { 0xEB, 0x90, 0xDE }); writebytelist.AddRange(BitConverter.GetBytes(NextDataIndex())); writebytelist.Add(BitConverter.GetBytes(totalbytecount)[1]); writebytelist.Add(BitConverter.GetBytes(totalbytecount)[0]); string LandAcotcinLog = ""; //循环添加功能地标信息 foreach (CommandLandMark cmdlm in CommandLandmarkList) { //组装导航路径中的节点坐标和角度 writebytelist.Add((byte)cmdlm.Avoidance); byte[] Xbytes = CRC.ToByte(cmdlm.X); writebytelist.AddRange(Xbytes.Reverse().ToArray()); byte[] Ybytes = CRC.ToByte(cmdlm.Y); writebytelist.AddRange(Ybytes.Reverse().ToArray()); byte[] Wbytes = CRC.ToByte(cmdlm.Angle); writebytelist.AddRange(Wbytes.Reverse().ToArray()); //组装导航信息中的最高移速 LandAcotcinLog += "地标:[" + cmdlm.LandmarkCode + "] X:[" + cmdlm.X.ToString() + "] Y:[" + cmdlm.Y.ToString() + "] 角度:[" + cmdlm.Angle.ToString() + "] "; writebytelist.Add((byte)cmdlm.Move_Speed); LandAcotcinLog += "速度:[" + cmdlm.Move_Speed.ToString() + "] 导航方式:[" + cmdlm.Avoidance.ToString() + "] "; //组装节点中的动作 writebytelist.Add((byte)cmdlm.ActionType); LandAcotcinLog += "动作:[" + cmdlm.ActionType.ToString() + "] "; } byte crc8 = CRC.CRC8(writebytelist.ToArray()); writebytelist.Add(crc8); #endregion foreach (byte item in writebytelist) { SenDLog += ((int)item).ToString("X").PadLeft(2, '0') + " "; } byte[] arr = new byte[] { writebytelist[4], writebytelist[3] }; Int16 SendPackIndex = BitConverter.ToInt16(arr, 0); LogHelper.WriteSendAGVMessLog("发送线路及动作说明:" + LandAcotcinLog); LogHelper.WriteSendAGVMessLog("报文序号:" + SendPackIndex.ToString() + "发送导航路径AGV" + this.DeviceID.ToString() + "命令" + ":" + SenDLog); //while (!SendExeCommand(writebytelist)) //{ // Thread.Sleep(50); //} if (!SendExeCommand(writebytelist)) { return(false); } LogHelper.WriteSendAGVMessLog("报文序号:" + SendPackIndex.ToString() + "发送导航路径AGV" + this.DeviceID.ToString() + "命令" + ":" + SenDLog + "成功!"); return(true); } else if (ctov.Command == AGVCommandEnum.SpeedSet)//速度设置 { return(true); } else if (ctov.Command == AGVCommandEnum.SetPBS)//设置避障 { return(true); } return(false); } catch (Exception ex) { LogHelper.WriteLog("发送AGV命令异常:" + ex.Message); return(false); } }