コード例 #1
0
ファイル: ChargeSession.cs プロジェクト: wsmyaopeng/agv-wcs
 /// <summary>
 /// 重新尝试连接小车
 /// </summary>
 public void ReConnect()
 {
     try
     {
         if (Init())
         {
             if (ReStart())
             {
                 LastConnectTime = DateTime.Now;
             }
             else
             {
                 ChargeStationInfo io = new ChargeStationInfo();
                 io.ID          = this.DeviceID;
                 io.IsCommBreak = true;
                 DelegateState.InvokeChargeChangeEvent(io);
             }
         }
         else
         {
             ChargeStationInfo io = new ChargeStationInfo();
             io.ID          = this.DeviceID;
             io.IsCommBreak = true;
             DelegateState.InvokeChargeChangeEvent(io);
         }
     }
     catch (Exception ex)
     {
         ChargeStationInfo io = new ChargeStationInfo();
         io.ID          = this.DeviceID;
         io.IsCommBreak = true;
         DelegateState.InvokeChargeChangeEvent(io);
     }
 }
コード例 #2
0
ファイル: ChargeSession.cs プロジェクト: wsmyaopeng/agv-wcs
        /// <summary>
        /// 启动
        /// </summary>
        public bool Start()
        {
            try
            {
                IPAddress  ip   = IPAddress.Parse(ComPara.ServerIP);
                IPEndPoint ipep = new IPEndPoint(ip, ComPara.Port);//IP和端口

                Tcpsocket.Connect(new IPEndPoint(ip, ComPara.Port));

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



                //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 result = connect.EndInvoke(asyncResult);
                //if (!result)
                //{ return false; }
                //LastRecTime = DateTime.Now;

                //processor = new Thread(Communication);
                //processor.IsBackground = true;
                //processor.Start();
                return(true);
            }
            catch (Exception ex)
            {
                ChargeStationInfo io = new ChargeStationInfo();
                io.ID          = this.DeviceID;
                io.IsCommBreak = true;
                DelegateState.InvokeChargeChangeEvent(io);
                return(false);
            }
            finally
            {
                communicationobserve_timer.Enabled = true;
            }
        }
コード例 #3
0
ファイル: ChargeSession.cs プロジェクト: wsmyaopeng/agv-wcs
 /// <summary>
 /// 通讯观察线程
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 public void CommunicationObser(object sender, System.Timers.ElapsedEventArgs e)
 {
     try
     {
         communicationobserve_timer.Enabled = false;
         if (LastRecLong > 2)
         {
             //如果接受消息时间已经大于2秒,则认为车子掉线了。
             //DelegateState.InvokeDispatchStateEvent(this.DeviceID.ToString() + "充电桩,已经掉线,将重新尝试连接...");
             //通知调度程序  充电桩已经掉线
             ChargeStationInfo ChargeInfo = new ChargeStationInfo();
             ChargeInfo.ID          = this.DeviceID;
             ChargeInfo.IsCommBreak = true;
             DelegateState.InvokeChargeChangeEvent(ChargeInfo);
             if (LastConnectLong > 3)
             {
                 //如果车子掉线且连接时间超过3秒则需要重连
                 LogHelper.WriteLog("重连充电桩" + DeviceID.ToString());
                 ReConnect();
             }
         }
         //if (LastConnectLong > 4 && LastRecLong > 3)
         //{
         //    ReConnect();//重新尝试连接
         //}
         //if (LastRecLong > 3)
         //{
         //    //通知调度程序  充电桩已经掉线
         //    ChargeStationInfo ChargeInfo = new ChargeStationInfo();
         //    ChargeInfo.ID = this.DeviceID;
         //    ChargeInfo.IsCommBreak = true;
         //    DelegateState.InvokeChargeChangeEvent(ChargeInfo);
         //}
     }
     catch (Exception ex)
     {
         LogHelper.WriteErrorLog(ex);
         //DelegateState.InvokeDispatchStateEvent(this.DeviceID.ToString() + "车,观察线程异常");
     }
     finally
     {
         communicationobserve_timer.Enabled = true;
     }
 }
コード例 #4
0
ファイル: ChargeSession.cs プロジェクト: wsmyaopeng/agv-wcs
 /// <summary>
 /// 初始化
 /// </summary>
 public bool Init()
 {
     try
     {
         Clear();
         Tcpsocket = new Socket(AddressFamily.InterNetwork,
                                SocketType.Stream, ProtocolType.Tcp);
         Tcpsocket.ReceiveTimeout = 200;
         return(true);
     }
     catch (Exception ex)
     {
         ChargeStationInfo io = new ChargeStationInfo();
         io.ID          = this.DeviceID;
         io.IsCommBreak = true;
         DelegateState.InvokeChargeChangeEvent(io);
         return(false);
     }
 }
コード例 #5
0
ファイル: ChargeSession.cs プロジェクト: wsmyaopeng/agv-wcs
        /// <summary>
        /// 命令反馈
        /// </summary>
        public bool GetCallBack()
        {
            try
            {
                int    offlinecount     = 0;
                int    allheadleftlengh = 3;
                int    receivedlengh    = 0;
                byte[] bufferhead       = new byte[3];
                while (allheadleftlengh - receivedlengh > 0)
                {
                    byte[] buffertemp = new byte[allheadleftlengh - receivedlengh];
                    if (Tcpsocket.Available <= 0)
                    {
                        continue;
                    }
                    int lengh = Tcpsocket.Receive(buffertemp);
                    if (lengh <= 0)
                    {
                        if (offlinecount == 3)
                        {
                            LogHelper.WriteReciveChargeMessLog("接受的充电桩" + DeviceID.ToString() + "反馈命令超时");
                            return(false);
                        }
                        offlinecount += 1;
                        Thread.Sleep(50);
                    }
                    Buffer.BlockCopy(buffertemp, 0, bufferhead, receivedlengh, lengh);
                    receivedlengh += lengh;
                }
                if (bufferhead[0] == 0x01 && bufferhead[1] == 0x01)
                {
                    offlinecount  = 0;
                    receivedlengh = 0;
                    int    allcontentleftlengh = Convert.ToInt32(bufferhead[2]) + 2;
                    byte[] buffercontent       = new byte[allcontentleftlengh];
                    while (allcontentleftlengh - receivedlengh > 0)
                    {
                        byte[] buffertemp = new byte[allcontentleftlengh - receivedlengh];
                        if (Tcpsocket.Available <= 0)
                        {
                            continue;
                        }
                        int lengh = Tcpsocket.Receive(buffertemp);
                        if (lengh <= 0)
                        {
                            if (offlinecount == 3)
                            {
                                LogHelper.WriteReciveChargeMessLog("接受的充电桩" + DeviceID.ToString() + "反馈命令超时");
                                return(false);
                            }
                            offlinecount += 1;
                            Thread.Sleep(50);
                        }
                        Buffer.BlockCopy(buffertemp, 0, buffercontent, receivedlengh, lengh);
                        receivedlengh += lengh;
                    }
                    List <byte> msg     = new List <byte>();
                    string      SenDLog = "";
                    msg.AddRange(bufferhead);
                    msg.AddRange(buffercontent);
                    foreach (byte item in msg)
                    {
                        SenDLog += ((int)item).ToString("X") + " ";
                    }
                    LogHelper.WriteReciveChargeMessLog("接受的充电桩" + this.DeviceID.ToString() + "反馈命令:" + SenDLog);


                    //List<byte> dd = new List<byte>();
                    //dd.AddRange(new byte[] { 0x01, 0x01, 0x03, 0x06, 0x01, 0x01, 0x1C, 0x1F });
                    //var rr = BitConverter.GetBytes(CRC.CRC16(dd.Take(dd.Count - 2).ToArray(), 0, dd.Count - 3));


                    var r = BitConverter.GetBytes(CRC.CRC16(msg.Take(msg.Count - 2).ToArray(), 0, msg.Count - 3));
                    if (msg[msg.Count - 2] != r[1] || msg[msg.Count - 1] != r[0])
                    {
                        LogHelper.WriteReciveChargeMessLog("接受的充电桩" + this.DeviceID.ToString() + "校验位错误!");
                        return(false);
                    }


                    //分析充电桩状态 0待机 1故障 2进行 3完成
                    ChargeStationInfo chargestation = new ChargeStationInfo();
                    chargestation.ID = this.DeviceID;
                    string StateStr = Convert.ToString(msg[3], 2).PadLeft(8, '0');
                    if (StateStr.Substring(7, 1) == "1")
                    {
                        chargestation.ChargeState = 0;
                    }
                    else if (StateStr.Substring(6, 1) == "1")
                    {
                        chargestation.ChargeState = 1;
                    }
                    else if (StateStr.Substring(5, 1) == "1")
                    {
                        chargestation.ChargeState = 2;
                    }
                    else if (StateStr.Substring(4, 1) == "1")
                    {
                        chargestation.ChargeState = 3;
                    }
                    else
                    {
                        chargestation.ChargeState = -1;
                    }
                    chargestation.IsCommBreak = false;
                    DelegateState.InvokeChargeChangeEvent(chargestation);
                    LastRecTime = DateTime.Now;
                    return(true);
                }
                else if (bufferhead[0] == 0x01 && bufferhead[1] == 0x05)
                {
                    return(true);
                }
                return(false);
            }
            catch (Exception ex)
            { LogHelper.WriteLog("充电桩解析编解码错误!" + ex.Message); }
            return(false);
        }