Exemple #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));
                }
        }
Exemple #2
0
    public void Read_Timeout()
    {
        using (SerialPort com1 = TCSupport.InitFirstSerialPort())
            using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1))
            {
                char[] charXmitBuffer = TCSupport.GetRandomChars(512, TCSupport.CharacterOptions.None);
                string endString      = "END";
                char   endChar        = endString[0];
                char   notEndChar     = TCSupport.GetRandomOtherChar(endChar, TCSupport.CharacterOptions.None);
                string result;

                Debug.WriteLine("Verifying that ReadTo(string) works appropriately after TimeoutException has been thrown");

                //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.Unicode;
                com2.Encoding    = Encoding.Unicode;
                com1.ReadTimeout = 500; // 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();
                }

                com2.Write(charXmitBuffer, 0, charXmitBuffer.Length);

                Assert.Throws <TimeoutException>(() => com1.ReadTo(endString));

                if (2 * charXmitBuffer.Length != com1.BytesToRead)
                {
                    Fail("Err_0585haieidp Expected BytesToRead: {0} actual: {1}", 2 * charXmitBuffer.Length, com1.BytesToRead);
                }

                com2.Write(endString);
                result = com1.ReadTo(endString);
                char[] charRcvBuffer = result.ToCharArray();

                Assert.Equal(charRcvBuffer, charXmitBuffer);

                VerifyReadTo(com1, com2, new string(charXmitBuffer), endString);
            }
    }
Exemple #3
0
        public void Read_LargeBuffer()
        {
            using (SerialPort com1 = TCSupport.InitFirstSerialPort())
                using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1))
                {
                    char[] charXmitBuffer = TCSupport.GetRandomChars(512, TCSupport.CharacterOptions.None);

                    bool continueRunning    = true;
                    int  numberOfIterations = 0;
                    var  writeToCom2Task    = new Task(delegate()
                    {
                        while (continueRunning)
                        {
                            com1.Write(charXmitBuffer, 0, charXmitBuffer.Length);
                            ++numberOfIterations;
                        }
                    });

                    char endChar    = TCSupport.GenerateRandomCharNonSurrogate();
                    char notEndChar = TCSupport.GetRandomOtherChar(endChar, TCSupport.CharacterOptions.None);

                    //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.BaudRate = 115200;
                    com2.BaudRate = 115200;

                    com1.Encoding = Encoding.Unicode;
                    com2.Encoding = Encoding.Unicode;

                    com2.ReadTimeout = 10000;

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

                    writeToCom2Task.Start();

                    Assert.Throws <TimeoutException>(() => com2.ReadTo(new string(endChar, 1)));

                    continueRunning = false;
                    writeToCom2Task.Wait();

                    com1.Write(new string(endChar, 1));

                    string stringRcvBuffer = com2.ReadTo(new string(endChar, 1));

                    if (charXmitBuffer.Length * numberOfIterations == stringRcvBuffer.Length)
                    {
                        for (int i = 0; i < charXmitBuffer.Length * numberOfIterations; ++i)
                        {
                            if (stringRcvBuffer[i] != charXmitBuffer[i % charXmitBuffer.Length])
                            {
                                Fail("Err_292aneid Expected to read {0} actually read {1}",
                                     charXmitBuffer[i % charXmitBuffer.Length], stringRcvBuffer[i]);
                                break;
                            }
                        }
                    }
                    else
                    {
                        Fail("Err_292haie Expected to read {0} characters actually read {1}", charXmitBuffer.Length * numberOfIterations, stringRcvBuffer.Length);
                    }
                }
        }
Exemple #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);
                }
        }
Exemple #5
0
    public bool Read_LargeBuffer()
    {
        SerialPort com1 = TCSupport.InitFirstSerialPort();
        SerialPort com2 = TCSupport.InitSecondSerialPort(com1);

        char[] charXmitBuffer = TCSupport.GetRandomChars(512, TCSupport.CharacterOptions.None);
        string stringRcvBuffer;
        bool   retValue = true;

        bool continueRunning    = true;
        int  numberOfIterations = 0;

        System.Threading.Thread writeToCom2Thread = new System.Threading.Thread(delegate()
        {
            while (continueRunning)
            {
                com1.Write(charXmitBuffer, 0, charXmitBuffer.Length);
                ++numberOfIterations;
            }
        });

        char endChar    = TCSupport.GenerateRandomCharNonSurrogate();
        char notEndChar = TCSupport.GetRandomOtherChar(endChar, TCSupport.CharacterOptions.None);

        //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.BaudRate = 115200;
        com2.BaudRate = 115200;

        com1.Encoding = System.Text.Encoding.Unicode;
        com2.Encoding = System.Text.Encoding.Unicode;

        com2.ReadTimeout = 10000;

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

        writeToCom2Thread.Start();

        try
        {
            com2.ReadTo(new String(endChar, 1));
            retValue = false;
            Console.WriteLine("Err_2928aneieud Expected ReadLine() to throw timeoutException()");
        }
        catch (TimeoutException) { };

        continueRunning = false;
        writeToCom2Thread.Join();

        com1.Write(new string(endChar, 1));

        stringRcvBuffer = com2.ReadTo(new String(endChar, 1));

        if (charXmitBuffer.Length * numberOfIterations == stringRcvBuffer.Length)
        {
            for (int i = 0; i < charXmitBuffer.Length * numberOfIterations; ++i)
            {
                if (stringRcvBuffer[i] != charXmitBuffer[i % charXmitBuffer.Length])
                {
                    retValue = false;
                    Console.WriteLine("Err_292aneid Expected to read {0} actually read {1}", charXmitBuffer[i % charXmitBuffer.Length], stringRcvBuffer[i]);
                    break;
                }
            }
        }
        else
        {
            retValue = false;
            Console.WriteLine("Err_292haie Expected to read {0} characters actually read {1}", charXmitBuffer.Length * numberOfIterations, stringRcvBuffer.Length);
        }

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

        return(retValue);
    }
Exemple #6
0
    public bool Read_Timeout()
    {
        SerialPort com1 = TCSupport.InitFirstSerialPort();
        SerialPort com2 = TCSupport.InitSecondSerialPort(com1);

        char[] charXmitBuffer = TCSupport.GetRandomChars(512, TCSupport.CharacterOptions.None);
        char[] charRcvBuffer;
        string endString  = "END";
        bool   retValue   = true;
        char   endChar    = endString[0];
        char   notEndChar = TCSupport.GetRandomOtherChar(endChar, TCSupport.CharacterOptions.None);
        string result;

        Console.WriteLine("Verifying that ReadTo(string) works appropriately after TimeoutException has been thrown");

        //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    = System.Text.Encoding.Unicode;
        com2.Encoding    = System.Text.Encoding.Unicode;
        com1.ReadTimeout = 500; // 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();
        }

        com2.Write(charXmitBuffer, 0, charXmitBuffer.Length);

        try
        {
            com1.ReadTo(endString);
            Console.WriteLine("Err_29299aize Expected ReadTo to throw TimeoutException");
            retValue = false;
        }
        catch (TimeoutException) { }//Expected

        if (2 * charXmitBuffer.Length != com1.BytesToRead)
        {
            Console.WriteLine("Err_0585haieidp Expected BytesToRead: {0} actual: {1}", 2 * charXmitBuffer.Length, com1.BytesToRead);
            retValue = false;
        }

        com2.Write(endString);
        result        = com1.ReadTo(endString);
        charRcvBuffer = 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_8988auzobn Characters differ at {0} expected:{1}({2:X}) actual:{3}({4:X})",
                                      i, charXmitBuffer[i], (int)charXmitBuffer[i], charRcvBuffer[i], (int)charRcvBuffer[i]);
                }
            }
        }

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

        if (!retValue)
        {
            Console.WriteLine("Err_05498352aiiueid Verifying that ReadTo(string) works appropriately after TimeoutException has been thrown failed");
        }

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

        return(retValue);
    }
Exemple #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);
    }