コード例 #1
0
        /// <summary>
        /// 数据接收
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PortDataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            Thread.Sleep(50);
            ////禁止接收事件时直接退出
            //if (OnReceive==null) return;
                        if (this.WasDisposed)
            {
                return;
            }
                                           //如果正在关闭,忽略操作,直接返回,尽快的完成串口监听线程的一次循环
                        try
            {
                IsParsing = true;  //设置标记,说明已经开始处理数据,一会儿要使用系统UI
                                int n = ListenerSocket.BytesToRead;
                byte[] buf            = new byte[n];
                ListenerSocket.Read(buf, 0, n);
                int receive_count = n;
                ReactorConnectionAdapter adapter = ((ReactorConnectionAdapter)ConnectionAdapter);
                FrameBase frame = adapter.ParsingReceivedData(buf);
                if (frame != null)
                {
                    //触发整条记录的处理
                    INode       node       = null;
                    IConnection connection = adapter.GetConnection(frame);
                    if (connection != null)
                    {
                        connection.RemoteHost.TaskTag = connection.ControlCode;
                        node = connection.RemoteHost;
                    }
                    else
                    {
                        node         = this.LocalEndpoint;
                        node.TaskTag = "none";
                    }
                    NetworkState state = CreateNetworkState(Listener, node);
                    state.RawBuffer = frame.FrameBytes;

                    this.ReceiveCallback(state);
                }
                else
                {
                    string log = String.Format("{0}:串口丢弃数据-->>{1}", DateTime.Now.ToString("hh:mm:ss"), this.Encoder.ByteEncode(buf));
                    Console.WriteLine(log);
                }
            }
            catch (Exception ex)
            {
                string enmsg = string.Format("Serial Port {0} Communication Fail\r\n" + ex.ToString(), ListenerSocket.PortName);
            }
            finally
            {
                IsParsing = false;   //监听完毕, UI可关闭串口
                           
            }
        }
コード例 #2
0
        /// <summary>
        /// 处理接收数据[解析+分配链接]
        /// </summary>
        /// <param name="availableData">远程主机原始数据</param>
        /// <param name="networkState">网络请求数据</param>
        protected override void ReceivedData(NetworkData availableData)
        {
            //检查未处理的缓冲区数据是否超时
            networkState.CheckPraseTimeOut();
            networkState.Buffer.WriteBytes(availableData.Buffer, 0, availableData.Length);
            LogTemplate("接收数据-{0}-->>{1}", networkState.Buffer.ToArray());
            ReactorConnectionAdapter adapter = ((ReactorConnectionAdapter)ConnectionAdapter);

            while (networkState.Buffer.ReadableBytes > 0)
            {
                if (networkState.CheckPraseTimeOut())
                {
                    return;
                }
                byte[]    readableBuffer = networkState.Buffer.ToArray();
                FrameBase frame          = adapter.ParsingReceivedData(readableBuffer);
                if (frame != null)
                {
                    ////触发整条记录的处理
                    if (frame.MatchOffset != 0)
                    {//从缓冲区移除要丢弃的数据
                        byte[] removeByte = networkState.Buffer.ReadBytes(frame.MatchOffset);
                        LogTemplate("丢弃数据-{0}-->>{1}", removeByte);
                    }
                    if (frame.FrameBytes != null && frame.FrameBytes.Length > 0)
                    {
                        INode remoteHost = availableData.RemoteHost.Clone() as INode;
                        remoteHost.TaskTag = frame.ControlCode;
                        NetworkState state = CreateNetworkState(networkState.Socket, remoteHost);
                        state.RawBuffer = networkState.Buffer.ReadBytes(frame.FrameBytes.Length);
                        this.ReceiveCallback(state);
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }
            }
            if (networkState.Buffer.WritableBytes == 0)
            {
                LogTemplate("丢弃数据-{0}-->>{1}", networkState.Buffer.ToArray());
            }
        }
コード例 #3
0
 /// <summary>
 /// 数据接收
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void PortDataReceived(object sender, SerialDataReceivedEventArgs e)
 {
     Thread.Sleep(50);
     ////禁止接收事件时直接退出
     //if (OnReceive==null) return;
                 if (this.WasDisposed)
     {
         return;
     }
                                    //如果正在关闭,忽略操作,直接返回,尽快的完成串口监听线程的一次循环
                 try
     {
         IsParsing = true;  //设置标记,说明已经开始处理数据,一会儿要使用系统UI
                         int n = Listener.BytesToRead;
         if (n < 1)
         {
             IsParsing = false;
             return;
         }
         string log = "";
         //检查未处理的缓冲区数据是否超时
         networkState.CheckPraseTimeOut();
         networkState.RawBuffer = new byte[n];
         Listener.Read(networkState.RawBuffer, 0, n);
         networkState.Buffer.WriteBytes(networkState.RawBuffer, 0, n);
         ReactorConnectionAdapter adapter = ((ReactorConnectionAdapter)ConnectionAdapter);
         while (networkState.Buffer.ReadableBytes > 0)
         {
             if (networkState.CheckPraseTimeOut())
             {
                 return;
             }
             FrameBase frame = adapter.ParsingReceivedData(networkState.Buffer.ToArray());
             if (frame != null)
             {
                 //触发整条记录的处理
                 INode       node       = null;
                 IConnection connection = adapter.GetConnection(frame);
                 if (connection != null)
                 {
                     connection.RemoteHost.TaskTag = connection.ControlCode;
                     node = connection.RemoteHost;
                 }
                 else
                 {
                     node         = this.LocalEndpoint;
                     node.TaskTag = "none";
                 }
                 NetworkState state = CreateNetworkState(Listener, node);
                 if (frame.MatchOffset != 0)
                 {
                     byte[] removeByte = networkState.Buffer.ReadBytes(frame.MatchOffset);
                     log = String.Format("{0}:串口丢弃数据-->>{1}", DateTime.Now.ToString("hh:mm:ss"),
                                         this.Encoder.ByteEncode(removeByte));
                     Console.WriteLine(log);
                 }
                 if (frame.FrameBytes != null && frame.FrameBytes.Length > 0)
                 {
                     state.RawBuffer = networkState.Buffer.ReadBytes(frame.FrameBytes.Length);
                     this.ReceiveCallback(state);
                 }
             }
             else
             {
                 log = String.Format("{0}:串口未处理数据-->>{1}", DateTime.Now.ToString("hh:mm:ss"), this.Encoder.ByteEncode(networkState.Buffer.ToArray()));
                 Console.WriteLine(log);
                 break;
             }
         }
         if (networkState.Buffer.WritableBytes == 0)
         {
             log = String.Format("{0}:串口丢弃数据-->>{1}", DateTime.Now.ToString("hh:mm:ss"),
                                 this.Encoder.ByteEncode(networkState.Buffer.ToArray()));
             Console.WriteLine(log);
         }
     }
     catch (Exception ex)
     {
         string enmsg = string.Format("Serial Port {0} Communication Fail\r\n" + ex.ToString(), Listener.PortName);
         Console.WriteLine(enmsg);
     }
     finally
     {
         IsParsing = false;   //监听完毕, UI可关闭串口
                    
     }
 }