public void BytesToWriteSuccessive()
        {
            using (SerialPort com = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
            {
                AsyncWriteRndCharArray asyncWriteRndCharArray = new AsyncWriteRndCharArray(com, CHAR_SIZE_BYTES_TO_WRITE);
                var t1 = new Task(asyncWriteRndCharArray.WriteRndCharArray);
                var t2 = new Task(asyncWriteRndCharArray.WriteRndCharArray);

                Debug.WriteLine("Verifying BytesToWrite with successive calls to Write");

                com.Handshake = Handshake.RequestToSend;
                com.Open();
                com.WriteTimeout = 4000;

                //Write a random char[] asynchronously so we can verify some things while the write call is blocking
                t1.Start();
                TCSupport.WaitForTaskToStart(t1);
                TCSupport.WaitForExactWriteBufferLoad(com, CHAR_SIZE_BYTES_TO_WRITE);

                //Write a random char[] asynchronously so we can verify some things while the write call is blocking
                t2.Start();
                TCSupport.WaitForTaskToStart(t2);
                TCSupport.WaitForExactWriteBufferLoad(com, CHAR_SIZE_BYTES_TO_WRITE * 2);

                //Wait for both write methods to timeout
                TCSupport.WaitForTaskCompletion(t1);
                var aggregatedException = Assert.Throws <AggregateException>(() => TCSupport.WaitForTaskCompletion(t2));
                Assert.IsType <IOException>(aggregatedException.InnerException);
            }
        }
        public void SuccessiveReadTimeoutWithWriteSucceeding()
        {
            using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
            {
                Random         rndGen         = new Random(-55);
                AsyncEnableRts asyncEnableRts = new AsyncEnableRts();
                var            t = new Task(asyncEnableRts.EnableRTS);

                com1.WriteTimeout = rndGen.Next(minRandomTimeout, maxRandomTimeout);
                com1.Handshake    = Handshake.RequestToSend;
                com1.Encoding     = new UTF8Encoding();

                Debug.WriteLine("Verifying WriteTimeout={0} with successive call to write method with the write succeeding sometime before its timeout", com1.WriteTimeout);
                com1.Open();

                //Call EnableRTS asynchronously this will enable RTS in the middle of the following write call allowing it to succeed
                //before the timeout is reached
                t.Start();
                TCSupport.WaitForTaskToStart(t);

                try
                {
                    com1.Write(new char[CHAR_SIZE_TIMEOUT], 0, CHAR_SIZE_TIMEOUT);
                }
                catch (TimeoutException)
                {
                }

                asyncEnableRts.Stop();

                TCSupport.WaitForTaskCompletion(t);

                VerifyTimeout(com1);
            }
        }
示例#3
0
        public void BytesToWriteSuccessive()
        {
            using (SerialPort com = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
            {
                AsyncWriteRndStr asyncWriteRndStr = new AsyncWriteRndStr(com, s_STRING_SIZE_BYTES_TO_WRITE);
                var t1 = new Task(asyncWriteRndStr.WriteRndStr);
                var t2 = new Task(asyncWriteRndStr.WriteRndStr);

                int numNewLineBytes;

                Debug.WriteLine("Case BytesToWriteSuccessive : Verifying BytesToWrite with successive calls to Write");

                com.Handshake = Handshake.RequestToSend;
                com.Open();
                com.WriteTimeout = 1000;
                numNewLineBytes  = com.Encoding.GetByteCount(com.NewLine.ToCharArray());

                //Write a random string asynchronously so we can verify some things while the write call is blocking
                t1.Start();
                TCSupport.WaitForTaskToStart(t1);
                TCSupport.WaitForWriteBufferToLoad(com, s_STRING_SIZE_BYTES_TO_WRITE + numNewLineBytes);

                //Write a random string asynchronously so we can verify some things while the write call is blocking
                t2.Start();
                TCSupport.WaitForTaskToStart(t2);
                TCSupport.WaitForWriteBufferToLoad(com, (s_STRING_SIZE_BYTES_TO_WRITE + numNewLineBytes) * 2);

                //Wait for both write methods to timeout
                TCSupport.WaitForTaskCompletion(t1);
                var aggregatedException = Assert.Throws <AggregateException>(() => TCSupport.WaitForTaskCompletion(t2));
                Assert.IsType <IOException>(aggregatedException.InnerException);
            }
        }
示例#4
0
        public void BytesToWriteSuccessive()
        {
            using (SerialPort com = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
            {
                Debug.WriteLine("Verifying BytesToWrite with successive calls to Write");

                com.Handshake = Handshake.RequestToSend;
                com.Open();
                com.WriteTimeout = 4000;

                int blockLength = TCSupport.MinimumBlockingByteCount;

                // Write a random byte[] asynchronously so we can verify some things while the write call is blocking
                Task t1 = Task.Run(() => WriteRandomDataBlock(com, blockLength));

                TCSupport.WaitForTaskToStart(t1);

                TCSupport.WaitForWriteBufferToLoad(com, blockLength);

                // Write a random byte[] asynchronously so we can verify some things while the write call is blocking
                Task t2 = Task.Run(() => WriteRandomDataBlock(com, blockLength));

                TCSupport.WaitForTaskToStart(t2);

                TCSupport.WaitForWriteBufferToLoad(com, blockLength * 2);

                // Wait for both write methods to timeout
                TCSupport.WaitForTaskCompletion(t1);
                TCSupport.WaitForTaskCompletion(t2);
            }
        }
示例#5
0
        public void BytesToWrite()
        {
            using (SerialPort com = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
            {
                AsyncWriteRndStr asyncWriteRndStr = new AsyncWriteRndStr(com, s_STRING_SIZE_BYTES_TO_WRITE);
                var t = new Task(asyncWriteRndStr.WriteRndStr);

                int numNewLineBytes;

                Debug.WriteLine("Case BytesToWrite : Verifying BytesToWrite with one call to Write");

                com.Handshake = Handshake.RequestToSend;
                com.Open();
                com.WriteTimeout = 500;

                numNewLineBytes = com.Encoding.GetByteCount(com.NewLine.ToCharArray());

                //Write a random string asynchronously so we can verify some things while the write call is blocking
                t.Start();

                TCSupport.WaitForTaskToStart(t);

                TCSupport.WaitForWriteBufferToLoad(com, s_STRING_SIZE_BYTES_TO_WRITE + numNewLineBytes);

                TCSupport.WaitForTaskCompletion(t);
            }
        }
        private void VerifyTimeout(int writeTimeout)
        {
            using (var com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
                using (var com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName))
                {
                    var asyncRead           = new AsyncWrite(com1);
                    var asyncEndWrite       = new Task(asyncRead.EndWrite);
                    var asyncCallbackCalled = false;

                    com1.Open();
                    com2.Open();
                    com1.Handshake    = Handshake.RequestToSend;
                    com1.WriteTimeout = writeTimeout;

                    IAsyncResult writeAsyncResult = com1.BaseStream.BeginWrite(new byte[8], 0, 8, ar => asyncCallbackCalled = true, null);
                    asyncRead.WriteAsyncResult = writeAsyncResult;

                    Thread.Sleep(100 > com1.WriteTimeout ? 2 * com1.WriteTimeout : 200);
                    // Sleep for 200ms or 2 times the WriteTimeout

                    if (writeAsyncResult.IsCompleted)
                    {
                        // Verify the IAsyncResult has not completed
                        Fail("Err_565088aueiud!!!: Expected read to not have completed");
                    }

                    asyncEndWrite.Start();
                    TCSupport.WaitForTaskToStart(asyncEndWrite);
                    Thread.Sleep(100 < com1.WriteTimeout ? 2 * com1.WriteTimeout : 200);
                    // Sleep for 200ms or 2 times the WriteTimeout

                    if (asyncEndWrite.IsCompleted)
                    {
                        // Verify EndRead is blocking and is still alive
                        Fail("Err_4085858aiehe!!!: Expected read to not have completed");
                    }

                    if (asyncCallbackCalled)
                    {
                        Fail("Err_750551aiuehd!!!: Expected AsyncCallback not to be called");
                    }

                    com2.RtsEnable = true;

                    TCSupport.WaitForTaskCompletion(asyncEndWrite);
                    var waitTime = 0;
                    while (!asyncCallbackCalled && waitTime < 5000)
                    {
                        Thread.Sleep(50);
                        waitTime += 50;
                    }

                    if (!asyncCallbackCalled)
                    {
                        Fail(
                            "Err_21208aheide!!!: Expected AsyncCallback to be called after some data was written to the port");
                    }
                }
        }
示例#7
0
        public void Handshake_None()
        {
            using (SerialPort com = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
            {
                Debug.WriteLine("Verifying Handshake=None");

                com.Open();

                // Write a random byte[] asynchronously so we can verify some things while the write call is blocking
                Task task = Task.Run(() => WriteRandomDataBlock(com, TCSupport.MinimumBlockingByteCount));

                TCSupport.WaitForTaskToStart(task);

                // Wait for write methods to complete
                TCSupport.WaitForTaskCompletion(task);

                Assert.Equal(0, com.BytesToWrite);
            }
        }
        public void BytesToWrite()
        {
            using (SerialPort com = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
            {
                AsyncWriteRndCharArray asyncWriteRndCharArray = new AsyncWriteRndCharArray(com, CHAR_SIZE_BYTES_TO_WRITE);
                var t = new Task(asyncWriteRndCharArray.WriteRndCharArray);

                Debug.WriteLine("Verifying BytesToWrite with one call to Write");

                com.Handshake = Handshake.RequestToSend;
                com.Open();
                com.WriteTimeout = 500;

                //Write a random char[] asynchronously so we can verify some things while the write call is blocking
                t.Start();
                TCSupport.WaitForTaskToStart(t);
                TCSupport.WaitForExactWriteBufferLoad(com, CHAR_SIZE_BYTES_TO_WRITE);
                TCSupport.WaitForTaskCompletion(t);
            }
        }
示例#9
0
        public void BytesToWrite()
        {
            using (SerialPort com = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
            {
                Debug.WriteLine("Verifying BytesToWrite with one call to Write");

                com.Handshake = Handshake.RequestToSend;
                com.Open();
                com.WriteTimeout = 200;

                // Write a random byte[] asynchronously so we can verify some things while the write call is blocking
                Task task = Task.Run(() => WriteRandomDataBlock(com, TCSupport.MinimumBlockingByteCount));
                TCSupport.WaitForTaskToStart(task);

                TCSupport.WaitForWriteBufferToLoad(com, TCSupport.MinimumBlockingByteCount);

                // Wait for write method to timeout and complete the task
                TCSupport.WaitForTaskCompletion(task);
            }
        }
示例#10
0
        public void SuccessiveWriteTimeoutSomeData_WriteByte()
        {
            using (var com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
            {
                var asyncEnableRts = new AsyncEnableRts();
                var t = new Task(asyncEnableRts.EnableRTS);

                com1.Open();
                com1.Handshake = Handshake.RequestToSend;
                Stream stream = com1.BaseStream;
                stream.WriteTimeout = SUCCESSIVE_WriteTimeout_SOMEDATA;

                Debug.WriteLine(
                    "Verifying WriteTimeout={0} with successive call to WriteByte() and some data being received in the first call",
                    stream.WriteTimeout);

                // Call EnableRTS asynchronously this will enable RTS in the middle of the following write call allowing it to succeed
                // before the timeout is reached
                t.Start();
                TCSupport.WaitForTaskToStart(t);
                try
                {
                    stream.WriteByte(DEFAULT_WRITE_BYTE);
                }
                catch (TimeoutException)
                {
                }

                asyncEnableRts.Stop();

                TCSupport.WaitForTaskCompletion(t);

                // Make sure there is no bytes in the buffer so the next call to write will timeout
                com1.DiscardInBuffer();

                VerifyTimeout(WriteByte, stream);
            }
        }
示例#11
0
        private void Verify_Handshake(Handshake handshake)
        {
            using (var com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
                using (var com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName))
                {
                    Debug.WriteLine("Verifying Handshake={0}", handshake);
                    com1.Handshake = handshake;
                    com1.Open();
                    com2.Open();

                    // Setup to ensure write will bock with type of handshake method being used
                    if (Handshake.RequestToSend == handshake || Handshake.RequestToSendXOnXOff == handshake)
                    {
                        com2.RtsEnable = false;
                    }

                    if (Handshake.XOnXOff == handshake || Handshake.RequestToSendXOnXOff == handshake)
                    {
                        com2.BaseStream.WriteByte(XOnOff.XOFF);
                        Thread.Sleep(250);
                    }

                    // Write a block of random data asynchronously so we can verify some things while the write call is blocking
                    Task task = Task.Run(() => WriteRandomDataBlock(com1, TCSupport.MinimumBlockingByteCount));

                    TCSupport.WaitForTaskToStart(task);

                    TCSupport.WaitForWriteBufferToLoad(com1, TCSupport.MinimumBlockingByteCount);

                    // Verify that CtsHolding is false if the RequestToSend or RequestToSendXOnXOff handshake method is used
                    if ((Handshake.RequestToSend == handshake || Handshake.RequestToSendXOnXOff == handshake) &&
                        com1.CtsHolding)
                    {
                        Fail("ERROR!!! Expected CtsHolding={0} actual {1}", false, com1.CtsHolding);
                    }

                    // Setup to ensure write will succeed
                    if (Handshake.RequestToSend == handshake || Handshake.RequestToSendXOnXOff == handshake)
                    {
                        com2.RtsEnable = true;
                    }

                    if (Handshake.XOnXOff == handshake || Handshake.RequestToSendXOnXOff == handshake)
                    {
                        com2.BaseStream.WriteByte(XOnOff.XON);
                    }

                    // Wait till write finishes
                    TCSupport.WaitForTaskCompletion(task);

                    // Verify that the correct number of bytes are in the buffer
                    // (There should be nothing because it's all been transmitted after the flow control was released)
                    Assert.Equal(0, com1.BytesToWrite);

                    // Verify that CtsHolding is true if the RequestToSend or RequestToSendXOnXOff handshake method is used
                    if ((Handshake.RequestToSend == handshake || Handshake.RequestToSendXOnXOff == handshake) &&
                        !com1.CtsHolding)
                    {
                        Fail("ERROR!!! Expected CtsHolding={0} actual {1}", true, com1.CtsHolding);
                    }
                }
        }
示例#12
0
        private void Verify_Handshake(Handshake handshake)
        {
            using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
                using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName))
                {
                    byte[] XOffBuffer = new byte[1];
                    byte[] XOnBuffer  = new byte[1];

                    XOffBuffer[0] = 19;
                    XOnBuffer[0]  = 17;

                    int numNewLineBytes;

                    Debug.WriteLine("Verifying Handshake={0}", handshake);

                    com1.WriteTimeout = 3000;
                    com1.Handshake    = handshake;
                    com1.Open();
                    com2.Open();

                    numNewLineBytes = com1.Encoding.GetByteCount(com1.NewLine.ToCharArray());

                    //Setup to ensure write will block with type of handshake method being used
                    if (Handshake.RequestToSend == handshake || Handshake.RequestToSendXOnXOff == handshake)
                    {
                        com2.RtsEnable = false;
                    }

                    if (Handshake.XOnXOff == handshake || Handshake.RequestToSendXOnXOff == handshake)
                    {
                        com2.Write(XOffBuffer, 0, 1);
                        Thread.Sleep(250);
                    }

                    //Write a random string asynchronously so we can verify some things while the write call is blocking
                    string randomLine      = TCSupport.GetRandomString(s_STRING_SIZE_HANDSHAKE, TCSupport.CharacterOptions.Surrogates);
                    byte[] randomLineBytes = com1.Encoding.GetBytes(randomLine);
                    Task   task            = Task.Run(() => com1.WriteLine(randomLine));

                    TCSupport.WaitForTaskToStart(task);

                    TCSupport.WaitForWriteBufferToLoad(com1, randomLineBytes.Length + numNewLineBytes);

                    //Verify that CtsHolding is false if the RequestToSend or RequestToSendXOnXOff handshake method is used
                    if ((Handshake.RequestToSend == handshake || Handshake.RequestToSendXOnXOff == handshake) && com1.CtsHolding)
                    {
                        Fail("ERROR!!! Expcted CtsHolding={0} actual {1}", false, com1.CtsHolding);
                    }

                    //Setup to ensure write will succeed
                    if (Handshake.RequestToSend == handshake || Handshake.RequestToSendXOnXOff == handshake)
                    {
                        com2.RtsEnable = true;
                    }

                    if (Handshake.XOnXOff == handshake || Handshake.RequestToSendXOnXOff == handshake)
                    {
                        com2.Write(XOnBuffer, 0, 1);
                    }

                    //Wait till write finishes
                    TCSupport.WaitForTaskCompletion(task);

                    Assert.Equal(0, com1.BytesToWrite);

                    //Verify that CtsHolding is true if the RequestToSend or RequestToSendXOnXOff handshake method is used
                    if ((Handshake.RequestToSend == handshake || Handshake.RequestToSendXOnXOff == handshake) &&
                        !com1.CtsHolding)
                    {
                        Fail("ERROR!!! Expected CtsHolding={0} actual {1}", true, com1.CtsHolding);
                    }
                }
        }
        private void Verify_Handshake(Handshake handshake)
        {
            using (var com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
                using (var com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName))
                {
                    var asyncWriteRndByteArray = new AsyncWriteRndByteArray(com1, s_BYTE_SIZE_HANDSHAKE);
                    var t = new Task(asyncWriteRndByteArray.WriteRndByteArray);

                    var XOffBuffer = new byte[1];
                    var XOnBuffer  = new byte[1];

                    XOffBuffer[0] = 19;
                    XOnBuffer[0]  = 17;

                    Debug.WriteLine("Verifying Handshake={0}", handshake);
                    com1.Handshake = handshake;

                    com1.Open();
                    com2.Open();

                    // Setup to ensure write will bock with type of handshake method being used
                    if (Handshake.RequestToSend == handshake || Handshake.RequestToSendXOnXOff == handshake)
                    {
                        com2.RtsEnable = false;
                    }

                    if (Handshake.XOnXOff == handshake || Handshake.RequestToSendXOnXOff == handshake)
                    {
                        com2.BaseStream.Write(XOffBuffer, 0, 1);
                        Thread.Sleep(250);
                    }

                    // Write a random byte asynchronously so we can verify some things while the write call is blocking
                    t.Start();
                    TCSupport.WaitForTaskToStart(t);
                    TCSupport.WaitForWriteBufferToLoad(com1, s_BYTE_SIZE_HANDSHAKE);

                    // Verify that CtsHolding is false if the RequestToSend or RequestToSendXOnXOff handshake method is used
                    if ((Handshake.RequestToSend == handshake || Handshake.RequestToSendXOnXOff == handshake) &&
                        com1.CtsHolding)
                    {
                        Fail("ERROR!!! Expected CtsHolding={0} actual {1}", false, com1.CtsHolding);
                    }

                    // Setup to ensure write will succeed
                    if (Handshake.RequestToSend == handshake || Handshake.RequestToSendXOnXOff == handshake)
                    {
                        com2.RtsEnable = true;
                    }

                    if (Handshake.XOnXOff == handshake || Handshake.RequestToSendXOnXOff == handshake)
                    {
                        com2.BaseStream.Write(XOnBuffer, 0, 1);
                    }

                    TCSupport.WaitForTaskCompletion(t);

                    // Verify that the correct number of bytes are in the buffer
                    Assert.Equal(0, com1.BytesToWrite);

                    // Verify that CtsHolding is true if the RequestToSend or RequestToSendXOnXOff handshake method is used
                    if ((Handshake.RequestToSend == handshake || Handshake.RequestToSendXOnXOff == handshake) &&
                        !com1.CtsHolding)
                    {
                        Fail("ERROR!!! Expected CtsHolding={0} actual {1}", true, com1.CtsHolding);
                    }
                }

            #endregion
        }
示例#14
0
        private void Verify_Handshake(Handshake handshake)
        {
            using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
                using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName))
                {
                    AsyncWriteRndStr asyncWriteRndStr = new AsyncWriteRndStr(com1, STRING_SIZE_HANDSHAKE);
                    var t = new Task(asyncWriteRndStr.WriteRndStr);

                    byte[] XOffBuffer = new byte[1];
                    byte[] XOnBuffer  = new byte[1];

                    XOffBuffer[0] = 19;
                    XOnBuffer[0]  = 17;

                    Debug.WriteLine("Verifying Handshake={0}", handshake);

                    com1.Handshake = handshake;
                    com1.Open();
                    com2.Open();

                    //Setup to ensure write will bock with type of handshake method being used
                    if (Handshake.RequestToSend == handshake || Handshake.RequestToSendXOnXOff == handshake)
                    {
                        com2.RtsEnable = false;
                    }

                    if (Handshake.XOnXOff == handshake || Handshake.RequestToSendXOnXOff == handshake)
                    {
                        com2.Write(XOffBuffer, 0, 1);
                        Thread.Sleep(250);
                    }

                    //Write a random string asynchronously so we can verify some things while the write call is blocking
                    t.Start();
                    TCSupport.WaitForTaskToStart(t);
                    TCSupport.WaitForExactWriteBufferLoad(com1, STRING_SIZE_HANDSHAKE);

                    //Verify that CtsHolding is false if the RequestToSend or RequestToSendXOnXOff handshake method is used
                    if ((Handshake.RequestToSend == handshake || Handshake.RequestToSendXOnXOff == handshake) && com1.CtsHolding)
                    {
                        Fail("ERROR!!! Expcted CtsHolding={0} actual {1}", false, com1.CtsHolding);
                    }

                    //Setup to ensure write will succeed
                    if (Handshake.RequestToSend == handshake || Handshake.RequestToSendXOnXOff == handshake)
                    {
                        com2.RtsEnable = true;
                    }

                    if (Handshake.XOnXOff == handshake || Handshake.RequestToSendXOnXOff == handshake)
                    {
                        com2.Write(XOnBuffer, 0, 1);
                    }

                    //Wait till write finishes
                    TCSupport.WaitForTaskCompletion(t);

                    //Verify that the correct number of bytes are in the buffer
                    if (0 != com1.BytesToWrite)
                    {
                        Fail("ERROR!!! Expcted BytesToWrite=0 actual {0}", com1.BytesToWrite);
                    }

                    //Verify that CtsHolding is true if the RequestToSend or RequestToSendXOnXOff handshake method is used
                    if ((Handshake.RequestToSend == handshake || Handshake.RequestToSendXOnXOff == handshake) &&
                        !com1.CtsHolding)
                    {
                        Fail("ERROR!!! Expcted CtsHolding={0} actual {1}", true, com1.CtsHolding);
                    }
                }
        }