private void VerifyBytesToWrite(WriteMethodDelegate writeMethod, int bufferSize, bool sendNewLine) { using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName)) { SerialPortProperties serPortProp = new SerialPortProperties(); int expectedBytesToWrite; int actualBytesToWrite; serPortProp.SetAllPropertiesToOpenDefaults(); serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); serPortProp.SetProperty("Handshake", Handshake.RequestToSend); serPortProp.SetProperty("WriteTimeout", 500); com1.Open(); com1.WriteTimeout = 500; com1.Handshake = Handshake.RequestToSend; Task <int> task = Task.Run(() => writeMethod(com1, bufferSize)); System.Threading.Thread.Sleep(200); actualBytesToWrite = com1.BytesToWrite; expectedBytesToWrite = task.Result - TCSupport.HardwareTransmitBufferSize; com2.Open(); com2.RtsEnable = true; Assert.Equal(expectedBytesToWrite, actualBytesToWrite); com2.RtsEnable = false; serPortProp.VerifyPropertiesAndPrint(com1); } }
public bool VerifyInfiniteTimeout(WriteMethodDelegate readMethod, bool setInfiniteTimeout) { SerialPort com1 = TCSupport.InitFirstSerialPort(); SerialPort com2 = null; WriteDelegateThread readThread = new WriteDelegateThread(com1, readMethod); System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(readThread.CallWrite)); SerialPortProperties serPortProp = new SerialPortProperties(); bool retValue = true; 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; } t.Start(); System.Threading.Thread.Sleep(DEFAULT_WAIT_INFINITE_TIMEOUT); if (!t.IsAlive) { Console.WriteLine("ERROR!!! {0} terminated with infinite timeout", readMethod.Method.Name); retValue = false; } com1.Handshake = Handshake.None; while (t.IsAlive) { System.Threading.Thread.Sleep(10); } com1.DiscardOutBuffer(); retValue &= serPortProp.VerifyPropertiesAndPrint(com1); if (com1.IsOpen) { com1.Close(); } return(retValue); }
public bool Verify1Timeout(SerialPort com, WriteMethodDelegate readMethod) { SerialPortProperties serPortProp = new SerialPortProperties(); System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); bool retValue = true; int actualTime = 0; serPortProp.SetAllPropertiesToOpenDefaults(); serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); serPortProp.SetProperty("WriteTimeout", 1); serPortProp.SetProperty("Handshake", Handshake.RequestToSend); com.Handshake = Handshake.RequestToSend; serPortProp.SetProperty("ReadTimeout", 1000); com.ReadTimeout = 1000; System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Highest; sw.Start(); readMethod(com); sw.Stop(); if (MAX_ACCEPTABLE_WARMUP_ZERO_TIMEOUT < sw.ElapsedMilliseconds) { Console.WriteLine("Err_2570ajdlkj!!! Write Method {0} timed out in {1}ms expected something less then {2}ms", readMethod.Method.Name, sw.ElapsedMilliseconds, MAX_ACCEPTABLE_WARMUP_ZERO_TIMEOUT); retValue = false; } sw.Reset(); for (int i = 0; i < NUM_TRYS; i++) { sw.Start(); readMethod(com); sw.Stop(); actualTime += (int)sw.ElapsedMilliseconds; sw.Reset(); } System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Normal; actualTime /= NUM_TRYS; if (MAX_ACCEPTABLE_ZERO_TIMEOUT < actualTime) { Console.WriteLine("ERROR!!! Write Method {0} timed out in {1}ms expected something less then {2}ms", readMethod.Method.Name, actualTime, MAX_ACCEPTABLE_ZERO_TIMEOUT); retValue = false; } retValue &= serPortProp.VerifyPropertiesAndPrint(com); if (com.IsOpen) { com.Close(); } return(retValue); }
private void Verify1TimeoutAfterOpen(WriteMethodDelegate writeMethod) { using (SerialPort com = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) { com.Open(); com.WriteTimeout = 1; Verify1Timeout(com, writeMethod); } }
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); }
public bool Verify1TimeoutAfterOpen(WriteMethodDelegate readMethod) { SerialPort com = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); bool retValue = true; com.Open(); com.WriteTimeout = 1; retValue &= Verify1Timeout(com, readMethod); return(retValue); }
private bool VerifyTimeout(WriteMethodDelegate writeMethod, Stream stream) { System.Diagnostics.Stopwatch timer = new Stopwatch(); int expectedTime = stream.WriteTimeout; int actualTime; double percentageDifference; bool retValue = true; //Warmup the write method. When called for the first time the write method seems to take much longer then subsequent calls timer.Start(); try { writeMethod(stream); } catch (TimeoutException) { } timer.Stop(); actualTime = (int)timer.ElapsedMilliseconds; percentageDifference = System.Math.Abs((expectedTime - actualTime) / (double)expectedTime); //Verify that the percentage difference between the expected and actual timeout is less then maxPercentageDifference retValue &= Eval(percentageDifference <= MAX_ACCEPTABLE_WARMUP_PERCENTAGE_DIFFERENCE, String.Format("Err_88558amuph!!!: The write method timedout in {0} expected {1} percentage difference: {2} when called for the first time", actualTime, expectedTime, percentageDifference)); actualTime = 0; timer.Reset(); //Perform the actual test verifying that the write method times out in approximately WriteTimeout milliseconds System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Highest; for (int i = 0; i < NUM_TRYS; i++) { timer.Start(); try { writeMethod(stream); } catch (TimeoutException) { } timer.Stop(); actualTime += (int)timer.ElapsedMilliseconds; timer.Reset(); } System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Normal; actualTime /= NUM_TRYS; percentageDifference = System.Math.Abs((expectedTime - actualTime) / (double)expectedTime); //Verify that the percentage difference between the expected and actual timeout is less then maxPercentageDifference retValue &= Eval(percentageDifference <= MAX_ACCEPTABLE_PERCENTAGE_DIFFERENCE, String.Format("Err_56485ahpbz!!!: The write method timedout in {0} expected {1} percentage difference: {2}", actualTime, expectedTime, percentageDifference)); return(retValue); }
private void VerifyLongTimeout(WriteMethodDelegate writeMethod, int writeTimeout) { using (SerialPort com1 = TCSupport.InitFirstSerialPort()) { com1.Open(); com1.Handshake = Handshake.RequestToSend; com1.BaseStream.ReadTimeout = 1; com1.BaseStream.WriteTimeout = writeTimeout; Assert.Equal(writeTimeout, com1.BaseStream.WriteTimeout); VerifyLongTimeout(writeMethod, com1); } }
private bool VerifyBytesToWrite(WriteMethodDelegate writeMethod, int bufferSize, bool sendNewLine) { SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName); SerialPortProperties serPortProp = new SerialPortProperties(); bool retValue = true; IAsyncResult writeMethodResult; int expectedlBytesToWrite; int acutualBytesToWrite; serPortProp.SetAllPropertiesToOpenDefaults(); serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); serPortProp.SetProperty("Handshake", Handshake.RequestToSend); serPortProp.SetProperty("WriteTimeout", 500); com1.Open(); com1.WriteTimeout = 500; com1.Handshake = Handshake.RequestToSend; writeMethodResult = writeMethod.BeginInvoke(com1, bufferSize, null, null); System.Threading.Thread.Sleep(200); acutualBytesToWrite = com1.BytesToWrite; writeMethodResult.AsyncWaitHandle.WaitOne(); expectedlBytesToWrite = (int)writeMethod.EndInvoke(writeMethodResult); com2.Open(); com2.RtsEnable = true; if (acutualBytesToWrite != expectedlBytesToWrite) { Console.WriteLine("ERRROR!!! Expected BytesToWrite={0} acutual BytesToWrite={1}", expectedlBytesToWrite, acutualBytesToWrite); retValue = true; } com2.RtsEnable = false; retValue &= serPortProp.VerifyPropertiesAndPrint(com1); if (com1.IsOpen) { com1.Close(); } if (com2.IsOpen) { com2.Close(); } return(retValue); }
private bool Verify0Timeout(WriteMethodDelegate writeMethod, Stream stream) { System.Diagnostics.Stopwatch timer = new Stopwatch(); int expectedTime = stream.WriteTimeout; int actualTime; bool retValue = true; //Warmup the write method. When called for the first time the write method seems to take much longer then subsequent calls timer.Start(); try { writeMethod(stream); } catch (TimeoutException) { } timer.Stop(); actualTime = (int)timer.ElapsedMilliseconds; //Verify that the time the method took to timeout is less then the maximum acceptable time retValue &= Eval(actualTime <= MAX_ACCEPTABLE_WARMUP_ZERO_TIMEOUT, String.Format("Err_277a0ahpsb!!!: With a timeout of 0 the write method timedout in {0} expected something less then {1} when called for the first time", actualTime, MAX_ACCEPTABLE_WARMUP_ZERO_TIMEOUT)); actualTime = 0; timer.Reset(); //Perform the actual test verifying that the write method times out in approximately WriteTimeout milliseconds System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Highest; for (int i = 0; i < NUM_TRYS; i++) { timer.Start(); try { writeMethod(stream); } catch (TimeoutException) { } timer.Stop(); actualTime += (int)timer.ElapsedMilliseconds; timer.Reset(); } System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Normal; actualTime /= NUM_TRYS; //Verify that the time the method took to timeout is less then the maximum acceptable time retValue &= Eval(actualTime <= MAX_ACCEPTABLE_ZERO_TIMEOUT, String.Format("Err_112389ahbp!!!: With a timeout of 0 the write method timedout in {0} expected something less then {1}", actualTime, MAX_ACCEPTABLE_ZERO_TIMEOUT)); return(retValue); }
private void VerifyLongTimeout(WriteMethodDelegate writeMethod, SerialPort com1) { var writeThread = new WriteDelegateThread(com1.BaseStream, writeMethod); var t = new Thread(writeThread.CallWrite); t.Start(); Thread.Sleep(DEFAULT_WAIT_LONG_TIMEOUT); Assert.True(t.IsAlive, string.Format("Err_17071ahpa!!! {0} terminated with a long timeout of {1}ms", writeMethod.Method.Name, com1.BaseStream.WriteTimeout)); com1.Handshake = Handshake.None; while (t.IsAlive) { Thread.Sleep(10); } }
private void Verify1Timeout(SerialPort com, WriteMethodDelegate writeMethod) { SerialPortProperties serPortProp = new SerialPortProperties(); Stopwatch sw = new Stopwatch(); int actualTime = 0; serPortProp.SetAllPropertiesToOpenDefaults(); serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); serPortProp.SetProperty("WriteTimeout", 1); serPortProp.SetProperty("Handshake", Handshake.RequestToSend); com.Handshake = Handshake.RequestToSend; serPortProp.SetProperty("ReadTimeout", 1000); com.ReadTimeout = 1000; Thread.CurrentThread.Priority = ThreadPriority.Highest; sw.Start(); writeMethod(com); sw.Stop(); if (MAX_ACCEPTABLE_WARMUP_ZERO_TIMEOUT < sw.ElapsedMilliseconds) { Fail("Err_2570ajdlkj!!! Write Method {0} timed out in {1}ms expected something less then {2}ms", writeMethod.Method.Name, sw.ElapsedMilliseconds, MAX_ACCEPTABLE_WARMUP_ZERO_TIMEOUT); } sw.Reset(); for (int i = 0; i < NUM_TRYS; i++) { sw.Start(); writeMethod(com); sw.Stop(); actualTime += (int)sw.ElapsedMilliseconds; sw.Reset(); } Thread.CurrentThread.Priority = ThreadPriority.Normal; actualTime /= NUM_TRYS; if (MAX_ACCEPTABLE_ZERO_TIMEOUT < actualTime) { Fail("ERROR!!! Write Method {0} timed out in {1}ms expected something less then {2}ms", writeMethod.Method.Name, actualTime, MAX_ACCEPTABLE_ZERO_TIMEOUT); } serPortProp.VerifyPropertiesAndPrint(com); }
private void VerifyDefaultTimeout(WriteMethodDelegate writeMethod) { using (SerialPort com1 = TCSupport.InitFirstSerialPort()) { Debug.WriteLine("Serial port being used : " + com1.PortName); com1.Open(); com1.Handshake = Handshake.RequestToSend; com1.BaseStream.ReadTimeout = 1; Assert.Equal(-1, com1.BaseStream.WriteTimeout); VerifyLongTimeout(writeMethod, com1); } }
private void Verify0Timeout(WriteMethodDelegate writeMethod) { using (var com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) { com1.Open(); com1.Handshake = Handshake.RequestToSend; com1.BaseStream.ReadTimeout = 1; com1.BaseStream.WriteTimeout = 1; com1.BaseStream.WriteTimeout = 1; Assert.Equal(1, com1.BaseStream.WriteTimeout); Verify0Timeout(writeMethod, com1.BaseStream); } }
public bool VerifyLongTimeout(WriteMethodDelegate writeMethod, SerialPort com1) { SerialPort com2 = null; WriteDelegateThread writeThread = new WriteDelegateThread(com1.BaseStream, writeMethod); System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(writeThread.CallWrite)); bool retValue = true; t.Start(); System.Threading.Thread.Sleep(DEFAULT_WAIT_LONG_TIMEOUT); retValue &= Eval(t.IsAlive, String.Format("Err_17071ahpa!!! {0} terminated with a long timeout of {1}ms", writeMethod.Method.Name, com1.BaseStream.WriteTimeout)); com1.Handshake = Handshake.None; while (t.IsAlive) { System.Threading.Thread.Sleep(10); } return(retValue); }
private void VerifyInfiniteTimeout(WriteMethodDelegate readMethod, bool setInfiniteTimeout) { using (SerialPort com1 = TCSupport.InitFirstSerialPort()) { WriteDelegateThread readThread = new WriteDelegateThread(com1, readMethod); System.Threading.Thread t = new System.Threading.Thread(readThread.CallWrite); 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; } t.Start(); System.Threading.Thread.Sleep(DEFAULT_WAIT_INFINITE_TIMEOUT); Assert.True(t.IsAlive); com1.Handshake = Handshake.None; while (t.IsAlive) { System.Threading.Thread.Sleep(10); } com1.DiscardOutBuffer(); // If we're looped-back, then there will be data queud on the receive side which we need to discard com1.DiscardInBuffer(); serPortProp.VerifyPropertiesAndPrint(com1); } }
public bool VerifyDefaultTimeout(WriteMethodDelegate writeMethod) { SerialPort com1 = TCSupport.InitFirstSerialPort(); Console.WriteLine("Serial port being used : " + com1.PortName); bool retValue = true; com1.Open(); com1.Handshake = Handshake.RequestToSend; com1.BaseStream.ReadTimeout = 1; retValue &= Eval(-1 == com1.BaseStream.WriteTimeout, String.Format("Err_70217shpza!!! Expected WriteTimeout to be {0} actual {1}", -1, com1.BaseStream.WriteTimeout)); retValue &= VerifyLongTimeout(writeMethod, com1); if (com1.IsOpen) { com1.Close(); } return(retValue); }
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); } }
public bool VerifyLongTimeout(WriteMethodDelegate writeMethod, int WriteTimeout) { SerialPort com1 = TCSupport.InitFirstSerialPort(); bool retValue = true; com1.Open(); com1.Handshake = Handshake.RequestToSend; com1.BaseStream.ReadTimeout = 1; com1.BaseStream.WriteTimeout = 1; com1.BaseStream.WriteTimeout = WriteTimeout; retValue &= Eval(WriteTimeout == com1.BaseStream.WriteTimeout, String.Format("Err_7071ahpsb!!! Expected WriteTimeout to be {0} actual {1}", WriteTimeout, com1.BaseStream.WriteTimeout)); retValue &= VerifyLongTimeout(writeMethod, com1); if (com1.IsOpen) { com1.Close(); } return(retValue); }
private bool Verify0Timeout(WriteMethodDelegate writeMethod) { SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); bool retValue = true; com1.Open(); com1.Handshake = Handshake.RequestToSend; com1.BaseStream.ReadTimeout = 1; com1.BaseStream.WriteTimeout = 1; com1.BaseStream.WriteTimeout = 1; retValue &= Eval(1 == com1.BaseStream.WriteTimeout, String.Format("Err_72072ahps!!! Expected WriteTimeout to be {0} actaul {1}", 1, com1.BaseStream.WriteTimeout)); retValue &= Verify0Timeout(writeMethod, com1.BaseStream); if (com1.IsOpen) { com1.Close(); } return(retValue); }
static CvLogger() { WriteMethod = (s) => Debug.WriteLine(s); Stopwatch = new Stopwatch(); Stopwatch.Start(); }
public WriteDelegateThread(SerialPort com, WriteMethodDelegate writeMethod) { _com = com; _writeMethod = writeMethod; }
public WriteDelegateThread(Stream stream, WriteMethodDelegate writeMethod) { _stream = stream; _writeMethod = writeMethod; }
static Logger() { Stopwatch = new Stopwatch(); Stopwatch.Start(); WriteMethod = new WriteMethodDelegate((s) => { Console.Write(s); }); }