Пример #1
0
        public bool SendMessage(string sendMessage)
        {
            if (!sendMessage.Equals(lastMessage))
            {
                AGVLog.WriteSendInfo(sendMessage, new StackFrame(true));
            }
            else
            {
            }
            lastMessage = sendMessage;

            Socket msock;

            try {
                msock = getTcpClient().Client;
                byte[] data = Encoding.ASCII.GetBytes(sendMessage);
                DBDao.getDao().InsertConnectMsg(sendMessage, "SendMessage");
                msock.Send(data);
                if (!"发送消息成功".Equals(lastMsgAboutSend))
                {
                    lastMsgAboutSend = "发送消息成功";
                    AGVLog.WriteConnectInfo(lastMsgAboutSend, new StackFrame(true));
                }
                return(true);
            } catch (Exception se) {
                lastMsgAboutSend = "发送消息错误" + se.Message + ",系统稍后将重新连接AGV";
                AGVLog.WriteConnectInfo(lastMsgAboutSend, new StackFrame(true));
                Closeclient();
                return(false);
            }
        }
Пример #2
0
 /// <summary>
 /// 与叉车连接
 /// </summary>
 public void connectForks()
 {
     foreach (ForkLiftWrapper fl in forkLiftWrapperList)
     {
         try {
             fl.setAGVSocketClient(new AGVSocketClient());
             fl.getAGVSocketClient().registerRecvMessageCallback(handleForkLiftMsg);
             fl.getAGVSocketClient().startRecvMsg();
         } catch (Exception ex) {
             AGVLog.WriteConnectInfo("连接到 ip: " + fl.getForkLift().ip + "port: " + fl.getForkLift().port + "失败!" + ex.Message, new StackFrame(true));
         }
     }
 }
Пример #3
0
 private void Closeclient()
 {
     try {
         if (tcpClient != null)
         {
             tcpClient.Client.Close();
             tcpClient.Close();
             tcpClient = null;
         }
         Thread.Sleep(5000);
     } catch (Exception ex) {
         lastMsgAboutSend = "关闭socket错误" + ex.Message + ",系统稍后将重新连接AGV";
         AGVLog.WriteConnectInfo(lastMsgAboutSend, new StackFrame(true));
     }
 }
Пример #4
0
        private void receive()
        {
            while (true)
            {
                try {
                    byte[] buffer = new byte[512];
                    Socket msock;
                    msock = getTcpClient().Client;
                    Array.Clear(buffer, 0, buffer.Length);
                    getTcpClient().GetStream();

                    int    bytes      = msock.Receive(buffer);
                    string receiveStr = Encoding.ASCII.GetString(buffer).Trim();

                    readTimeOutTimes = 0;                     //读取超时次数清零
                    if (receiveMsgCallback != null)
                    {
                        receiveMsgCallback(forkLiftWrapper.getForkLift().id, buffer, bytes);
                    }
                    if (!"正常接收AGV消息".Equals(lastMsgAboutSend))
                    {
                        lastMsgAboutSend = "正常接收AGV消息";
                        AGVLog.WriteConnectInfo(lastMsgAboutSend, new StackFrame(true));
                    }
                } catch (SocketException ex) {
                    if (ex.ErrorCode == 10060 && readTimeOutTimes < 10)                     //超时次数超过10次,关闭socket进行重连
                    {
                        lastMsgAboutSend = "读取消息超时" + ",系统稍后将重新连接AGV";
                        AGVLog.WriteConnectInfo(lastMsgAboutSend, new StackFrame(true));
                        readTimeOutTimes++;
                        continue;
                    }
                    lastMsgAboutSend = "读取消息错误,系统稍后将重新连接AGV";
                    AGVLog.WriteConnectInfo(lastMsgAboutSend, new StackFrame(true));
                    Closeclient();
                } catch (IOException ex) {
                    lastMsgAboutSend = "读取消息错误,系统稍后将重新连接AGV";
                    AGVLog.WriteConnectInfo(lastMsgAboutSend, new StackFrame(true));
                    Closeclient();
                } catch (Exception ex) {
                    lastMsgAboutSend = "读取消息错误,系统稍后将重新连接AGV";
                    AGVLog.WriteConnectInfo(lastMsgAboutSend, new StackFrame(true));
                    Closeclient();
                }
            }
        }
Пример #5
0
 private TcpClient getTcpConnect(string ip, int port)
 {
     try {
         if (tcpClient == null)
         {
             (tcpClient = new TcpClient()).Connect(ip, port);
             initTcpClient();
             lastMsgAboutSend = "连接到AGV" + "成功!" + "(ip:" + ip + ",port:" + port + ")";
             AGVLog.WriteConnectInfo(lastMsgAboutSend, new StackFrame(true));
         }
         return(tcpClient);
     } catch (Exception ex) {
         lastMsgAboutSend = "连接到 ip: " + ip + " port: " + port + " 失败" + ex.Message + "5秒后重新获取连接";
         AGVLog.WriteConnectInfo(lastMsgAboutSend, new StackFrame(true));
         Closeclient();
         return(getTcpConnect(ip, port));
     }
 }