private void Listen()
 {
     while (ListenThreadKey)
     {
         try
         {
             foreach (object o in tcpScoketList.Keys)
             {
                 string        IP        = o.ToString();
                 TcpClient     tcpScoket = (TcpClient)tcpScoketList[IP];
                 NetworkStream netStream = (NetworkStream)netStreamList[IP];
                 if (tcpScoket != null && netStream != null)
                 {
                     if (netStream.CanRead & netStream.DataAvailable)
                     {
                         if (tcpScoket.Available >= Global.MinBagLength)
                         {
                             if (netStream.ReadByte() == base.head[0])
                             {
                                 if (netStream.ReadByte() == base.head[1])
                                 {
                                     //第3个字节的帧控制字
                                     int control = netStream.ReadByte();
                                     //第4个字节的有效数据长度
                                     int lengh = netStream.ReadByte();
                                     int times = 0;
                                     //当缓冲区内的数据不及lenth + 5时,循环等待总计500毫秒的时间等数据来。
                                     while (tcpScoket.Available < lengh + 5)
                                     {
                                         Thread.Sleep(25);
                                         times++;
                                         if (times == 20)
                                         {
                                             break;
                                         }
                                     }
                                     if (times < 20)
                                     {
                                         //500毫秒之内来了,则处理数据
                                         byte[] temp = new byte[lengh + Global.MinBagLength];
                                         temp[0] = base.head[0];
                                         temp[1] = base.head[1];
                                         temp[2] = Convert.ToByte(control);
                                         temp[3] = Convert.ToByte(lengh);
                                         netStream.Read(temp, 4, lengh + 5);
                                         //使用多播委托异步抛出收到消息的事件
                                         if (Event_GotMessage != null)
                                         {
                                             Delegate[] delegateList = Event_GotMessage.GetInvocationList();
                                             foreach (GotMessageEventHandler GMEH in delegateList)
                                             {
                                                 GMEH.BeginInvoke(temp, null, null);
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
         catch (Exception ex)
         {
             if (Global.IsShowBug)
             {
                 System.Windows.Forms.MessageBox.Show(ex.Message + "\n" + ex.TargetSite + "\n" + ex.StackTrace, "服务器底层网口类监听错误");
             }
         }
         Thread.Sleep(1);
     }
 }
Beispiel #2
0
 /// <summary>
 /// 监听线程方法
 /// </summary>
 private void Listen()
 {
     while (ListenThreadKey)
     {
         try
         {
             if (serialPort.IsOpen)
             {
                 if (serialPort.BytesToRead >= Global.MinBagLength)
                 {
                     if (serialPort.ReadByte() == base.head[0])
                     {
                         if (serialPort.ReadByte() == base.head[1])
                         {
                             //第3个字节的帧控制字
                             int control = serialPort.ReadByte();
                             //第4个字节的有效数据长度
                             int lengh = serialPort.ReadByte();
                             int times = 0;
                             //当缓冲区内的数据不及lenth + 5时,循环等待总计500毫秒的时间等数据来。
                             while (serialPort.BytesToRead < lengh + 5)
                             {
                                 Thread.Sleep(25);
                                 times++;
                                 if (times == 20)
                                 {
                                     break;
                                 }
                             }
                             if (times < 20)
                             {
                                 //500毫秒之内来了,则处理数据
                                 byte[] temp = new byte[lengh + Global.MinBagLength];
                                 temp[0] = base.head[0];
                                 temp[1] = base.head[1];
                                 temp[2] = Convert.ToByte(control);
                                 temp[3] = Convert.ToByte(lengh);
                                 serialPort.Read(temp, 4, lengh + 5);
                                 //使用多播委托异步抛出收到消息的事件
                                 if (Event_GotMessage != null)
                                 {
                                     Delegate[] delegateList = Event_GotMessage.GetInvocationList();
                                     foreach (GotMessageEventHandler GMEH in delegateList)
                                     {
                                         GMEH.BeginInvoke(temp, null, null);
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
         catch (Exception ex)
         {
             if (Global.IsShowBug)
             {
                 System.Windows.Forms.MessageBox.Show(ex.Message + "\n" + ex.TargetSite + "\n" + ex.StackTrace, "服务器底层串口类解析错误");
             }
         }
         Thread.Sleep(1);
     }
 }