Example #1
0
        public Result Connect(Commor commor)
        {
            commor.Connector = null;
            var ethernetCommor = (EthernetCommor)commor.Communicator;
            var socket         = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                ConnectSocketDelegate connect     = ConnectSocket;
                IAsyncResult          asyncResult = connect.BeginInvoke(socket, ethernetCommor.IP, ethernetCommor.Port, null, null);
                bool success = asyncResult.AsyncWaitHandle.WaitOne(1000, false);
                if (!success)
                {
                    return(new Result(string.Format("连接{0}失败({1} :{2})", ethernetCommor.Name, ethernetCommor.IP, ethernetCommor.Port)));
                }
                //socket.Connect(ethernetCommor.IP, ethernetCommor.Port); 连接失败时卡死 超时(20s)
            }
            catch (Exception ex)
            {
                return(new Result(ex.Message));
            }
            if (!socket.Connected)
            {
                return(new Result(string.Format("连接失败:{0}:{1}", ethernetCommor.IP, ethernetCommor.Port)));
            }
            commor.Connector = socket;
            return(Result.Success);
        }
Example #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;
            }
        }
Example #3
0
 public AGVSession_OMARK(int agvid, AGVComPara ComPara) : base()
 {
     try
     {
         this.DeviceID   = agvid;
         this.DeviceType = 0;
         this.DeviceName = "AGV小车";
         this.ComPara    = ComPara;
         communicationobserve_timer.Elapsed += new System.Timers.ElapsedEventHandler(CommunicationObser);
         connect    = ConnectSocket;
         SocketSend = SendData;
     }
     catch (Exception ex)
     {
         LogHelper.WriteErrorLog(ex);
     }
 }
Example #4
0
 public IOSession_Fbell(int IOID, AGVComPara ComPara) : base()
 {
     try
     {
         this.DeviceID   = IOID;
         this.DeviceType = 2;
         this.DeviceName = IOID.ToString() + "号IO设备";
         this.ComPara    = ComPara;
         communicationobserve_timer.Elapsed += new System.Timers.ElapsedEventHandler(CommunicationObser);
         connect = ConnectSocket;
         //SocketSend = SendData;
         ////初始化读
         //readbytelist.AddRange(BitConverter.GetBytes(NextDataIndex()));
         //readbytelist.AddRange(new byte[] { 0x00, 0x00 });
         //readbytelist.AddRange(new byte[] { 0x00, 0x06 });
         //readbytelist.Add((byte)IOID);
         //readbytelist.Add(0x03);
         //readbytelist.AddRange(new byte[] { 0x01, 0x2c, 0x00, 0x02 });
     }
     catch (Exception ex)
     { LogHelper.WriteErrorLog(ex); }
 }
Example #5
0
 public AGVSession_OMARK()
 {
     communicationobserve_timer.Elapsed += new System.Timers.ElapsedEventHandler(CommunicationObser);
     connect    = ConnectSocket;
     SocketSend = SendData;
 }