Esempio n. 1
0
 public bool Connect()
 {
     try
     {
         int tryMax  = 20;
         int counter = 0;
         while (PortInUse((int)hostPort))
         {
             if (hostPort > 10000)
             {
                 hostPort = 5000;
             }
             hostPort += 1;
             counter++;
             if (counter > tryMax)
             {
                 Console.WriteLine("{0}RFID连接失败,端口占用,自动分配方案失败", readerIP);
                 return(false);
             }
         }
         if (udpClient == null)
         {
             udpClient = new UdpClient((int)hostPort);
         }
         this.udpRemotePoint = new IPEndPoint(IPAddress.Parse(readerIP), (int)readerPort);
         WqrfidUdpState udpReceiveState = new WqrfidUdpState();
         udpReceiveState.ipEndPoint = this.udpRemotePoint;
         udpReceiveState.udpClient  = udpClient;
         // udpClient.Connect(this.udpRemotePoint);
         udpClient.BeginReceive(UdpRecvCallback, udpReceiveState);
         return(true);
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         return(false);
     }
 }
Esempio n. 2
0
        private void UdpRecvCallback(IAsyncResult iar)
        {
            WqrfidUdpState udpState = iar.AsyncState as WqrfidUdpState;

            if (iar.IsCompleted)
            {
                Byte[] recBytes = udpState.udpClient.EndReceive(iar, ref udpState.ipEndPoint);
                if (recBytes != null && recBytes.Count() > 0)
                {
                    lock (bufLock)
                    {
                        this.recBuffer.AddRange(recBytes);
                    }
                    //Console.Write("Recv:");
                    //for(int i=0;i<recBytes.Count();i++)
                    //{
                    //    Console.Write("{0}", recBytes[i].ToString("X2"));
                    //}
                    //Console.WriteLine("");
                }
            }
            udpClient.BeginReceive(UdpRecvCallback, udpState);
        }