Пример #1
0
        private void VerifyReadToWithWriteLine(Encoding encoding, string newLine)
        {
            using (SerialPort com1 = TCSupport.InitFirstSerialPort())
                using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1))
                {
                    Random        rndGen = new Random(-55);
                    StringBuilder strBldrToWrite;
                    string        strExpected;
                    bool          isUTF7Encoding = encoding.EncodingName == Encoding.UTF7.EncodingName;

                    Debug.WriteLine("Verifying ReadTo with WriteLine encoding={0}, newLine={1}", encoding, newLine);

                    com1.ReadTimeout = 500;
                    com2.NewLine     = newLine;
                    com1.Encoding    = encoding;
                    com2.Encoding    = encoding;

                    //Generate random characters
                    do
                    {
                        strBldrToWrite = new StringBuilder();
                        for (int i = 0; i < DEFAULT_NUM_CHARS_TO_READ; i++)
                        {
                            strBldrToWrite.Append((char)rndGen.Next(0, 128));
                        }
                    } while (-1 != TCSupport.OrdinalIndexOf(strBldrToWrite.ToString(), newLine));
                    //SerialPort does a Ordinal comparison

                    string strWrite = strBldrToWrite.ToString();
                    strExpected = new string(com1.Encoding.GetChars(com1.Encoding.GetBytes(strWrite.ToCharArray())));

                    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.WriteLine(strBldrToWrite.ToString());
                    string strRead = com1.ReadTo(newLine);

                    if (0 != strBldrToWrite.ToString().CompareTo(strRead))
                    {
                        Fail("ERROR!!! The string written: \"{0}\" and the string read \"{1}\" differ", strBldrToWrite, strRead);
                    }

                    if (0 != com1.BytesToRead && (!isUTF7Encoding || 1 != com1.BytesToRead))
                    {
                        Fail("ERROR!!! BytesToRead={0} expected 0", com1.BytesToRead);
                    }
                }
        }
Пример #2
0
        private void VerifyReadLineWithWriteLine(Encoding encoding, string newLine)
        {
            using (SerialPort com1 = TCSupport.InitFirstSerialPort())
                using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1))
                {
                    var           rndGen = new Random(-55);
                    StringBuilder strBldrToWrite;

                    Debug.WriteLine("Verifying ReadLine with WriteLine encoding={0}, newLine={1}", encoding, newLine);

                    com1.ReadTimeout = 500;
                    com1.NewLine     = newLine;
                    com2.NewLine     = newLine;
                    com1.Encoding    = encoding;
                    com2.Encoding    = encoding;

                    //Genrate random characters
                    do
                    {
                        strBldrToWrite = new StringBuilder();
                        for (var i = 0; i < DEFAULT_NUM_CHARS_TO_READ; i++)
                        {
                            strBldrToWrite.Append((char)rndGen.Next(0, 128));
                        }
                    } while (-1 != TCSupport.OrdinalIndexOf(strBldrToWrite.ToString(), newLine));
                    //SerialPort does a Ordinal comparison

                    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.WriteLine(strBldrToWrite.ToString());
                    string strRead = com1.ReadLine();

                    Assert.Equal(strBldrToWrite.ToString(), strRead);

                    Assert.Equal(0, com1.BytesToRead);
                }
        }
Пример #3
0
        private void PerformReadOnCom1FromCom2(SerialPort com1, SerialPort com2, string strToWrite, string newLine)
        {
            StringBuilder strBldrRead         = new StringBuilder();
            int           newLineStringLength = newLine.Length;
            int           numNewLineChars     = newLine.ToCharArray().Length;
            int           numNewLineBytes     = com1.Encoding.GetByteCount(newLine.ToCharArray());
            int           totalBytesRead;
            int           totalCharsRead;
            int           lastIndexOfNewLine = -newLineStringLength;
            string        expectedString;
            bool          isUTF7Encoding = com1.Encoding.EncodingName == Encoding.UTF7.EncodingName;

            char[] charsToWrite = strToWrite.ToCharArray();
            byte[] bytesToWrite = com1.Encoding.GetBytes(charsToWrite);
            expectedString = new string(com1.Encoding.GetChars(bytesToWrite));

            totalBytesRead = 0;
            totalCharsRead = 0;

            while (true)
            {
                string rcvString;
                try
                {
                    rcvString = com1.ReadTo(newLine);
                }
                catch (TimeoutException)
                {
                    break;
                }

                //While their are more characters to be read
                char[] rcvCharBuffer = rcvString.ToCharArray();
                int    charsRead     = rcvCharBuffer.Length;

                if (isUTF7Encoding)
                {
                    totalBytesRead = GetUTF7EncodingBytes(charsToWrite, 0, totalCharsRead + charsRead + numNewLineChars);
                }
                else
                {
                    int bytesRead = com1.Encoding.GetByteCount(rcvCharBuffer, 0, charsRead);
                    totalBytesRead += bytesRead + numNewLineBytes;
                }

                //			indexOfNewLine = strToWrite.IndexOf(newLine, lastIndexOfNewLine + newLineStringLength);
                int indexOfNewLine = TCSupport.OrdinalIndexOf(expectedString, lastIndexOfNewLine + newLineStringLength, newLine);


                if ((indexOfNewLine - (lastIndexOfNewLine + newLineStringLength)) != charsRead)
                {
                    //If we have not read all of the characters that we should have
                    Debug.WriteLine("indexOfNewLine={0} lastIndexOfNewLine={1} charsRead={2} numNewLineChars={3} newLineStringLength={4} strToWrite.Length={5}",
                                    indexOfNewLine, lastIndexOfNewLine, charsRead, numNewLineChars, newLineStringLength, strToWrite.Length);
                    Debug.WriteLine(strToWrite);
                    Fail("Err_1707ahsp!!!: Read did not return all of the characters that were in SerialPort buffer");
                }

                if (charsToWrite.Length < totalCharsRead + charsRead)
                {
                    //If we have read in more characters then we expect
                    Fail("Err_21707adad!!!: We have received more characters then were sent");
                }

                strBldrRead.Append(rcvString);
                strBldrRead.Append(newLine);

                totalCharsRead += charsRead + numNewLineChars;

                lastIndexOfNewLine = indexOfNewLine;

                if (bytesToWrite.Length - totalBytesRead != com1.BytesToRead)
                {
                    Fail("Err_99087ahpbx!!!: Expected BytesToRead={0} actual={1}", bytesToWrite.Length - totalBytesRead, com1.BytesToRead);
                }
            }//End while there are more characters to read

            if (0 != com1.BytesToRead)
            {
                //If there are more bytes to read but there must not be a new line char at the end
                int charRead;

                while (true)
                {
                    try
                    {
                        charRead = com1.ReadChar();
                        strBldrRead.Append((char)charRead);
                    }
                    catch (TimeoutException) { break; }
                }
            }

            if (0 != com1.BytesToRead && (!isUTF7Encoding || 1 != com1.BytesToRead))
            {
                Fail("Err_558596ahbpa!!!: BytesToRead is not zero");
            }

            if (0 != expectedString.CompareTo(strBldrRead.ToString()))
            {
                Fail("Err_7797ajpba!!!: Expected to read \"{0}\"  actual read  \"{1}\"", expectedString, strBldrRead.ToString());
            }

            /*
             *      if (!retValue)
             *      {
             *          Debug.WriteLine("\nstrToWrite = ");
             *          TCSupport.PrintChars(strToWrite.ToCharArray());
             *
             *          Debug.WriteLine("\nnewLine = ");
             *          TCSupport.PrintChars(newLine.ToCharArray());
             *      }
             */
        }
Пример #4
0
        private void PerformReadOnCom1FromCom2(SerialPort com1, SerialPort com2, string strToWrite, string newLine)
        {
            var strBldrRead = new StringBuilder();
            int newLineStringLength = newLine.Length;
            int numNewLineChars = newLine.ToCharArray().Length;
            int numNewLineBytes = com1.Encoding.GetByteCount(newLine.ToCharArray());
            int bytesRead, totalBytesRead, charsRead, totalCharsRead;
            int indexOfNewLine, lastIndexOfNewLine = -newLineStringLength;

            char[] charsToWrite   = strToWrite.ToCharArray();
            byte[] bytesToWrite   = com1.Encoding.GetBytes(charsToWrite);
            var    expectedString = new string(com1.Encoding.GetChars(bytesToWrite));

            totalBytesRead = 0;
            totalCharsRead = 0;

            while (true)
            {
                string rcvString;
                try
                {
                    rcvString = com1.ReadLine();
                }
                catch (TimeoutException)
                {
                    break;
                }

                //While their are more characters to be read
                char[] rcvCharBuffer = rcvString.ToCharArray();
                charsRead = rcvCharBuffer.Length;

                bytesRead       = com1.Encoding.GetByteCount(rcvCharBuffer, 0, charsRead);
                totalBytesRead += bytesRead + numNewLineBytes;

                //			indexOfNewLine = strToWrite.IndexOf(com1.NewLine, lastIndexOfNewLine + newLineStringLength);
                indexOfNewLine = TCSupport.OrdinalIndexOf(expectedString, lastIndexOfNewLine + newLineStringLength, newLine);
                //SerialPort does a Ordinal comparison

                if ((indexOfNewLine - (lastIndexOfNewLine + newLineStringLength)) != charsRead)
                {
                    //If we have not read all of the characters that we should have
                    Debug.WriteLine(
                        "indexOfNewLine={0} lastIndexOfNewLine={1} charsRead={2} numNewLineChars={3} newLineStringLength={4} strToWrite.Length={5}",
                        indexOfNewLine, lastIndexOfNewLine, charsRead, numNewLineChars, newLineStringLength,
                        strToWrite.Length);
                    Debug.WriteLine(strToWrite);
                    Fail("Err_1707ahsp!!!: Read did not return all of the characters that were in SerialPort buffer");
                }

                if (charsToWrite.Length < totalCharsRead + charsRead)
                {
                    //If we have read in more characters then we expect
                    Fail("Err_21707adad!!!: We have received more characters then were sent");
                }

                strBldrRead.Append(rcvString);
                strBldrRead.Append(newLine);

                totalCharsRead += charsRead + numNewLineChars;

                lastIndexOfNewLine = indexOfNewLine;

                if (bytesToWrite.Length - totalBytesRead != com1.BytesToRead)
                {
                    Fail("Err_99087ahpbx!!!: Expected BytesToRead={0} actual={1}",
                         bytesToWrite.Length - totalBytesRead, com1.BytesToRead);
                }
            } //End while there are more characters to read

            if (0 != com1.BytesToRead)
            {
                //If there are more bytes to read but there must not be a new line char at the end

                while (true)
                {
                    try
                    {
                        int charRead = com1.ReadChar();
                        strBldrRead.Append((char)charRead);
                    }
                    catch (TimeoutException)
                    {
                        break;
                    }
                }
            }

            Assert.Equal(0, com1.BytesToRead);

            Assert.Equal(expectedString, strBldrRead.ToString());
        }
Пример #5
0
    private bool PerformReadOnCom1FromCom2(SerialPort com1, SerialPort com2, string strToWrite, string newLine)
    {
        bool retValue = true;

        char[] charsToWrite;
        byte[] bytesToWrite;
        string rcvString;

        System.Text.StringBuilder strBldrRead = new System.Text.StringBuilder();
        char[] rcvCharBuffer;
        int    newLineStringLength = newLine.Length;
        int    numNewLineChars = newLine.ToCharArray().Length;
        int    numNewLineBytes = com1.Encoding.GetByteCount(newLine.ToCharArray());
        int    bytesRead, totalBytesRead, charsRead, totalCharsRead;
        int    indexOfNewLine, lastIndexOfNewLine = -newLineStringLength;
        string expectedString;
        bool   isUTF7Encoding = com1.Encoding.EncodingName == System.Text.Encoding.UTF7.EncodingName;

        charsToWrite   = strToWrite.ToCharArray();
        bytesToWrite   = com1.Encoding.GetBytes(charsToWrite);
        expectedString = new string(com1.Encoding.GetChars(bytesToWrite));

        totalBytesRead = 0;
        totalCharsRead = 0;

        while (true)
        {
            try
            {
                rcvString = com1.ReadLine();
            }
            catch (TimeoutException)
            {
                break;
            }

            //While their are more characters to be read
            rcvCharBuffer = rcvString.ToCharArray();
            charsRead     = rcvCharBuffer.Length;

            if (isUTF7Encoding)
            {
                totalBytesRead = GetUTF7EncodingBytes(charsToWrite, 0, totalCharsRead + charsRead + numNewLineChars);
            }
            else
            {
                bytesRead       = com1.Encoding.GetByteCount(rcvCharBuffer, 0, charsRead);
                totalBytesRead += bytesRead + numNewLineBytes;
            }

            //			indexOfNewLine = strToWrite.IndexOf(com1.NewLine, lastIndexOfNewLine + newLineStringLength);
            indexOfNewLine = TCSupport.OrdinalIndexOf(expectedString, lastIndexOfNewLine + newLineStringLength, newLine);//SerialPort does a Ordinal comparison

            if ((indexOfNewLine - (lastIndexOfNewLine + newLineStringLength)) != charsRead)
            {
                //If we have not read all of the characters that we should have
                Console.WriteLine("Err_1707ahsp!!!: Read did not return all of the characters that were in SerialPort buffer");
                Console.WriteLine("indexOfNewLine={0} lastIndexOfNewLine={1} charsRead={2} numNewLineChars={3} newLineStringLength={4} strToWrite.Length={5}",
                                  indexOfNewLine, lastIndexOfNewLine, charsRead, numNewLineChars, newLineStringLength, strToWrite.Length);
                Console.WriteLine(strToWrite);
                retValue = false;
            }

            if (charsToWrite.Length < totalCharsRead + charsRead)
            {
                //If we have read in more characters then we expect
                Console.WriteLine("Err_21707adad!!!: We have received more characters then were sent");
                retValue = false;
                break;
            }

            strBldrRead.Append(rcvString);
            strBldrRead.Append(newLine);

            totalCharsRead += charsRead + numNewLineChars;

            lastIndexOfNewLine = indexOfNewLine;

            if (bytesToWrite.Length - totalBytesRead != com1.BytesToRead)
            {
                System.Console.WriteLine("Err_99087ahpbx!!!: Expected BytesToRead={0} actual={1}", bytesToWrite.Length - totalBytesRead, com1.BytesToRead);
                retValue = false;
            }
        }//End while there are more characters to read

        if (0 != com1.BytesToRead)
        {
            //If there are more bytes to read but there must not be a new line char at the end
            int charRead;

            while (true)
            {
                try
                {
                    charRead = com1.ReadChar();
                    strBldrRead.Append((char)charRead);
                }
                catch (TimeoutException) { break; }
                catch (Exception e)
                {
                    Console.WriteLine("Err_15054akjeid!!!: The following exception was thrown while reading remaining chars");
                    Console.WriteLine(e);
                    retValue = false;
                }
            }
        }

        if (0 != com1.BytesToRead && (!isUTF7Encoding || 1 != com1.BytesToRead))
        {
            System.Console.WriteLine("Err_558596ahbpa!!!: BytesToRead is not zero");
            retValue = false;
        }

        if (0 != expectedString.CompareTo(strBldrRead.ToString()))
        {
            System.Console.WriteLine("Err_7797ajpba!!!: Expected to read \"{0}\"  actual read  \"{1}\"", expectedString, strBldrRead.ToString());
            retValue = false;
        }

        if (!retValue)
        {
            Console.WriteLine("\nstrToWrite = ");
            TCSupport.PrintChars(strToWrite.ToCharArray());

            Console.WriteLine("\nnewLine = ");
            TCSupport.PrintChars(newLine.ToCharArray());
        }

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

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

        return(retValue);
    }