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); } }
public void Thread_In_ErrorEvent() { using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName)) { ErrorEventHandler errorEventHandler = new ErrorEventHandler(com1, false, true); Debug.WriteLine("Verifying that if a thread is blocked in a ErrorEvent handler the port can still be closed"); com1.Open(); com2.Open(); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); com1.ErrorReceived += errorEventHandler.HandleEvent; //This should cause ErrorEvent to be fired with a parity error since the //8th bit on com1 is the parity bit, com1 one expest this bit to be 1(Mark), //and com2 is writing 0 for this bit com1.DataBits = 7; com1.Parity = Parity.Mark; com2.BaseStream.Write(new byte[1], 0, 1); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); if (!errorEventHandler.WaitForEvent(MAX_TIME_WAIT, 1)) { Fail("Err_215887ajeid Expected 1 ErrorEvents to be fired and only {0} occured", errorEventHandler.NumEventsHandled); } Task task = Task.Run(() => com1.Close()); Thread.Sleep(5000); errorEventHandler.ResumeHandleEvent(); TCSupport.WaitForTaskCompletion(task); } }
public void SuccessiveReadTimeoutSomeData() { using (var com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) { var rndGen = new Random(-55); var t = new Task(WriteToCom1); com1.ReadTimeout = rndGen.Next(minRandomTimeout, maxRandomTimeout); com1.Encoding = new UTF8Encoding(); Debug.WriteLine( "Verifying ReadTimeout={0} with successive call to read method and some data being received in the first call", com1.ReadTimeout); com1.Open(); // Call WriteToCom1 asynchronously this will write to com1 some time before the following call // to a read method times out t.Start(); try { com1.BaseStream.ReadByte(); } catch (TimeoutException) { } TCSupport.WaitForTaskCompletion(t); // Make sure there is no bytes in the buffer so the next call to read will timeout com1.DiscardInBuffer(); VerifyTimeout(com1); } }
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); } }
public void InOutBufferFilled_Flush_Once() { using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName)) { AsyncWriteRndByteArray asyncWriteRndByteArray = new AsyncWriteRndByteArray(com1, DEFAULT_BUFFER_SIZE); var t = new Task(asyncWriteRndByteArray.WriteRndByteArray); byte[] xmitBytes = new byte[DEFAULT_BUFFER_SIZE]; Debug.WriteLine("Verifying Flush method after input and output buffer has been filled"); com1.Open(); com2.Open(); com1.WriteTimeout = 500; com1.Handshake = Handshake.RequestToSend; for (int i = 0; i < xmitBytes.Length; i++) { xmitBytes[i] = (byte)i; } com2.Write(xmitBytes, 0, xmitBytes.Length); TCSupport.WaitForReadBufferToLoad(com1, DEFAULT_BUFFER_SIZE); t.Start(); TCSupport.WaitForWriteBufferToLoad(com1, DEFAULT_BUFFER_LENGTH); VerifyFlush(com1); TCSupport.WaitForTaskCompletion(t); } }
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); } }
public void Thread_In_PinChangedEvent() { using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName)) { PinChangedEventHandler pinChangedEventHandler = new PinChangedEventHandler(com1, false, true); Debug.WriteLine( "Verifying that if a thread is blocked in a PinChangedEvent handler the port can still be closed"); com1.Open(); com2.Open(); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); com1.PinChanged += pinChangedEventHandler.HandleEvent; //This should cause PinChangedEvent to be fired with SerialPinChanges.DsrChanged //since we are setting DtrEnable to true com2.DtrEnable = true; if (!pinChangedEventHandler.WaitForEvent(MAX_TIME_WAIT, 1)) { Fail("Err_32688ajoid Expected 1 PinChangedEvents to be fired and only {0} occured", pinChangedEventHandler.NumEventsHandled); } Task task = Task.Run(() => com1.Close()); Thread.Sleep(5000); pinChangedEventHandler.ResumeHandleEvent(); TCSupport.WaitForTaskCompletion(task); } }
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 SuccessiveReadTimeoutSomeData_ReadByte() { using (var com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) { var t = new Task(WriteToCom1); com1.Open(); Stream stream = com1.BaseStream; stream.ReadTimeout = SUCCESSIVE_READTIMEOUT_SOMEDATA; Debug.WriteLine( "Verifying ReadTimeout={0} with successive call to ReadByte() and some data being received in the first call", stream.ReadTimeout); // Call WriteToCom1 asynchronously this will write to com1 some time before the following call // to a read method times out t.Start(); try { stream.ReadByte(); } catch (TimeoutException) { } TCSupport.WaitForTaskCompletion(t); // Make sure there is no bytes in the buffer so the next call to read will timeout com1.DiscardInBuffer(); VerifyTimeout(ReadByte, stream); } }
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"); } } }
public void Read_DataReceivedBeforeTimeout() { using (SerialPort com1 = TCSupport.InitFirstSerialPort()) using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1)) { char[] charXmitBuffer = TCSupport.GetRandomChars(512, TCSupport.CharacterOptions.None); char[] charRcvBuffer = new char[charXmitBuffer.Length]; ASyncRead asyncRead = new ASyncRead(com1); var asyncReadTask = new Task(asyncRead.Read); Debug.WriteLine( "Verifying that ReadChar will read characters that have been received after the call to Read was made"); com1.Encoding = Encoding.UTF8; com2.Encoding = Encoding.UTF8; com1.ReadTimeout = 20000; // 20 seconds 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(); } asyncReadTask.Start(); asyncRead.ReadStartedEvent.WaitOne(); //This only tells us that the thread has started to execute code in the method Thread.Sleep(2000); //We need to wait to guarentee that we are executing code in SerialPort com2.Write(charXmitBuffer, 0, charXmitBuffer.Length); asyncRead.ReadCompletedEvent.WaitOne(); Assert.Null(asyncRead.Exception); if (asyncRead.Result != charXmitBuffer[0]) { Fail("Err_0158ahei Expected ReadChar to read {0}({0:X}) actual {1}({1:X})", charXmitBuffer[0], asyncRead.Result); } else { charRcvBuffer[0] = (char)asyncRead.Result; int receivedLength = 1; while (receivedLength < charXmitBuffer.Length) { receivedLength += com1.Read(charRcvBuffer, receivedLength, charRcvBuffer.Length - receivedLength); } Assert.Equal(receivedLength, charXmitBuffer.Length); Assert.Equal(charXmitBuffer, charRcvBuffer); } TCSupport.WaitForTaskCompletion(asyncReadTask); } }
private void VerifyLongTimeout(WriteMethodDelegate writeMethod, SerialPort com1) { var t = new Task(() => { writeMethod(com1.BaseStream); }); t.Start(); Thread.Sleep(DEFAULT_WAIT_LONG_TIMEOUT); Assert.False(t.IsCompleted, string.Format("Err_17071ahpa!!! {0} terminated with a long timeout of {1}ms", writeMethod.Method.Name, com1.BaseStream.WriteTimeout)); com1.Handshake = Handshake.None; TCSupport.WaitForTaskCompletion(t); }
private void VerifyLongTimeout(ReadMethodDelegate readMethod, SerialPort com1, SerialPort com2) { var readThread = new ReadDelegateThread(com1.BaseStream, readMethod); var t = new Task(readThread.CallRead); t.Start(); Thread.Sleep(DEFAULT_WAIT_LONG_TIMEOUT); Assert.False(t.IsCompleted, string.Format("Err_17071ahpa!!! {0} terminated with a long timeout of {1}ms", readMethod.Method.Name, com1.BaseStream.ReadTimeout)); com2.Write(new byte[8], 0, 8); TCSupport.WaitForTaskCompletion(t); }
public void Handshake_None() { using (SerialPort com = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) { AsyncWriteRndStr asyncWriteRndStr = new AsyncWriteRndStr(com, s_STRING_SIZE_HANDSHAKE); var t = new Task(asyncWriteRndStr.WriteRndStr); //Write a random string asynchronously so we can verify some things while the write call is blocking Debug.WriteLine("Case Handshake_None : Verifying Handshake=None"); com.Open(); t.Start(); TCSupport.WaitForTaskCompletion(t); Assert.Equal(0, com.BytesToWrite); } }
public void InOutBufferFilled_Flush_Multiple() { using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName)) { AsyncWriteRndByteArray asyncWriteRndByteArray = new AsyncWriteRndByteArray(com1, DEFAULT_BUFFER_SIZE); var t = new Task(asyncWriteRndByteArray.WriteRndByteArray); int elapsedTime = 0; byte[] xmitBytes = new byte[DEFAULT_BUFFER_SIZE]; Debug.WriteLine( "Verifying call Flush method several times after input and output buffer has been filled"); com1.Open(); com2.Open(); com1.WriteTimeout = 500; com1.Handshake = Handshake.RequestToSend; for (int i = 0; i < xmitBytes.Length; i++) { xmitBytes[i] = (byte)i; } com2.Write(xmitBytes, 0, xmitBytes.Length); TCSupport.WaitForReadBufferToLoad(com1, DEFAULT_BUFFER_SIZE); t.Start(); elapsedTime = 0; while (com1.BytesToWrite < DEFAULT_BUFFER_LENGTH && elapsedTime < MAX_WAIT_TIME) { Thread.Sleep(50); elapsedTime += 50; } VerifyFlush(com1); VerifyFlush(com1); VerifyFlush(com1); TCSupport.WaitForTaskCompletion(t); } }
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); } }
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); } }
public void Handshake_None() { using (SerialPort com = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) { AsyncWriteRndByteArray asyncWriteRndByteArray = new AsyncWriteRndByteArray(com, BYTE_SIZE_HANDSHAKE); var t = new Task(asyncWriteRndByteArray.WriteRndByteArray); //Write a random byte[] asynchronously so we can verify some things while the write call is blocking Debug.WriteLine("Verifying Handshake=None"); com.Open(); t.Start(); TCSupport.WaitForTaskCompletion(t); if (0 != com.BytesToWrite) { Fail("ERROR!!! Expcted BytesToWrite=0 actual {0}", com.BytesToWrite); } } }
public void InAndOutBufferFilled_Discard() { using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName)) { int origBytesToWrite; Debug.WriteLine("Verifying Discard method after input buffer has been filled"); com1.Open(); com2.Open(); com1.WriteTimeout = 500; var task = Task.Run(() => WriteRndByteArray(com1, DEFAULT_BUFFER_LENGTH)); System.Threading.Thread.Sleep(100); origBytesToWrite = com1.BytesToWrite; VerifyDiscard(com1); Assert.Equal(com1.BytesToWrite, origBytesToWrite); //Wait for write method to timeout TCSupport.WaitForTaskCompletion(task); } }
public void VerifyInfiniteTimeout(Action <SerialPort> readMethod, bool setInfiniteTimeout) { using (SerialPort com1 = TCSupport.InitFirstSerialPort()) using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1)) { SerialPortProperties serPortProp = new SerialPortProperties(); serPortProp.SetAllPropertiesToOpenDefaults(); serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); serPortProp.SetProperty("WriteTimeout", 10); com1.WriteTimeout = 10; com1.Open(); serPortProp.VerifyPropertiesAndPrint(com1); if (!com2.IsOpen) { com2.Open(); } if (setInfiniteTimeout) { com1.ReadTimeout = 500; com1.ReadTimeout = SerialPort.InfiniteTimeout; } Task task = Task.Run(() => readMethod(com1)); System.Threading.Thread.Sleep(DEFAULT_WAIT_INFINITE_TIMEOUT); Assert.True(!task.IsCompleted); serPortProp.VerifyPropertiesAndPrint(com1); com2.WriteLine(string.Empty); TCSupport.WaitForTaskCompletion(task); } }
public void OutBufferFilled_Flush_Once() { using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) { AsyncWriteRndByteArray asyncWriteRndByteArray = new AsyncWriteRndByteArray(com1, DEFAULT_BUFFER_SIZE); var t = new Task(asyncWriteRndByteArray.WriteRndByteArray); Debug.WriteLine("Verifying Flush method after output buffer has been filled"); com1.Open(); com1.WriteTimeout = 500; com1.Handshake = Handshake.RequestToSend; t.Start(); TCSupport.WaitForWriteBufferToLoad(com1, DEFAULT_BUFFER_LENGTH); VerifyFlush(com1); TCSupport.WaitForTaskCompletion(t); } }
public void OutBufferFilled_Flush_Cycle() { using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) { AsyncWriteRndByteArray asyncWriteRndByteArray = new AsyncWriteRndByteArray(com1, DEFAULT_BUFFER_SIZE); var t1 = new Task(asyncWriteRndByteArray.WriteRndByteArray); var t2 = new Task(asyncWriteRndByteArray.WriteRndByteArray); int elapsedTime; Debug.WriteLine( "Verifying call Flush method after output buffer has been filled discarded and filled again"); com1.Open(); com1.WriteTimeout = 500; com1.Handshake = Handshake.RequestToSend; t1.Start(); elapsedTime = 0; while (com1.BytesToWrite < DEFAULT_BUFFER_LENGTH && elapsedTime < MAX_WAIT_TIME) { Thread.Sleep(50); elapsedTime += 50; } VerifyFlush(com1); TCSupport.WaitForTaskCompletion(t1); t2.Start(); TCSupport.WaitForWriteBufferToLoad(com1, DEFAULT_BUFFER_LENGTH); VerifyFlush(com1); TCSupport.WaitForTaskCompletion(t2); } }
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); } }
public void Thread_In_ReceivedEvent() { using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName)) { ReceivedEventHandler receivedEventHandler = new ReceivedEventHandler(com1, false, true); Debug.WriteLine( "Verifying that if a thread is blocked in a RecevedEvent handler the port can still be closed"); com1.Open(); com2.Open(); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); com1.DataReceived += receivedEventHandler.HandleEvent; //This should cause ReceivedEvent to be fired with ReceivedChars //since we are writing some bytes com1.DataBits = 8; com1.Parity = Parity.None; com2.BaseStream.Write(new byte[] { 40 }, 0, 1); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); if (!receivedEventHandler.WaitForEvent(MAX_TIME_WAIT, 1)) { Fail("Err_122808aoeid Expected 1 ReceivedEvents to be fired and only {0} occured", receivedEventHandler.NumEventsHandled); } Task task = Task.Run(() => com1.Close()); Thread.Sleep(5000); receivedEventHandler.ResumeHandleEvent(); TCSupport.WaitForTaskCompletion(task); } }
private void VerifyInfiniteTimeout(WriteMethodDelegate writeMethod, bool setInfiniteTimeout) { using (SerialPort com1 = TCSupport.InitFirstSerialPort()) { SerialPortProperties serPortProp = new SerialPortProperties(); serPortProp.SetAllPropertiesToOpenDefaults(); serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); com1.Handshake = Handshake.RequestToSend; serPortProp.SetProperty("ReadTimeout", 10); com1.ReadTimeout = 10; com1.Open(); if (setInfiniteTimeout) { com1.WriteTimeout = 500; com1.WriteTimeout = SerialPort.InfiniteTimeout; } Task task = Task.Run(() => writeMethod(com1)); Thread.Sleep(DEFAULT_WAIT_INFINITE_TIMEOUT); Assert.False(task.IsCompleted, "Task should not have completed while tx is blocked by flow-control"); com1.Handshake = Handshake.None; TCSupport.WaitForTaskCompletion(task); com1.DiscardOutBuffer(); // If we're looped-back, then there will be data queued on the receive side which we need to discard com1.DiscardInBuffer(); serPortProp.VerifyPropertiesAndPrint(com1); } }
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); } } }
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 }