Ejemplo n.º 1
0
        public void Read_DataReceivedBeforeTimeout()
        {
            using (SerialPort com1 = TCSupport.InitFirstSerialPort())
                using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1))
                {
                    char[] charXmitBuffer  = TCSupport.GetRandomChars(512, TCSupport.CharacterOptions.None);
                    var    asyncRead       = new ASyncRead(com1);
                    var    asyncReadThread = new Thread(new ThreadStart(asyncRead.Read));

                    char endLineChar    = com1.NewLine[0];
                    char notEndLineChar = TCSupport.GetRandomOtherChar(endLineChar, TCSupport.CharacterOptions.None);

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

                    //Ensure the new line is not in charXmitBuffer
                    for (var i = 0; i < charXmitBuffer.Length; ++i)
                    {
                        //Se any appearances of a character in the new line string to some other char
                        if (endLineChar == charXmitBuffer[i])
                        {
                            charXmitBuffer[i] = notEndLineChar;
                        }
                    }

                    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();
                    }

                    asyncReadThread.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(charXmitBuffer, 0, charXmitBuffer.Length);
                    com2.WriteLine(string.Empty);

                    asyncRead.ReadCompletedEvent.WaitOne();

                    Assert.Null(asyncRead.Exception);

                    if (null == asyncRead.Result || 0 == asyncRead.Result.Length)
                    {
                        Fail("Err_0158ahei Expected Read to read at least one character");
                    }
                    else
                    {
                        Assert.Equal(charXmitBuffer, asyncRead.Result.ToCharArray());
                    }

                    VerifyReadLine(com1, com2, new string(charXmitBuffer));
                }
        }
Ejemplo n.º 2
0
    public void Read_DataReceivedBeforeTimeout()
    {
        using (SerialPort com1 = TCSupport.InitFirstSerialPort())
            using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1))
            {
                char[]    charXmitBuffer = TCSupport.GetRandomChars(512, TCSupport.CharacterOptions.None);
                char[]    charRcvBuffer  = new char[charXmitBuffer.Length];
                ASyncRead asyncRead      = new ASyncRead(com1, charRcvBuffer, 0, charRcvBuffer.Length);
                System.Threading.Thread asyncReadThread = new System.Threading.Thread(asyncRead.Read);


                Debug.WriteLine(
                    "Verifying that Read(char[], int, int) will read characters 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(charXmitBuffer, 0, charXmitBuffer.Length);

                asyncRead.ReadCompletedEvent.WaitOne();

                Assert.Null(asyncRead.Exception);

                if (asyncRead.Result < 1)
                {
                    Fail("Err_0158ahei Expected Read to read at least one character {0}", asyncRead.Result);
                }
                else
                {
                    System.Threading.Thread.Sleep(1000); //We need to wait for all of the bytes to be received
                    int readResult = com1.Read(charRcvBuffer, asyncRead.Result, charRcvBuffer.Length - asyncRead.Result);

                    if (asyncRead.Result + readResult != charXmitBuffer.Length)
                    {
                        Fail("Err_051884ajoedo Expected Read to read {0} characters actually read {1}",
                             charXmitBuffer.Length - asyncRead.Result, readResult);
                    }
                    else
                    {
                        Assert.Equal(charXmitBuffer, charRcvBuffer);
                    }
                }
            }
    }
Ejemplo n.º 3
0
        public void Read_DataReceivedBeforeTimeout()
        {
            using (SerialPort com1 = TCSupport.InitFirstSerialPort())
                using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1))
                {
                    char[]    charXmitBuffer = TCSupport.GetRandomChars(512, TCSupport.CharacterOptions.None);
                    char[]    charRcvBuffer  = new char[charXmitBuffer.Length];
                    ASyncRead asyncRead      = new ASyncRead(com1);
                    var       asyncReadTask  = new Task(asyncRead.Read);


                    Debug.WriteLine(
                        "Verifying that ReadChar 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(charXmitBuffer, 0, charXmitBuffer.Length);

                    asyncRead.ReadCompletedEvent.WaitOne();

                    Assert.Null(asyncRead.Exception);

                    if (asyncRead.Result != charXmitBuffer[0])
                    {
                        Fail("Err_0158ahei Expected ReadChar to read {0}({0:X}) actual {1}({1:X})", charXmitBuffer[0], asyncRead.Result);
                    }
                    else
                    {
                        charRcvBuffer[0] = (char)asyncRead.Result;

                        int receivedLength = 1;
                        while (receivedLength < charXmitBuffer.Length)
                        {
                            receivedLength += com1.Read(charRcvBuffer, receivedLength, charRcvBuffer.Length - receivedLength);
                        }

                        Assert.Equal(receivedLength, charXmitBuffer.Length);
                        Assert.Equal(charXmitBuffer, charRcvBuffer);
                    }

                    TCSupport.WaitForTaskCompletion(asyncReadTask);
                }
        }
Ejemplo n.º 4
0
        public void Read_DataReceivedBeforeTimeout()
        {
            using (SerialPort com1 = TCSupport.InitFirstSerialPort())
                using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1))
                {
                    char[]    charXmitBuffer = TCSupport.GetRandomChars(512, TCSupport.CharacterOptions.None);
                    string    endString      = "END";
                    ASyncRead asyncRead      = new ASyncRead(com1, endString);
                    var       asyncReadTask  = new Task(asyncRead.Read);

                    char endChar    = endString[0];
                    char notEndChar = TCSupport.GetRandomOtherChar(endChar, TCSupport.CharacterOptions.None);

                    Debug.WriteLine(
                        "Verifying that ReadTo(string) will read characters that have been received after the call to Read was made");

                    //Ensure the new line is not in charXmitBuffer
                    for (int i = 0; i < charXmitBuffer.Length; ++i)
                    {
                        //Se any appearances of a character in the new line string to some other char
                        if (endChar == charXmitBuffer[i])
                        {
                            charXmitBuffer[i] = notEndChar;
                        }
                    }

                    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(charXmitBuffer, 0, charXmitBuffer.Length);
                    com2.Write(endString);

                    asyncRead.ReadCompletedEvent.WaitOne();

                    if (null != asyncRead.Exception)
                    {
                        Fail("Err_04448ajhied Unexpected exception thrown from async read:\n{0}", asyncRead.Exception);
                    }
                    else if (null == asyncRead.Result || 0 == asyncRead.Result.Length)
                    {
                        Fail("Err_0158ahei Expected Read to read at least one character");
                    }
                    else
                    {
                        char[] charRcvBuffer = asyncRead.Result.ToCharArray();

                        if (charRcvBuffer.Length != charXmitBuffer.Length)
                        {
                            Fail("Err_051884ajoedo Expected Read to read {0} characters actually read {1}", charXmitBuffer.Length, charRcvBuffer.Length);
                        }
                        else
                        {
                            for (int i = 0; i < charXmitBuffer.Length; ++i)
                            {
                                if (charRcvBuffer[i] != charXmitBuffer[i])
                                {
                                    Fail(
                                        "Err_0518895akiezp Characters differ at {0} expected:{1}({2:X}) actual:{3}({4:X})", i,
                                        charXmitBuffer[i], (int)charXmitBuffer[i], charRcvBuffer[i], (int)charRcvBuffer[i]);
                                }
                            }
                        }
                    }
                    VerifyReadTo(com1, com2, new string(charXmitBuffer), endString);
                }
        }
Ejemplo n.º 5
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);
                }
        }
Ejemplo n.º 6
0
    public bool Read_DataReceivedBeforeTimeout()
    {
        SerialPort com1 = TCSupport.InitFirstSerialPort();
        SerialPort com2 = TCSupport.InitSecondSerialPort(com1);

        char[]    charXmitBuffer = TCSupport.GetRandomChars(512, TCSupport.CharacterOptions.None);
        char[]    charRcvBuffer  = new char[charXmitBuffer.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 ReadChar will read characters 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(charXmitBuffer, 0, charXmitBuffer.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 != charXmitBuffer[0])
        {
            retValue = false;
            Console.WriteLine("Err_0158ahei Expected ReadChar to read {0}({0:X}) actual {1}({1:X})", charXmitBuffer[0], asyncRead.Result);
        }
        else
        {
            System.Threading.Thread.Sleep(1000); //We need to wait for all of the bytes to be received
            charRcvBuffer[0] = (char)asyncRead.Result;
            int readResult = com1.Read(charRcvBuffer, 1, charRcvBuffer.Length - 1);

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

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

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

        return(retValue);
    }
Ejemplo n.º 7
0
    public bool Read_DataReceivedBeforeTimeout()
    {
        SerialPort com1 = TCSupport.InitFirstSerialPort();
        SerialPort com2 = TCSupport.InitSecondSerialPort(com1);

        char[]    charXmitBuffer = TCSupport.GetRandomChars(512, TCSupport.CharacterOptions.None);
        char[]    charRcvBuffer;
        ASyncRead asyncRead = new ASyncRead(com1);

        System.Threading.Thread asyncReadThread = new System.Threading.Thread(new System.Threading.ThreadStart(asyncRead.Read));
        bool retValue       = true;
        char endLineChar    = com1.NewLine[0];
        char notEndLineChar = TCSupport.GetRandomOtherChar(endLineChar, TCSupport.CharacterOptions.None);

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

        //Ensure the new line is not in charXmitBuffer
        for (int i = 0; i < charXmitBuffer.Length; ++i)
        {//Se any appearances of a character in the new line string to some other char
            if (endLineChar == charXmitBuffer[i])
            {
                charXmitBuffer[i] = notEndLineChar;
            }
        }

        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(charXmitBuffer, 0, charXmitBuffer.Length);
        com2.WriteLine(String.Empty);

        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 (null == asyncRead.Result || 0 == asyncRead.Result.Length)
        {
            retValue = false;
            Console.WriteLine("Err_0158ahei Expected Read to read at least one character");
        }
        else
        {
            charRcvBuffer = asyncRead.Result.ToCharArray();

            if (charRcvBuffer.Length != charXmitBuffer.Length)
            {
                retValue = false;
                Console.WriteLine("Err_051884ajoedo Expected Read to read {0} cahracters actually read {1}",
                                  charXmitBuffer.Length, charRcvBuffer.Length);
            }
            else
            {
                for (int i = 0; i < charXmitBuffer.Length; ++i)
                {
                    if (charRcvBuffer[i] != charXmitBuffer[i])
                    {
                        retValue = false;
                        Console.WriteLine("Err_018956ahiaz Characters differ at {0} expected:{1}({2:X}) actual:{3}({4:X})",
                                          i, charXmitBuffer[i], (int)charXmitBuffer[i], charRcvBuffer[i], (int)charRcvBuffer[i]);
                    }
                }
            }
        }

        if (!VerifyReadLine(com1, com2, new string(charXmitBuffer)))
        {
            Console.WriteLine("Err_05188ajied Verify ReadLine after read failed");
            retValue = false;
        }

        if (!retValue)
        {
            Console.WriteLine("Err_018068ajkid Verifying that Read(char[], int, int) will read characters that have been received after the call to Read was made failed");
        }

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

        return(retValue);
    }