Пример #1
0
        public void GreedyRead()
        {
            using (SerialPort com1 = TCSupport.InitFirstSerialPort())
                using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1))
                {
                    byte[] byteXmitBuffer = new byte[1024];
                    char   utf32Char      = TCSupport.GenerateRandomCharNonSurrogate();
                    byte[] utf32CharBytes = System.Text.Encoding.UTF32.GetBytes(new[] { utf32Char });
                    int    charRead;

                    Debug.WriteLine("Verifying that ReadChar() will read everything from internal buffer and drivers buffer");

                    //Put the first byte of the utf32 encoder char in the last byte of this buffer
                    //when we read this later the buffer will have to be resized
                    byteXmitBuffer[byteXmitBuffer.Length - 1] = utf32CharBytes[0];

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

                    while (com1.BytesToRead < byteXmitBuffer.Length)
                    {
                        System.Threading.Thread.Sleep(50);
                    }

                    //Read Every Byte except the last one. The last bye should be left in the last position of SerialPort's
                    //internal buffer. When we try to read this char as UTF32 the buffer should have to be resized so
                    //the other 3 bytes of the ut32 encoded char can be in the buffer
                    com1.Read(new char[1023], 0, 1023);

                    Assert.Equal(1, com1.BytesToRead);

                    com1.Encoding = System.Text.Encoding.UTF32;
                    com2.Write(utf32CharBytes, 1, 3);

                    while (com1.BytesToRead < 4)
                    {
                        System.Threading.Thread.Sleep(50);
                    }

                    if (utf32Char != (charRead = com1.ReadChar()))
                    {
                        Fail("Err_6481sfadw ReadChar() returned={0} expected={1}", charRead, utf32Char);
                    }

                    Assert.Equal(0, com1.BytesToRead);
                }
        }
Пример #2
0
        private void BufferData(SerialPort com1, SerialPort com2, byte[] bytesToWrite)
        {
            char c = TCSupport.GenerateRandomCharNonSurrogate();

            byte[] bytesForSingleChar = com1.Encoding.GetBytes(new char[] { c }, 0, 1);

            com2.Write(bytesForSingleChar, 0, bytesForSingleChar.Length); // Write one byte at the beginning because we are going to read this to buffer the rest of the data
            com2.Write(bytesToWrite, 0, bytesToWrite.Length);

            TCSupport.WaitForReadBufferToLoad(com1, bytesToWrite.Length);

            com1.Read(new char[1], 0, 1); // This should put the rest of the bytes in SerialPorts own internal buffer

            Assert.Equal(bytesToWrite.Length, com1.BytesToRead);
        }
Пример #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);
                    }
                }
        }
Пример #4
0
        public void GreedyRead()
        {
            using (SerialPort com1 = TCSupport.InitFirstSerialPort())
                using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1))
                {
                    char[] charXmitBuffer = TCSupport.GetRandomChars(128, TCSupport.CharacterOptions.Surrogates);
                    byte[] byteXmitBuffer = new byte[1024];
                    char   utf32Char      = TCSupport.GenerateRandomCharNonSurrogate();
                    byte[] utf32CharBytes = Encoding.UTF32.GetBytes(new[] { utf32Char });
                    int    numBytes;

                    Debug.WriteLine("Verifying that ReadTo() will read everything from internal buffer and drivers buffer");

                    //Put the first byte of the utf32 encoder char in the last byte of this buffer
                    //when we read this later the buffer will have to be resized
                    byteXmitBuffer[byteXmitBuffer.Length - 1] = utf32CharBytes[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();
                    }

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

                    TCSupport.WaitForReadBufferToLoad(com1, byteXmitBuffer.Length);

                    //Read Every Byte except the last one. The last bye should be left in the last position of SerialPort's
                    //internal buffer. When we try to read this char as UTF32 the buffer should have to be resized so
                    //the other 3 bytes of the ut32 encoded char can be in the buffer
                    com1.Read(new char[1023], 0, 1023);

                    Assert.Equal(1, com1.BytesToRead);

                    com1.Encoding = Encoding.UTF32;
                    com2.Encoding = Encoding.UTF32;

                    com2.Write(utf32CharBytes, 1, 3);
                    com2.Write(charXmitBuffer, 0, charXmitBuffer.Length);
                    com2.WriteLine(string.Empty);

                    numBytes = Encoding.UTF32.GetByteCount(charXmitBuffer);

                    byte[] byteBuffer = Encoding.UTF32.GetBytes(charXmitBuffer);

                    char[] expectedChars = new char[1 + Encoding.UTF32.GetCharCount(byteBuffer)];
                    expectedChars[0] = utf32Char;

                    Encoding.UTF32.GetChars(byteBuffer, 0, byteBuffer.Length, expectedChars, 1);

                    TCSupport.WaitForReadBufferToLoad(com1, 4 + numBytes);

                    string rcvString = com1.ReadTo(com2.NewLine);
                    Assert.NotNull(rcvString);

                    Assert.Equal(expectedChars, rcvString.ToCharArray());

                    Assert.Equal(0, com1.BytesToRead);
                }
        }
Пример #5
0
    public bool GreedyRead()
    {
        SerialPort com1 = TCSupport.InitFirstSerialPort();
        SerialPort com2 = TCSupport.InitSecondSerialPort(com1);

        byte[] byteXmitBuffer = new byte[1024];
        char   utf32Char      = TCSupport.GenerateRandomCharNonSurrogate();

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

        Console.WriteLine("Verifying that ReadChar() will read everything from internal buffer and drivers buffer");

        //Put the first byte of the utf32 encoder char in the last byte of this buffer
        //when we read this later the buffer will have to be resized
        byteXmitBuffer[byteXmitBuffer.Length - 1] = utf32CharBytes[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();
        }

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

        while (com1.BytesToRead < byteXmitBuffer.Length)
        {
            System.Threading.Thread.Sleep(50);
        }

        //Read Every Byte except the last one. The last bye should be left in the last position of SerialPort's
        //internal buffer. When we try to read this char as UTF32 the buffer should have to be resized so
        //the other 3 bytes of the ut32 encoded char can be in the buffer
        com1.Read(new char[1023], 0, 1023);

        if (1 != com1.BytesToRead)
        {
            Console.WriteLine("Err_9416sapz ExpectedByteToRead={0} actual={1}", 1, com1.BytesToRead);
            retValue = false;
        }

        com1.Encoding = System.Text.Encoding.UTF32;
        com2.Write(utf32CharBytes, 1, 3);

        while (com1.BytesToRead < 4)
        {
            System.Threading.Thread.Sleep(50);
        }

        if (utf32Char != (charRead = com1.ReadChar()))
        {
            Console.WriteLine("Err_6481sfadw ReadChar() returned={0} expected={1}", charRead, utf32Char);
            retValue = false;
        }

        if (0 != com1.BytesToRead)
        {
            Console.WriteLine("Err_78028asdf ExpectedByteToRead={0} actual={1}", 0, com1.BytesToRead);
            retValue = false;
        }

        if (!retValue)
        {
            Console.WriteLine("Err_1389 Verifying that ReadChar() will read everything from internal buffer and drivers buffer failed");
        }

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

        return(retValue);
    }
Пример #6
0
    public bool GreedyRead()
    {
        SerialPort com1   = TCSupport.InitFirstSerialPort();
        SerialPort com2   = TCSupport.InitSecondSerialPort(com1);
        Random     rndGen = new Random(-55);

        char[] charXmitBuffer = TCSupport.GetRandomChars(128, true);
        byte[] byteXmitBuffer = new byte[1024];
        char[] expectedChars;
        string rcvString;

        char[] actualChars;
        char   utf32Char = TCSupport.GenerateRandomCharNonSurrogate();

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

        Console.WriteLine("Verifying that ReadExisting() will read everything from internal buffer and drivers buffer");

        //Put the first byte of the utf32 encoder char in the last byte of this buffer
        //when we read this later the buffer will have to be resized
        byteXmitBuffer[byteXmitBuffer.Length - 1] = utf32CharBytes[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();
        }

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

        while (com1.BytesToRead < byteXmitBuffer.Length)
        {
            System.Threading.Thread.Sleep(50);
        }

        //Read Every Byte except the last one. The last bye should be left in the last position of SerialPort's
        //internal buffer. When we try to read this char as UTF32 the buffer should have to be resized so
        //the other 3 bytes of the ut32 encoded char can be in the buffer
        com1.Read(new char[1023], 0, 1023);
        if (1 != com1.BytesToRead)
        {
            Console.WriteLine("Err_9416sapz ExpectedByteToRead={0} actual={1}", 1, com1.BytesToRead);
            retValue = false;
        }

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

        com2.Write(utf32CharBytes, 1, 3);
        com2.Write(charXmitBuffer, 0, charXmitBuffer.Length);

        numBytes = System.Text.Encoding.UTF32.GetByteCount(charXmitBuffer);

        byte[] byteBuffer = System.Text.Encoding.UTF32.GetBytes(charXmitBuffer);

        expectedChars    = new char[1 + System.Text.Encoding.UTF32.GetCharCount(byteBuffer)];
        expectedChars[0] = utf32Char;

        System.Text.Encoding.UTF32.GetChars(byteBuffer, 0, byteBuffer.Length, expectedChars, 1);

        while (com1.BytesToRead < 4 + numBytes)
        {
            System.Threading.Thread.Sleep(50);
        }

        if (null == (rcvString = com1.ReadExisting()))
        {
            Console.WriteLine("Err_6481sfadw ReadExisting returned null");
            retValue = false;
        }
        else
        {
            actualChars = rcvString.ToCharArray();

            if (actualChars.Length != expectedChars.Length)
            {
                Console.WriteLine("Err_0872watr Expected to read {0} chars actually read {1} chars", expectedChars.Length, actualChars.Length);
                retValue = false;
            }
            else
            {
                for (int i = 0; i < expectedChars.Length; i++)
                {
                    if (expectedChars[i] != actualChars[i])
                    {
                        Console.WriteLine("Err_70782apzh Expected to read {0} actually read {1}", (int)expectedChars[i], (int)actualChars[i]);
                        retValue = false;
                        break;
                    }
                }
            }
        }

        if (0 != com1.BytesToRead)
        {
            Console.WriteLine("Err_78028asdf ExpectedByteToRead={0} actual={1}", 0, com1.BytesToRead);
            retValue = false;
        }

        if (!retValue)
        {
            Console.WriteLine("Err_1389 Verifying that ReadExisting() will read everything from internal buffer and drivers buffer failed");
        }

        com1.Close();
        com2.Close();
        return(retValue);
    }
Пример #7
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);
    }