Exemplo n.º 1
0
        private void VerifyBytesFollowedByChars(System.Text.Encoding encoding)
        {
            using (SerialPort com1 = TCSupport.InitFirstSerialPort())
                using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1))
                {
                    char[] xmitCharBuffer = TCSupport.GetRandomChars(numRndBytesToRead, TCSupport.CharacterOptions.Surrogates);
                    char[] rcvCharBuffer  = new char[xmitCharBuffer.Length];
                    byte[] xmitByteBuffer = new byte[numRndBytesToRead];
                    byte[] rcvByteBuffer  = new byte[xmitByteBuffer.Length];
                    Random rndGen         = new Random(-55);

                    int numRead;

                    Debug.WriteLine("Verifying read method does not alter stream of bytes after chars have been read with {0}",
                                    encoding.GetType());

                    for (int i = 0; i < xmitByteBuffer.Length; i++)
                    {
                        xmitByteBuffer[i] = (byte)rndGen.Next(0, 256);
                    }

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

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

                    System.Threading.Thread.Sleep(
                        (int)
                        (((xmitByteBuffer.Length + com2.Encoding.GetByteCount(xmitCharBuffer) * 10.0) / com1.BaudRate) * 1000) +
                        500);
                    System.Threading.Thread.Sleep(500);

                    if (xmitCharBuffer.Length != (numRead = com1.Read(rcvCharBuffer, 0, rcvCharBuffer.Length)))
                    {
                        Fail("ERROR!!!: Expected to read {0} chars actually read {1}", xmitCharBuffer.Length, numRead);
                    }

                    if (encoding.EncodingName == System.Text.Encoding.UTF7.EncodingName)
                    {
//If UTF7Encoding is being used we we might leave a - in the stream
                        if (com1.BytesToRead == xmitByteBuffer.Length + 1)
                        {
                            int byteRead;

                            if ('-' != (char)(byteRead = com1.ReadByte()))
                            {
                                Fail("Err_29282naie Expected '-' to be left in the stream with UTF7Encoding and read {0}", byteRead);
                            }
                        }
                    }

                    if (xmitByteBuffer.Length != (numRead = com1.Read(rcvByteBuffer, 0, rcvByteBuffer.Length)))
                    {
                        Fail("ERROR!!!: Expected to read {0} bytes actually read {1}", xmitByteBuffer.Length, numRead);
                    }

                    for (int i = 0; i < xmitByteBuffer.Length; i++)
                    {
                        if (xmitByteBuffer[i] != rcvByteBuffer[i])
                        {
                            Fail("ERROR!!!: Expected to read {0}  actual read  {1} at {2}", (int)xmitByteBuffer[i], (int)rcvByteBuffer[i], i);
                        }
                    }

                    Assert.Equal(0, com1.BytesToRead);

                    /*DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG
                     * if(!retValue) {
                     * for(int i=0; i<xmitCharBuffer.Length; ++i) {
                     *  Debug.WriteLine("(char){0}, ", (int)xmitCharBuffer[i]);
                     * }
                     *
                     * for(int i=0; i<xmitCharBuffer.Length; ++i) {
                     *  Debug.WriteLine("{0}, ", (int)xmitByteBuffer[i]);
                     * }
                     * }*/
                }
        }