Exemple #1
0
    void VerifyReadExisting(System.Text.Encoding encoding)
    {
        // TODO - this test text did not come across from legacy properly.
        string text = "????????????4??????????????????,?11????????????????????????????????????????????????,????????????,??????????";

        using (s_com1 = TCSupport.InitFirstSerialPort())
            using (s_com2 = TCSupport.InitSecondSerialPort(s_com1))
            {
                s_com1.ReadTimeout = 500;
                s_com1.Encoding    = encoding;
                s_com2.Encoding    = encoding;

                s_com1.Open();

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

                s_com2.DataReceived += com2_DataReceived;
                s_com1.Write(text);

                //3 seconds is more than enough time to write a few bytes to the other port
                TCSupport.WaitForPredicate(() => s_readComplete, 3000,
                                           "ReadExisting did not complete in a timely fashion.  Timeout");

                Assert.Equal(text, s_receivedstr);
            }
    }
Exemple #2
0
    public void ReadTimeout_Zero_Bytes()
    {
        using (SerialPort com1 = TCSupport.InitFirstSerialPort())
            using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1))
            {
                char   utf32Char      = (char)0x254b; //Box drawing char
                byte[] utf32CharBytes = System.Text.Encoding.UTF32.GetBytes(new[] { utf32Char });

                int readChar;

                Debug.WriteLine("Verifying Read method with zero timeout that resizes SerialPort's buffer");

                com1.Encoding    = System.Text.Encoding.UTF32;
                com1.ReadTimeout = 0;

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

                //[] Try ReadChar with no bytes available
                Assert.Throws <TimeoutException>(() => com1.ReadChar());

                Assert.Equal(0, com1.BytesToRead);

                //[] Try ReadChar with 1 byte available
                com2.Write(utf32CharBytes, 0, 1);

                TCSupport.WaitForPredicate(() => com1.BytesToRead == 1, 2000,
                                           "Err_28292aheid Expected BytesToRead to be 1");

                Assert.Throws <TimeoutException>(() => com1.ReadChar());

                Assert.Equal(1, com1.BytesToRead);

                //[] Try ReadChar with the bytes in the buffer and in available in the SerialPort
                com2.Write(utf32CharBytes, 1, 3);

                TCSupport.WaitForPredicate(() => com1.BytesToRead == 4, 2000,
                                           "Err_415568haikpas Expected BytesToRead to be 4");

                readChar = com1.ReadChar();

                Assert.Equal(utf32Char, readChar);
            }
    }
        private void VerifyReadExisting(Encoding encoding)
        {
            // TODO - this test text did not come across from legacy properly.
            string     text        = "????????????4??????????????????,?11????????????????????????????????????????????????,????????????,??????????";
            string     receivedstr = "";
            SerialPort com1;
            SerialPort com2;

            using (com1 = TCSupport.InitFirstSerialPort())
                using (com2 = TCSupport.InitSecondSerialPort(com1))
                {
                    com1.ReadTimeout = 500;
                    com1.Encoding    = encoding;
                    com2.Encoding    = encoding;

                    TCSupport.SetHighSpeed(com1, com2);

                    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.DataReceived += (sender, args) =>
                    {
                        receivedstr += com2.ReadExisting();
                    };

                    com1.Write(text);

                    //3 seconds is more than enough time to write a few bytes to the other port
                    TCSupport.WaitForPredicate(() => receivedstr.Length >= text.Length, 3000,
                                               "Data was not returned in a timely fashion.  Timeout");

                    Assert.Equal(text, receivedstr);
                }
        }
Exemple #4
0
        private void VerifyReadExisting(Encoding encoding)
        {
            string text = "Za\u017C\u00F3\u0142\u0107 g\u0119\u015Bl\u0105 ja\u017A\u0144";

            using (SerialPort com1 = TCSupport.InitFirstSerialPort())
                using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1))
                {
                    com1.ReadTimeout = 500;
                    com1.Encoding    = encoding;
                    com2.Encoding    = encoding;

                    TCSupport.SetHighSpeed(com1, com2);

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

                    string receivedstr = "";
                    com2.DataReceived += (sender, args) =>
                    {
                        receivedstr += com2.ReadExisting();
                    };

                    com1.Write(text);

                    //3 seconds is more than enough time to write a few bytes to the other port
                    TCSupport.WaitForPredicate(() => receivedstr.Length >= text.Length, 3000,
                                               "Data was not returned in a timely fashion.  Timeout");

                    Assert.Equal(text, receivedstr);
                }
        }
    public bool VerifyReadExisting(System.Text.Encoding encoding)
    {
        string text = "????????????4??????????????????,?11????????????????????????????????????????????????,????????????,??????????";

        s_com1             = TCSupport.InitFirstSerialPort();
        s_com2             = TCSupport.InitSecondSerialPort(s_com1);
        s_com1.ReadTimeout = 500;
        s_com1.Encoding    = encoding;
        s_com2.Encoding    = encoding;

        s_com1.Open();

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

        s_com2.DataReceived += com2_DataReceived;
        s_com1.Write(text);

        //3 seconds is more than enough time to write a few bytes to the other port
        TCSupport.WaitForPredicate(delegate() { return(s_readComplete == true); }, 3000, "ReadExisting did not complete in a timely fashion.  Timeout");

        if (String.Compare(s_receivedstr, text) == 0)
        {
            Console.WriteLine("Received and Sent strings are the same");
            return(true);
        }
        else
        {
            Console.WriteLine("Received and Sent strings are different");
            Console.WriteLine("Sent string:" + text);
            Console.WriteLine("Received string:" + s_receivedstr);
            return(false);
        }
    }
Exemple #6
0
        public void WriteChars()
        {
            using (SerialPort com1 = TCSupport.InitFirstSerialPort())
                using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1))
                {
                    char[]        xmitCharBuffer = TCSupport.GetRandomChars(TRANSMIT_BUFFER_SIZE, TCSupport.CharacterOptions.None);
                    char[]        rcvCharBuffer  = new char[RECEIVE_BUFFER_SIZE];
                    Random        random         = new Random(-55);
                    Stopwatch     sw             = new Stopwatch();
                    Buffer <char> buffer         = new Buffer <char>(MAX_BUFFER_SIZE);

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

                    com1.BaudRate = 115200;
                    com2.BaudRate = 115200;

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

                    sw.Start();
                    while (sw.ElapsedMilliseconds < s_testDuration.TotalMilliseconds)
                    {
                        switch (random.Next(0, 2))
                        {
                        case 0: //Write
                            if (com2.BytesToRead < MAX_BUFFER_SIZE)
                            {
                                int maxNumberOfCharactes = (MAX_BUFFER_SIZE - com2.BytesToRead) / 2;
                                int numberOfCharacters   = random.Next(0,
                                                                       Math.Min(xmitCharBuffer.Length, maxNumberOfCharactes) + 1);
                                int expectedBytesToRead = com2.BytesToRead + 2 * numberOfCharacters;

                                //                        Debug.WriteLine("Writing {0,5} characters BytesToRead={1,5}", numberOfCharacters, com2.BytesToRead);
                                com1.Write(xmitCharBuffer, 0, numberOfCharacters);
                                buffer.Append(xmitCharBuffer, 0, numberOfCharacters);

                                TCSupport.WaitForPredicate(delegate() { return(com2.BytesToRead == expectedBytesToRead); },
                                                           60000,
                                                           "Err_29829haie Expected to received {0} bytes actual={1}", expectedBytesToRead,
                                                           com2.BytesToRead);
                            }
                            break;

                        case 1: //Read
                            if (0 < com2.BytesToRead)
                            {
                                int maxNumberOfCharactes = com2.BytesToRead / 2;
                                int numberOfCharacters   = random.Next(0,
                                                                       Math.Min(rcvCharBuffer.Length, maxNumberOfCharactes) + 1);
                                int actualNumberOfCharactersRead;
                                int expectedBytesToRead = com2.BytesToRead - (2 * numberOfCharacters);

                                //                        Debug.WriteLine("Reading {0,5} characters BytesToRead={1,5}", numberOfCharacters, com2.BytesToRead);
                                actualNumberOfCharactersRead = com2.Read(rcvCharBuffer, 0, numberOfCharacters);

                                if (actualNumberOfCharactersRead == numberOfCharacters)
                                {
                                    buffer.CompareAndRemove(rcvCharBuffer, 0, numberOfCharacters);

                                    if (com2.BytesToRead != expectedBytesToRead)
                                    {
                                        Fail("Err_895879uhedbuz Expected to BytesToRead={0} actual={1}",
                                             expectedBytesToRead, com2.BytesToRead);
                                    }
                                }
                                else
                                {
                                    Fail("Err_895879uhedbuz Expected to read {0} chars actual {1}",
                                         numberOfCharacters, actualNumberOfCharactersRead);
                                }
                            }
                            break;
                        }
                    }
                }
        }
Exemple #7
0
        public void Read_ResizeBuffer()
        {
            using (SerialPort com1 = TCSupport.InitFirstSerialPort())
                using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1))
                {
                    char[] charXmitBuffer = TCSupport.GetRandomChars(1023, TCSupport.CharacterOptions.ASCII);
                    int    readResult;

                    Debug.WriteLine("Verifying that Read(char[], int, int) will compact data in the buffer");

                    com1.Encoding = Encoding.ASCII;
                    com2.Encoding = Encoding.ASCII;

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


                    //[] Fill the buffer up then read in all but one of the chars
                    var expectedChars = new char[charXmitBuffer.Length - 1];
                    var charRcvBuffer = new char[charXmitBuffer.Length - 1];
                    Array.Copy(charXmitBuffer, expectedChars, charXmitBuffer.Length - 1);

                    com2.Write(charXmitBuffer, 0, charXmitBuffer.Length);
                    TCSupport.WaitForPredicate(() => com1.BytesToRead == charXmitBuffer.Length, 2000,
                                               "Err_29829haie Expected to received {0} bytes actual={1}", charXmitBuffer.Length, com1.BytesToRead);

                    if (charXmitBuffer.Length - 1 != (readResult = com1.Read(charRcvBuffer, 0, charXmitBuffer.Length - 1)))
                    {
                        Fail("Err_55084aheid Expected to read {0} chars actual {1}", charXmitBuffer.Length - 1,
                             readResult);
                    }

                    TCSupport.VerifyArray(expectedChars, charRcvBuffer);

                    //[] Write 16 more cahrs and read in 16 chars
                    expectedChars    = new char[16];
                    charRcvBuffer    = new char[16];
                    expectedChars[0] = charXmitBuffer[charXmitBuffer.Length - 1];
                    Array.Copy(charXmitBuffer, 0, expectedChars, 1, 15);

                    com2.Write(charXmitBuffer, 0, 16);
                    TCSupport.WaitForPredicate(() => com1.BytesToRead == 17, 2000,
                                               "Err_0516848aied Expected to received {0} bytes actual={1}", 17, com1.BytesToRead);

                    if (16 != (readResult = com1.Read(charRcvBuffer, 0, 16)))
                    {
                        Fail("Err_650848ahide Expected to read {0} chars actual {1}", 16, readResult);
                    }

                    Assert.Equal(expectedChars, charRcvBuffer);

                    //[] Write more chars and read in all of the chars
                    expectedChars    = new char[charXmitBuffer.Length + 1];
                    charRcvBuffer    = new char[charXmitBuffer.Length + 1];
                    expectedChars[0] = charXmitBuffer[15];
                    Array.Copy(charXmitBuffer, 0, expectedChars, 1, charXmitBuffer.Length);

                    com2.Write(charXmitBuffer, 0, charXmitBuffer.Length);
                    TCSupport.WaitForPredicate(() => com1.BytesToRead == charXmitBuffer.Length + 1, 2000,
                                               "Err_41515684 Expected to received {0} bytes actual={1}", charXmitBuffer.Length + 2, com1.BytesToRead);

                    if (charXmitBuffer.Length + 1 != (readResult = com1.Read(charRcvBuffer, 0, charXmitBuffer.Length + 1)))
                    {
                        Fail("Err_460574ajied Expected to read {0} chars actual {1}", charXmitBuffer.Length + 1, readResult);
                    }
                    Assert.Equal(expectedChars, charRcvBuffer);
                }
        }
Exemple #8
0
    public bool ReadTimeout_Zero_Bytes()
    {
        SerialPort com1      = TCSupport.InitFirstSerialPort();
        SerialPort com2      = TCSupport.InitSecondSerialPort(com1);
        char       utf32Char = (char)0x254b; //Box drawing char

        byte[] utf32CharBytes = System.Text.Encoding.UTF32.GetBytes(new char[] { utf32Char });
        bool   retValue       = true;
        int    readChar;

        Console.WriteLine("Verifying Read method with zero timeout that resizes SerialPort's buffer");

        com1.Encoding    = System.Text.Encoding.UTF32;
        com1.ReadTimeout = 0;

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

        //[] Try ReadChar with no bytes available
        try
        {
            try
            {
                readChar = com1.ReadChar();
                Console.WriteLine("Err_28292haied Expected ReadChar to throw TimeoutException with no bytes in " +
                                  "the buffer {0}({0:X}) returned instead", readChar);
                retValue = false;
            }
            catch (TimeoutException)
            {
                //Expected
            }

            if (0 != com1.BytesToRead)
            {
                Console.WriteLine("Err_155anjied Expected BytesToRead={0} actual={1}", 0, com1.BytesToRead);
                retValue = false;
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Err_205566ahuied Unexpected exception thrown: {0}", e);
            retValue = false;
        }

        //[] Try ReadChar with 1 byte available
        try
        {
            com2.Write(utf32CharBytes, 0, 1);

            retValue &= TCSupport.WaitForPredicate(delegate() { return(com1.BytesToRead == 1); }, 2000,
                                                   "Err_28292aheid Expected BytesToRead to be 1");

            try
            {
                readChar = com1.ReadChar();
                Console.WriteLine("Err_9279ajei Expected ReadChar to throw TimeoutException with some bytes available " +
                                  "in the SerialPort {0}({0:X}) returned instead", readChar);
                retValue = false;
            }
            catch (TimeoutException)
            {
                //Expected
            }

            if (1 != com1.BytesToRead)
            {
                Console.WriteLine("Err_1983218aheid Expected BytesToRead={0} actual={1}", 1, com1.BytesToRead);
                retValue = false;
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Err_021855aueid Unexpected exception thrown: {0}", e);
            retValue = false;
        }

        //[] Try ReadChar with the bytes in the buffer and in available in the SerialPort
        try
        {
            com2.Write(utf32CharBytes, 1, 3);

            retValue &= TCSupport.WaitForPredicate(delegate() { return(com1.BytesToRead == 4); }, 2000,
                                                   "Err_415568haikpas Expected BytesToRead to be 4");

            readChar = com1.ReadChar();

            if (readChar != utf32Char)
            {
                retValue = false;
                Console.WriteLine("Err_19173ahid Expected to read {0}({0:X}) actually read {1}({1:X})", utf32Char, readChar);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Err_4929aheidea Unexpected exception thrown: {0}", e);
            retValue = false;
        }

        if (com1.IsOpen)
        {
            com1.Close();
        }

        if (com2.IsOpen)
        {
            com2.Close();
        }

        if (!retValue)
        {
            Console.WriteLine("Err_115858ahjeid Verifying Read method with zero timeout");
        }

        return(retValue);
    }