Exemplo n.º 1
0
        public void Read_DataReceivedBeforeTimeout()
        {
            using (SerialPort com1 = TCSupport.InitFirstSerialPort())
                using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1))
                {
                    byte[]    byteXmitBuffer = TCSupport.GetRandomBytes(512);
                    byte[]    byteRcvBuffer  = new byte[byteXmitBuffer.Length];
                    ASyncRead asyncRead      = new ASyncRead(com1, byteRcvBuffer, 0, byteRcvBuffer.Length);
                    var       asyncReadTask  = new Task(asyncRead.Read);


                    Debug.WriteLine(
                        "Verifying that Read(byte[], int, int) will read characters that have been received after the call to Read was made");

                    com1.Encoding    = Encoding.UTF8;
                    com2.Encoding    = Encoding.UTF8;
                    com1.ReadTimeout = 20000; // 20 seconds

                    com1.Open();

                    if (!com2.IsOpen) //This is necessary since com1 and com2 might be the same port if we are using a loopback
                    {
                        com2.Open();
                    }

                    asyncReadTask.Start();
                    asyncRead.ReadStartedEvent.WaitOne();
                    //This only tells us that the thread has started to execute code in the method
                    Thread.Sleep(2000); //We need to wait to guarentee that we are executing code in SerialPort
                    com2.Write(byteXmitBuffer, 0, byteXmitBuffer.Length);

                    asyncRead.ReadCompletedEvent.WaitOne();

                    if (null != asyncRead.Exception)
                    {
                        Fail("Err_04448ajhied Unexpected exception thrown from async read:\n{0}", asyncRead.Exception);
                    }
                    else if (asyncRead.Result < 1)
                    {
                        Fail("Err_0158ahei Expected Read to read at least one character {0}", asyncRead.Result);
                    }
                    else
                    {
                        Thread.Sleep(1000); //We need to wait for all of the bytes to be received
                        int readResult = com1.Read(byteRcvBuffer, asyncRead.Result, byteRcvBuffer.Length - asyncRead.Result);

                        if (asyncRead.Result + readResult != byteXmitBuffer.Length)
                        {
                            Fail("Err_051884ajoedo Expected Read to read {0} characters actually read {1}",
                                 byteXmitBuffer.Length - asyncRead.Result, readResult);
                        }
                        else
                        {
                            for (int i = 0; i < byteXmitBuffer.Length; ++i)
                            {
                                if (byteRcvBuffer[i] != byteXmitBuffer[i])
                                {
                                    Fail(
                                        "Err_05188ahed Characters differ at {0} expected:{1}({1:X}) actual:{2}({2:X}) asyncRead.Result={3}",
                                        i, byteXmitBuffer[i], byteRcvBuffer[i], asyncRead.Result);
                                }
                            }
                        }
                    }

                    TCSupport.WaitForTaskCompletion(asyncReadTask);
                }
        }
Exemplo n.º 2
0
    public bool Read_DataReceivedBeforeTimeout()
    {
        SerialPort com1 = TCSupport.InitFirstSerialPort();
        SerialPort com2 = TCSupport.InitSecondSerialPort(com1);

        byte[]    byteXmitBuffer = TCSupport.GetRandomBytes(512);
        byte[]    byteRcvBuffer  = new byte[byteXmitBuffer.Length];
        ASyncRead asyncRead      = new ASyncRead(com1);

        System.Threading.Thread asyncReadThread = new System.Threading.Thread(new System.Threading.ThreadStart(asyncRead.Read));
        bool retValue = true;

        Console.WriteLine("Verifying that ReadByte() will read bytes that have been received after the call to Read was made");

        com1.Encoding    = System.Text.Encoding.UTF8;
        com2.Encoding    = System.Text.Encoding.UTF8;
        com1.ReadTimeout = 20000; // 20 seconds

        com1.Open();

        if (!com2.IsOpen) //This is necessary since com1 and com2 might be the same port if we are using a loopback
        {
            com2.Open();
        }

        asyncReadThread.Start();
        asyncRead.ReadStartedEvent.WaitOne(); //This only tells us that the thread has started to execute code in the method
        System.Threading.Thread.Sleep(2000);  //We need to wait to guarentee that we are executing code in SerialPort
        com2.Write(byteXmitBuffer, 0, byteXmitBuffer.Length);

        asyncRead.ReadCompletedEvent.WaitOne();

        if (null != asyncRead.Exception)
        {
            retValue = false;
            Console.WriteLine("Err_04448ajhied Unexpected exception thrown from async read:\n{0}", asyncRead.Exception);
        }
        else if (asyncRead.Result != byteXmitBuffer[0])
        {
            retValue = false;
            Console.WriteLine("Err_0158ahei Expected ReadChar to read {0}({0:X}) actual {1}({1:X})", byteXmitBuffer[0], asyncRead.Result);
        }
        else
        {
            System.Threading.Thread.Sleep(1000); //We need to wait for all of the bytes to be received
            byteRcvBuffer[0] = (byte)asyncRead.Result;
            int readResult = com1.Read(byteRcvBuffer, 1, byteRcvBuffer.Length - 1);

            if (1 + readResult != byteXmitBuffer.Length)
            {
                retValue = false;
                Console.WriteLine("Err_051884ajoedo Expected Read to read {0} bytes actually read {1}",
                                  byteXmitBuffer.Length - 1, readResult);
            }
            else
            {
                for (int i = 0; i < byteXmitBuffer.Length; ++i)
                {
                    if (byteRcvBuffer[i] != byteXmitBuffer[i])
                    {
                        retValue = false;
                        Console.WriteLine("Err_05188ahed Characters differ at {0} expected:{1}({1:X}) actual:{2}({2:X}) asyncRead.Result={3}",
                                          i, byteXmitBuffer[i], byteRcvBuffer[i], asyncRead.Result);
                    }
                }
            }
        }

        if (!retValue)
        {
            Console.WriteLine("Err_018068ajkid Verifying that ReadByte() will read bytes that have been received after the call to Read was made failed");
        }

        com1.Close();
        com2.Close();

        return(retValue);
    }