Example #1
0
 /// <summary>
 /// 通讯状态观察
 /// </summary>
 private void CommunicationObser(object sender, System.Timers.ElapsedEventArgs e)
 {
     try
     {
         communicationobserve_timer.Enabled = false;
         //接收超时,进行重新连接
         if (LastConnectLong > 3)
         {
             LogHelper.WriteLog("重连" + DeviceID.ToString() + "号IO设备");
             ReConnect();
         }
         if (LastRecLong > 2)
         {
             IODeviceInfo IOInfo = new IODeviceInfo();
             IOInfo.ID = this.DeviceID;
             DelegateState.InvokeDispatchStateEvent(this.DeviceID.ToString() + "号IO设备掉线,重新尝试连接...");
             //通知调度程序  小车已经掉线
             IOInfo.bIsCommBreak = true;
             DelegateState.InvokeIOFeedBackEvent(IOInfo);
         }
     }
     catch (Exception ex)
     {
         DelegateState.InvokeDispatchStateEvent(this.DeviceID.ToString() + "号IO设备,观察线程异常");
         LogHelper.WriteErrorLog(ex);
     }
     finally
     {
         if (!IsStop)
         {
             communicationobserve_timer.Enabled = true;
         }
     }
 }
Example #2
0
 /// <summary>
 /// 删行
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void btnDele_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
 {
     try
     {
         IODeviceInfo CurrentIOInfo = this.bsDetail.Current as IODeviceInfo;
         if (CurrentIOInfo != null)
         {
             if (MsgBox.ShowQuestion("是否确认删除当前行") != DialogResult.No)
             {
                 OperateReturnInfo opr = AGVClientDAccess.Delete_IODeviceInfo(CurrentIOInfo);
                 if (opr.ReturnCode == OperateCodeEnum.Success)
                 {
                     bsDetail.DataSource = AGVDAccess.AGVClientDAccess.LoadIODeviceInfo();
                     bsDetail.ResetBindings(false);
                 }
                 else
                 {
                     MsgBox.Show(opr);
                 }
             }
         }
     }
     catch (Exception ex)
     { MsgBox.ShowError(ex.Message); }
 }
Example #3
0
 /// <summary>
 /// 验证行信息有效性
 /// </summary>
 private bool Valid(IODeviceInfo DeviceInfo)
 {
     try
     {
         if (DeviceInfo == null)
         {
             return(false);
         }
         if (string.IsNullOrEmpty(DeviceInfo.DeviceName))
         {
             MsgBox.ShowWarn("请维护IO设备名称!");
             return(false);
         }
         if (string.IsNullOrEmpty(DeviceInfo.IP))
         {
             MsgBox.ShowWarn("请维护IO设备IP地址!");
             return(false);
         }
         if (string.IsNullOrEmpty(DeviceInfo.Port))
         {
             MsgBox.ShowWarn("请维护IO设备端口号!");
             return(false);
         }
         return(true);
     }
     catch (Exception ex)
     {
         MsgBox.ShowError(ex.Message);
         return(false);
     }
 }
Example #4
0
        /// <summary>
        /// 启动
        /// </summary>
        /// <returns></returns>
        public bool Start()
        {
            try
            {
                //先执行ping 命令
                KeepServer = true;
                IPAddress  ip   = IPAddress.Parse(ComPara.ServerIP);
                IPEndPoint ipep = new IPEndPoint(ip, ComPara.Port);//IP和端口

                //Tcpsocket.Connect(new IPEndPoint(ip, ComPara.Port));
                ConnectSocketDelegate connect     = ConnectSocket;
                IAsyncResult          asyncResult = connect.BeginInvoke(ipep, Tcpsocket, null, null);

                bool connectSuccess = asyncResult.AsyncWaitHandle.WaitOne(3 * 1000, false);
                if (!connectSuccess)
                {
                    //MessageBox.Show(string.Format("失败!错误信息:{0}", "连接超时"));
                    return(false);
                }

                bool exmessage = connect.EndInvoke(asyncResult);
                if (exmessage == false)
                {
                    return(false);
                }
                //if (!string.IsNullOrEmpty(exmessage))
                //{
                //    //MessageBox.Show(string.Format("失败!错误信息:{0}", exmessage));
                //    return false;
                //}
                LastRecTime = DateTime.Now;

                processor = new Thread(Communication);
                processor.IsBackground = true;
                processor.Start();

                RecevProvessor = new Thread(ReceverMes);
                RecevProvessor.IsBackground = true;
                RecevProvessor.Start();
                return(true);
            }
            catch (Exception ex)
            {
                KeepServer = false;
                IODeviceInfo IOInfo = new IODeviceInfo();
                IOInfo.ID           = this.DeviceID;
                IOInfo.bIsCommBreak = true;
                DelegateState.InvokeIOFeedBackEvent(IOInfo);
                return(false);
            }
            finally
            {
                this.communicationobserve_timer.Enabled = true;
            }
        }
Example #5
0
 /// <summary>
 /// 触发IO设备信息反馈
 /// </summary>
 /// <param name="e"></param>
 public async static void InvokeIOFeedBackEvent(IODeviceInfo e)
 {
     try
     {
         await Task.Factory.StartNew(() =>
         {
             IOFeedEvent?.Invoke(e);
         });
     }
     catch (Exception ex)
     {
         LogHelper.WriteErrorLog(ex);
     }
 }
Example #6
0
 /// <summary>
 /// 新增
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void barAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
 {
     try
     {
         gvDetail.CloseEditor();
         IODeviceInfo CurrentIOInfo = this.bsDetail.Current as IODeviceInfo;
         if (bsDetail.Count <= 0 || (CurrentIOInfo != null && Valid(CurrentIOInfo)))
         {
             IODeviceInfo newItem = new IODeviceInfo();
             newItem.ID = bsDetail.Count + 1;
             bsDetail.Add(newItem);
             bsDetail.ResetBindings(false);
         }
     }
     catch (Exception ex)
     { MsgBox.ShowError(ex.Message); }
 }
Example #7
0
 /// <summary>
 /// 初始化通信
 /// </summary>
 /// <returns></returns>
 public bool Init()
 {
     try
     {
         Clear();
         Tcpsocket = new Socket(AddressFamily.InterNetwork,
                                SocketType.Stream, ProtocolType.Tcp);
         IsStop = false;
         return(true);
     }
     catch (Exception ex)
     {
         IODeviceInfo IOInfo = new IODeviceInfo();
         IOInfo.ID           = this.DeviceID;
         IOInfo.bIsCommBreak = true;
         DelegateState.InvokeIOFeedBackEvent(IOInfo);
         return(false);
     }
 }
Example #8
0
 /// <summary>
 /// 保存
 /// </summary>
 private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
 {
     try
     {
         gvDetail.CloseEditor();
         IODeviceInfo CurrentDevice = bsDetail.Current as IODeviceInfo;
         if (Valid(CurrentDevice))
         {
             IList <IODeviceInfo> AllDevices = this.bsDetail.List as IList <IODeviceInfo>;
             if (AllDevices != null && AllDevices.Count > 0)
             {
                 OperateReturnInfo opr = AGVDAccess.AGVClientDAccess.SaveIO(AllDevices);
                 MsgBox.Show(opr);
             }
         }
     }
     catch (Exception ex)
     { MsgBox.ShowError(ex.Message); }
 }
Example #9
0
 private void txtDeviceID_EditValueChanged(object sender, EventArgs e)
 {
     try
     {
         txtDeviceID.EditValueChanged -= txtDeviceID_EditValueChanged;
         IOActionInfo CurrentActionInfo = this.bsIOActionSet.Current as IOActionInfo;
         if (CurrentActionInfo != null && !string.IsNullOrEmpty(((DevExpress.XtraEditors.LookUpEdit)sender).Text))
         {
             CurrentActionInfo.DeviceID = Convert.ToInt16(((DevExpress.XtraEditors.LookUpEdit)sender).Text);
             IODeviceInfo CurrDevice = (bsIODevices.DataSource as IList <IODeviceInfo>).FirstOrDefault(p => p.ID == CurrentActionInfo.DeviceID);
             if (CurrDevice != null)
             {
                 CurrentActionInfo.DeviceName = CurrDevice.DeviceName;
             }
             bsIOActionSet.ResetBindings(false);
         }
     }
     catch (Exception ex)
     { MsgBox.ShowError(ex.Message); }
     finally
     { txtDeviceID.EditValueChanged += txtDeviceID_EditValueChanged; }
 }
Example #10
0
 /// <summary>
 /// 重新创建连接
 /// </summary>
 public void ReConnect()
 {
     try
     {
         LastConnectTime = DateTime.Now;
         if (Init())
         {
             IODeviceInfo IOInfo = new IODeviceInfo();
             IOInfo.ID = this.DeviceID;
             if (ReStart())
             {
                 LastConnectTime = DateTime.Now;
             }
             else
             {
                 DelegateState.InvokeDispatchStateEvent("尝试连接" + this.DeviceID.ToString() + "号IO设备...");
                 IOInfo.bIsCommBreak = true;
                 DelegateState.InvokeIOFeedBackEvent(IOInfo);
             }
         }
         else
         {
             DelegateState.InvokeDispatchStateEvent("尝试连接" + this.DeviceID.ToString() + "号IO设备...");
             IODeviceInfo IOInfo = new IODeviceInfo();
             IOInfo.ID           = DeviceID;
             IOInfo.bIsCommBreak = true;
             DelegateState.InvokeIOFeedBackEvent(IOInfo);
         }
     }
     catch (Exception ex)
     {
         DelegateState.InvokeDispatchStateEvent("尝试连接" + this.DeviceID.ToString() + "号IO设备...");
         IODeviceInfo IOInfo = new IODeviceInfo();
         IOInfo.ID           = DeviceID;
         IOInfo.bIsCommBreak = true;
         DelegateState.InvokeIOFeedBackEvent(IOInfo);
     }
 }
Example #11
0
        public bool SendData(List <byte> bytelist, FunctionCode fc)
        {
            try
            {
                Tcpsocket.Send(bytelist.ToArray());//发送字节
                if (fc == FunctionCode.Write)
                {
                    Thread.Sleep(60);
                }

                int    offlinecount     = 0;
                int    allheadleftlengh = 6;
                int    receivedlengh    = 0;
                byte[] bufferhead       = new byte[6];
                while (allheadleftlengh - receivedlengh > 0)
                {
                    byte[] buffertemp = new byte[allheadleftlengh - receivedlengh];
                    int    lengh      = Tcpsocket.Receive(buffertemp);
                    if (lengh <= 0)
                    {
                        if (offlinecount == 3)
                        {
                            throw new Exception("Socket  错误!");
                        }
                        offlinecount += 1;
                        Thread.Sleep(1000 * 2);
                    }
                    Buffer.BlockCopy(buffertemp, 0, bufferhead, receivedlengh, lengh);
                    receivedlengh += lengh;
                }
                offlinecount  = 0;
                receivedlengh = 0;
                int    allcontentleftlengh = int.Parse(Convert.ToString(bufferhead[5], 10));
                byte[] buffercontent       = new byte[allcontentleftlengh];
                while (allcontentleftlengh - receivedlengh > 0)
                {
                    byte[] buffertemp = new byte[allcontentleftlengh - receivedlengh];
                    int    lengh      = Tcpsocket.Receive(buffertemp);
                    if (lengh <= 0)
                    {
                        if (offlinecount == 3)
                        {
                            throw new Exception("Socket  错误!");
                        }
                        offlinecount += 1;
                        Thread.Sleep(1000 * 2);
                    }
                    Buffer.BlockCopy(buffertemp, 0, buffercontent, receivedlengh, lengh);
                    receivedlengh += lengh;
                }
                if (fc == FunctionCode.Read && buffercontent[1] == 0x0f)
                {
                    IODeviceInfo io = new IODeviceInfo();
                    io.ID = this.DeviceID;
                    //解析
                    //1-8口
                    char[] bytestr = Convert.ToString(buffercontent[4], 2).PadLeft(8, '0').ToArray();
                    for (int i = 0; i < 8; i++)
                    {
                        IOPortInfo ioport = new IOPortInfo();
                        ioport.PortNo    = 8 - i;
                        ioport.PortState = bytestr[i] == '1' ? 1 : 0;
                        io.DIPortList.Add(ioport);
                    }
                    //9-16口
                    bytestr = Convert.ToString(buffercontent[3], 2).PadLeft(8, '0').ToArray();
                    for (int i = 0; i < 8; i++)
                    {
                        IOPortInfo ioport = new IOPortInfo();
                        ioport.PortNo    = 16 - i;
                        ioport.PortState = bytestr[i] == '1' ? 1 : 0;
                        io.DIPortList.Add(ioport);
                    }
                    //17-24口
                    bytestr = Convert.ToString(buffercontent[6], 2).PadLeft(8, '0').ToArray();
                    for (int i = 0; i < 8; i++)
                    {
                        IOPortInfo ioport = new IOPortInfo();
                        ioport.PortNo    = 24 - i;
                        ioport.PortState = bytestr[i] == '1' ? 1 : 0;
                        io.DIPortList.Add(ioport);
                    }
                    //25-32口
                    bytestr = Convert.ToString(buffercontent[5], 2).PadLeft(8, '0').ToArray();
                    for (int i = 0; i < 8; i++)
                    {
                        IOPortInfo ioport = new IOPortInfo();
                        ioport.PortNo    = 32 - i;
                        ioport.PortState = bytestr[i] == '1' ? 1 : 0;
                        io.DIPortList.Add(ioport);
                    }

                    DelegateState.InvokeIOFeedBackEvent(io);
                    LastRecTime = DateTime.Now;
                    return(true);
                }
                if (fc == FunctionCode.Write && buffercontent[1] == 0x0f)
                {
                    LastRecTime = DateTime.Now;
                    return(true);
                }
                return(false);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
Example #12
0
        /// <summary>
        /// 处理命令反馈
        /// </summary>
        public bool GetCallBack()
        {
            try
            {
                int    offlinecount     = 0;
                int    allheadleftlengh = 6;
                int    receivedlengh    = 0;
                byte[] bufferhead       = new byte[6];
                while (allheadleftlengh - receivedlengh > 0)
                {
                    byte[] buffertemp = new byte[allheadleftlengh - receivedlengh];
                    int    lengh      = Tcpsocket.Receive(buffertemp);
                    if (lengh <= 0)
                    {
                        if (offlinecount == 3)
                        {
                            throw new Exception("Socket  错误!");
                        }
                        offlinecount += 1;
                        Thread.Sleep(1000 * 2);
                    }
                    Buffer.BlockCopy(buffertemp, 0, bufferhead, receivedlengh, lengh);
                    receivedlengh += lengh;
                }
                offlinecount  = 0;
                receivedlengh = 0;
                int    allcontentleftlengh = int.Parse(Convert.ToString(bufferhead[5], 10));
                byte[] buffercontent       = new byte[allcontentleftlengh];
                while (allcontentleftlengh - receivedlengh > 0)
                {
                    byte[] buffertemp = new byte[allcontentleftlengh - receivedlengh];
                    int    lengh      = Tcpsocket.Receive(buffertemp);
                    if (lengh <= 0)
                    {
                        if (offlinecount == 3)
                        {
                            throw new Exception("Socket  错误!");
                        }
                        offlinecount += 1;
                        Thread.Sleep(1000 * 2);
                    }
                    Buffer.BlockCopy(buffertemp, 0, buffercontent, receivedlengh, lengh);
                    receivedlengh += lengh;
                }

                //读返回
                if (buffercontent[1] == 0x03)
                {
                    IODeviceInfo IOInfo = new IODeviceInfo();
                    IOInfo.ID = this.DeviceID;
                    //解析 1-8口
                    char[] bytestr = Convert.ToString(buffercontent[4], 2).PadLeft(8, '0').ToArray();
                    for (int i = 0; i < 8; i++)
                    {
                        IOPortInfo ioport = new IOPortInfo();
                        ioport.PortNo    = 8 - i;
                        ioport.PortState = bytestr[i] == '1' ? 1 : 0;
                        IOInfo.DIPortList.Add(ioport);
                    }
                    //9-16口
                    bytestr = Convert.ToString(buffercontent[3], 2).PadLeft(8, '0').ToArray();
                    for (int i = 0; i < 8; i++)
                    {
                        IOPortInfo ioport = new IOPortInfo();
                        ioport.PortNo    = 16 - i;
                        ioport.PortState = bytestr[i] == '1' ? 1 : 0;
                        IOInfo.DIPortList.Add(ioport);
                    }
                    //17-24口
                    bytestr = Convert.ToString(buffercontent[6], 2).PadLeft(8, '0').ToArray();
                    for (int i = 0; i < 8; i++)
                    {
                        IOPortInfo ioport = new IOPortInfo();
                        ioport.PortNo    = 24 - i;
                        ioport.PortState = bytestr[i] == '1' ? 1 : 0;
                        IOInfo.DIPortList.Add(ioport);
                    }
                    //25-32口
                    bytestr = Convert.ToString(buffercontent[5], 2).PadLeft(8, '0').ToArray();
                    for (int i = 0; i < 8; i++)
                    {
                        IOPortInfo ioport = new IOPortInfo();
                        ioport.PortNo    = 32 - i;
                        ioport.PortState = bytestr[i] == '1' ? 1 : 0;
                        IOInfo.DIPortList.Add(ioport);
                    }
                    DelegateState.InvokeIOFeedBackEvent(IOInfo);
                    LastRecTime = DateTime.Now;
                    return(true);
                }
                //写返回
                if (buffercontent[1] == 0x10)
                {
                    LastRecTime = DateTime.Now;
                    return(true);
                }
                return(false);
            }
            catch (Exception ex)
            { LogHelper.WriteLog("AGV解析编解码错误!" + ex.Message); }
            return(false);
        }
Example #13
0
 /// <summary>
 /// 启动通讯
 /// </summary>
 public bool Start()
 {
     try
     {
         foreach (var item in AGVSessions)
         {
             new Thread(delegate()
             {
                 bool StartResult = (bool)ExcuteRflectMethod(item, "Start", null);
                 if (StartResult)
                 {
                     DelegateState.InvokeDispatchStateEvent(GetModelValue("DeviceID", item).ToString() + "号AGV启动通讯成功");
                     LogHelper.WriteLog(GetModelValue("DeviceID", item).ToString() + "号AGV启动通讯成功");
                 }
                 else
                 {
                     try
                     {
                         CarInfo Car = CoreData.CarList.FirstOrDefault(p => p.AgvID == Convert.ToInt16(GetModelValue("DeviceID", item)));
                         if (Car != null)
                         {
                             Car.bIsCommBreak = true;
                         }
                     }
                     catch
                     { }
                     DelegateState.InvokeDispatchStateEvent(GetModelValue("DeviceID", item).ToString() + "号AGV启动通讯失败");
                     LogHelper.WriteLog(GetModelValue("DeviceID", item).ToString() + "号AGV启动通讯失败");
                 }
             })
             {
                 IsBackground = true
             }.Start();
         }
         foreach (var item in ChargeStationSessions)
         {
             new Thread(delegate()
             {
                 bool StartResult = (bool)ExcuteRflectMethod(item, "Start", null);
                 if (StartResult)
                 {
                     DelegateState.InvokeDispatchStateEvent(GetModelValue("DeviceID", item).ToString() + "号充电桩通讯启动成功...");
                 }
                 else
                 {
                     DelegateState.InvokeDispatchStateEvent(GetModelValue("DeviceID", item).ToString() + "号充电桩通讯启动失败...");
                 }
             })
             {
                 IsBackground = true
             }.Start();
         }
         foreach (var item in IOSessions)
         {
             new Thread(delegate()
             {
                 if (item.Start())
                 {
                     DelegateState.InvokeDispatchStateEvent(item.DeviceID.ToString() + "号IO设备通讯启动成功...");
                 }
                 else
                 {
                     IODeviceInfo IOInfo = CoreData.IOList.FirstOrDefault(p => p.ID == item.DeviceID);
                     if (IOInfo != null)
                     {
                         IOInfo.bIsCommBreak = true;
                     }
                     DelegateState.InvokeDispatchStateEvent(item.DeviceID.ToString() + "号IO设备通讯启动失败...");
                 }
             })
             {
                 IsBackground = true
             }.Start();
         }
         return(true);
     }
     catch (Exception ex)
     {
         DelegateState.InvokeDispatchStateEvent("设备通讯启动异常");
         LogHelper.WriteErrorLog(ex);
         return(false);
     }
 }