Example #1
0
        private void PerformWriteRead(SerialPort com1, SerialPort com2, byte[] xmitBytes, byte[] expectedBytes)
        {
            byte[]    rcvBytes = new byte[expectedBytes.Length * 4];
            Stopwatch sw = new Stopwatch();
            double    expectedTime, actualTime, percentageDifference;
            int       numParityBits = (Parity)com1.Parity == Parity.None ? 0 : 1;
            double    numStopBits   = GetNumberOfStopBits(com1);
            int       length        = xmitBytes.Length;
            int       rcvLength;

            // TODO: Consider removing all of the code to check the time it takes to transfer the bytes.
            // This was likely just a copy and paste from another test case
            actualTime = 0;
            Thread.CurrentThread.Priority = ThreadPriority.Highest;
            for (int i = 0; i < NUM_TRYS; i++)
            {
                com2.DiscardInBuffer();

                IAsyncResult beginWriteResult = com1.BaseStream.BeginWrite(xmitBytes, 0, length, null, null);

                while (0 == com2.BytesToRead)
                {
                    ;
                }

                sw.Start();

                // Wait for all of the bytes to reach the input buffer of com2
                TCSupport.WaitForReadBufferToLoad(com2, length);

                sw.Stop();
                actualTime += sw.ElapsedMilliseconds;
                com1.BaseStream.EndWrite(beginWriteResult);
                sw.Reset();
            }

            Thread.CurrentThread.Priority = ThreadPriority.Normal;
            actualTime          /= NUM_TRYS;
            expectedTime         = ((xmitBytes.Length * (1 + numStopBits + com1.DataBits + numParityBits)) / com1.BaudRate) * 1000;
            percentageDifference = Math.Abs((expectedTime - actualTime) / expectedTime);

            //If the percentageDifference between the expected time and the actual time is to high
            //then the expected baud rate must not have been used and we should report an error
            if (MAX_ACCEPTABEL_PERCENTAGE_DIFFERENCE < percentageDifference)
            {
                Fail("ERROR!!! Parity not used Expected time:{0}, actual time:{1} percentageDifference:{2}", expectedTime, actualTime, percentageDifference);
            }

            rcvLength = com2.Read(rcvBytes, 0, rcvBytes.Length);
            if (0 != com2.BytesToRead)
            {
                Fail("ERROR!!! BytesToRead={0} expected 0", com2.BytesToRead);
            }

            //Verify that the bytes we sent were the same ones we received
            int expectedIndex = 0, actualIndex = 0;

            for (; expectedIndex < expectedBytes.Length && actualIndex < rcvBytes.Length; ++expectedIndex, ++actualIndex)
            {
                if (expectedBytes[expectedIndex] != rcvBytes[actualIndex])
                {
                    if (actualIndex != rcvBytes.Length - 1 && expectedBytes[expectedIndex] == rcvBytes[actualIndex + 1])
                    {
                        //Sometimes if there is a parity error an extra byte gets added to the input stream so
                        //look ahead at the next byte
                        actualIndex++;
                    }
                    else
                    {
                        Debug.WriteLine("Bytes Sent:");
                        TCSupport.PrintBytes(xmitBytes);

                        Debug.WriteLine("Bytes Received:");
                        TCSupport.PrintBytes(rcvBytes);

                        Debug.WriteLine("Expected Bytes:");
                        TCSupport.PrintBytes(expectedBytes);

                        Fail(
                            "ERROR: Expected to read {0,2:X} at {1,3} actually read {2,2:X} sent {3,2:X}",
                            expectedBytes[expectedIndex],
                            expectedIndex,
                            rcvBytes[actualIndex],
                            xmitBytes[expectedIndex]);
                    }
                }
            }

            if (expectedIndex < expectedBytes.Length)
            {
                Fail("ERRROR: Did not enumerate all of the expected bytes index={0} length={1}", expectedIndex, expectedBytes.Length);
            }
        }