コード例 #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;
         }
     }
 }
コード例 #2
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;
            }
        }
コード例 #3
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);
     }
 }
コード例 #4
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);
     }
 }
コード例 #5
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);
            }
        }
コード例 #6
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);
        }