Exemple #1
0
        private void PerformReadOnCom1FromCom2(SerialPort com1, SerialPort com2, char[] expectedChars, char[] rcvBuffer, int offset, int count)
        {
            char[] buffer          = new char[expectedChars.Length];
            char[] oldRcvBuffer    = (char[])rcvBuffer.Clone();
            int    numBytesWritten = com1.Encoding.GetByteCount(expectedChars);

            int totalBytesRead = 0;
            int totalCharsRead = 0;

            while (true)
            {
                int charsRead;
                try
                {
                    charsRead = com1.Read(rcvBuffer, offset, count);
                }
                catch (TimeoutException)
                {
                    break;
                }

                // While there are more characters to be read
                int bytesRead = com1.Encoding.GetByteCount(rcvBuffer, offset, charsRead);
                totalBytesRead += bytesRead;

                if (expectedChars.Length < totalCharsRead + charsRead)
                {
                    //If we have read in more characters than we expect

                    //1<DEBUG>
                    Debug.WriteLine("count={0}, charsRead={1} expectedChars.Length={2}, totalCharsRead={3}", count, charsRead, expectedChars.Length, totalCharsRead);

                    Debug.WriteLine("rcvBuffer");
                    TCSupport.PrintChars(rcvBuffer);

                    Debug.WriteLine("\nexpectedChars");
                    TCSupport.PrintChars(expectedChars);
                    //1</DEBUG>

                    Fail("ERROR!!!: We have received more characters then were sent");
                }

                if (count != charsRead &&
                    (count < charsRead ||
                     ((expectedChars.Length - totalCharsRead) != charsRead &&
                      !TCSupport.IsSurrogate(expectedChars[totalCharsRead + charsRead]))))
                {
                    //If we have not read all of the characters that we should have

                    //1<DEBUG>
                    Debug.WriteLine("count={0}, charsRead={1} expectedChars.Length={2}, totalCharsRead={3}", count, charsRead, expectedChars.Length, totalCharsRead);

                    Debug.WriteLine("rcvBuffer");
                    TCSupport.PrintChars(rcvBuffer);

                    Debug.WriteLine("\nexpectedChars");
                    TCSupport.PrintChars(expectedChars);
                    //1</DEBUG>

                    Fail("ERROR!!!: Read did not return all of the characters that were in SerialPort buffer");
                }

                VerifyBuffer(rcvBuffer, oldRcvBuffer, offset, charsRead);

                Array.Copy(rcvBuffer, offset, buffer, totalCharsRead, charsRead);

                totalCharsRead += charsRead;

                Assert.Equal(numBytesWritten - totalBytesRead, com1.BytesToRead);

                oldRcvBuffer = (char[])rcvBuffer.Clone();
            }

            VerifyBuffer(rcvBuffer, oldRcvBuffer, 0, rcvBuffer.Length);

            //Compare the chars that were written with the ones we expected to read
            for (int i = 0; i < expectedChars.Length; i++)
            {
                if (expectedChars[i] != buffer[i])
                {
                    Fail("ERROR!!!: Expected to read {0}  actual read  {1} at {2}", (int)expectedChars[i], (int)buffer[i], i);
                }
            }

            Assert.Equal(0, com1.BytesToRead);
        }
Exemple #2
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.ReadTo(newLine);
            }
            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(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);
    }