private void VerifyExceptionAtOpen(SerialPort com, int stopBits, ThrowAt throwAt, Type expectedException) { int origStopBits = (int)com.StopBits; SerialPortProperties serPortProp = new SerialPortProperties(); serPortProp.SetAllPropertiesToDefaults(); serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); if (ThrowAt.Open == throwAt) { serPortProp.SetProperty("StopBits", (StopBits)stopBits); } try { com.StopBits = (StopBits)stopBits; if (ThrowAt.Open == throwAt) { com.Open(); } if (null != expectedException) { Fail("ERROR!!! Expected Open() to throw {0} and nothing was thrown", expectedException); } } catch (Exception e) { if (null == expectedException) { Fail("ERROR!!! Expected Open() NOT to throw an exception and {0} was thrown", e.GetType()); } else if (e.GetType() != expectedException) { Fail("ERROR!!! Expected Open() throw {0} and {1} was thrown", expectedException, e.GetType()); } } serPortProp.VerifyPropertiesAndPrint(com); com.StopBits = (StopBits)origStopBits; }
private void VerifyCtor(string portName, int baudRate, int parity, int dataBits, int stopBits, Type expectedException, ThrowAt throwAt) { SerialPortProperties serPortProp = new SerialPortProperties(); Debug.WriteLine("Verifying properties where PortName={0},BaudRate={1},Parity={2},DatBits={3},StopBits={4}", portName, baudRate, parity, dataBits, stopBits); try { using (SerialPort com = new SerialPort(portName, baudRate, (Parity)parity, dataBits, (StopBits)stopBits)) { if (null != expectedException && throwAt == ThrowAt.Set) { Fail("Err_7212ahsdj Expected Ctor to throw {0}", expectedException); } serPortProp.SetAllPropertiesToDefaults(); serPortProp.SetProperty("PortName", portName); serPortProp.SetProperty("BaudRate", baudRate); serPortProp.SetProperty("Parity", (Parity)parity); serPortProp.SetProperty("DataBits", dataBits); serPortProp.SetProperty("StopBits", (StopBits)stopBits); serPortProp.VerifyPropertiesAndPrint(com); } } catch (Exception e) { if (null == expectedException) { Fail("Err_07081hadnh Did not expect exception to be thrown and the following was thrown: \n{0}", e); } else if (throwAt == ThrowAt.Open) { Fail("Err_88916adfa Expected {0} to be thrown at Open and the following was thrown at Set: \n{1}", expectedException, e); } else if (e.GetType() != expectedException) { Fail("Err_90282ahwhp Expected {0} to be thrown and the following was thrown: \n{1}", expectedException, e); } } }
private void VerifyCtor(string portName, Type expectedException, ThrowAt throwAt) { SerialPortProperties serPortProp = new SerialPortProperties(); Debug.WriteLine($"Verifying properties where PortName={portName}"); try { using (SerialPort com = new SerialPort(portName)) { if (null != expectedException && throwAt == ThrowAt.Set) { Assert.True(false, $"Err_7212ahsdj Expected Ctor to throw {expectedException}"); } serPortProp.SetAllPropertiesToDefaults(); serPortProp.SetProperty("PortName", portName); serPortProp.VerifyPropertiesAndPrint(com); } } catch (TrueException) { // This is an inner failure throw; } catch (Exception e) { if (null == expectedException) { Assert.True(false, $"Err_07081hadnh Did not expect exception to be thrown and the following was thrown: \n{e}"); } else if (throwAt == ThrowAt.Open) { Assert.True(false, $"Err_88916adfa Expected {expectedException} to be thrown at Open and the following was thrown at Set: \n{e}"); } else if (e.GetType() != expectedException) { Assert.True(false, $"Err_90282ahwhp Expected {expectedException} to be thrown and the following was thrown: \n{e}"); } } }
public bool ReadBufferSize_Default() { SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); SerialPortProperties serPortProp = new SerialPortProperties(); bool retValue = true; Console.WriteLine("Verifying default ReadBufferSize before Open"); serPortProp.SetAllPropertiesToDefaults(); serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); retValue &= serPortProp.VerifyPropertiesAndPrint(com1); Console.WriteLine("Verifying default ReadBufferSize after Open"); com1.Open(); serPortProp = new SerialPortProperties(); serPortProp.SetAllPropertiesToOpenDefaults(); serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); retValue &= serPortProp.VerifyPropertiesAndPrint(com1); Console.WriteLine("Verifying default ReadBufferSize after Close"); com1.Close(); serPortProp = new SerialPortProperties(); serPortProp.SetAllPropertiesToDefaults(); serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); retValue &= serPortProp.VerifyPropertiesAndPrint(com1); if (!retValue) { Console.WriteLine("Err_001!!! Verifying default ReadBufferSize FAILED"); } if (com1.IsOpen) { com1.Close(); } return(retValue); }
public void OpenTwoInstances() { using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) { SerialPortProperties serPortProp1 = new SerialPortProperties(); SerialPortProperties serPortProp2 = new SerialPortProperties(); Debug.WriteLine("Verifying calling Open() on two instances of SerialPort"); serPortProp1.SetAllPropertiesToOpenDefaults(); serPortProp1.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); serPortProp2.SetAllPropertiesToDefaults(); serPortProp2.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); com1.Open(); Assert.Throws <UnauthorizedAccessException>(() => com2.Open()); serPortProp1.VerifyPropertiesAndPrint(com1); serPortProp2.VerifyPropertiesAndPrint(com2); } }
private bool VerifyExceptionAtOpen(SerialPort com, int receivedBytesThreshold, System.Type expectedException) { int origReceivedBytesThreshold = com.ReceivedBytesThreshold; bool retValue = true; SerialPortProperties serPortProp = new SerialPortProperties(); serPortProp.SetAllPropertiesToDefaults(); serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); try { com.ReceivedBytesThreshold = receivedBytesThreshold; if (null != expectedException) { Console.WriteLine("ERROR!!! Expected Open() to throw {0} and nothing was thrown", expectedException); retValue = false; } } catch (System.Exception e) { if (null == expectedException) { Console.WriteLine("ERROR!!! Expected Open() NOT to throw an exception and {0} was thrown", e.GetType()); retValue = false; } else if (e.GetType() != expectedException) { Console.WriteLine("ERROR!!! Expected Open() throw {0} and {1} was thrown", expectedException, e.GetType()); retValue = false; } } retValue &= serPortProp.VerifyPropertiesAndPrint(com); com.ReceivedBytesThreshold = origReceivedBytesThreshold; return(retValue); }
public bool OpenClose_Properties() { SerialPort com = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); bool retValue = true; SerialPortProperties serPortProp = new SerialPortProperties(); Console.WriteLine("Verifying Properites after calling Open() and BaseStream.Close()"); com.Open(); com.BaseStream.Close(); serPortProp.SetAllPropertiesToDefaults(); serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); retValue &= serPortProp.VerifyPropertiesAndPrint(com); if (!retValue) { Console.WriteLine("Err_001!!! erifying Properites after calling Open() and BaseStream.Close() FAILED"); } return(retValue); }
public bool CDHolding_Default() { SerialPort com1 = new SerialPort(); SerialPortProperties serPortProp = new SerialPortProperties(); bool retValue = true; Console.WriteLine("Verifying default CDHolding before Open"); serPortProp.SetAllPropertiesToDefaults(); retValue &= serPortProp.VerifyPropertiesAndPrint(com1); if (!retValue) { Console.WriteLine("Err_001!!! Verifying default CDHolding before Open FAILED"); } if (com1.IsOpen) { com1.Close(); } return(retValue); }
public bool BaseStream_Default() { SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); SerialPortProperties serPortProp = new SerialPortProperties(); bool retValue = true; Console.WriteLine("Verifying default BaseStream"); serPortProp.SetAllPropertiesToDefaults(); serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); retValue &= serPortProp.VerifyPropertiesAndPrint(com1); if (!retValue) { Console.WriteLine("Err_001!!! Verifying default BaseStream FAILED"); } if (com1.IsOpen) { com1.Close(); } return(retValue); }
private void VerifyExceptionAtOpen(SerialPort com, string portName, ThrowAt throwAt, Type[] expectedExceptions) { string origPortName = com.PortName; SerialPortProperties serPortProp = new SerialPortProperties(); if (null != expectedExceptions && 0 < expectedExceptions.Length) { serPortProp.SetAllPropertiesToDefaults(); } else { serPortProp.SetAllPropertiesToOpenDefaults(); } if (ThrowAt.Open == throwAt) { serPortProp.SetProperty("PortName", portName); } else { serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); } try { com.PortName = portName; if (ThrowAt.Open == throwAt) { com.Open(); } if (null != expectedExceptions && 0 < expectedExceptions.Length) { Fail("ERROR!!! Expected Open() to throw "); for (int i = 0; i < expectedExceptions.Length; ++i) { Console.Write(expectedExceptions[i] + " "); } Debug.WriteLine(" and nothing was thrown"); } } catch (Exception e) { if (null == expectedExceptions || 0 == expectedExceptions.Length) { Fail("ERROR!!! Expected Open() NOT to throw an exception and the following was thrown:\n{0}", e); } else { bool exceptionFound = false; Type actualExceptionType = e.GetType(); for (int i = 0; i < expectedExceptions.Length; ++i) { if (actualExceptionType == expectedExceptions[i]) { exceptionFound = true; break; } } if (exceptionFound) { Debug.WriteLine("Caught expected exception:\n{0}", e.GetType()); } else { Fail("ERROR!!! Expected Open() throw "); for (int i = 0; i < expectedExceptions.Length; ++i) { Console.Write(expectedExceptions[i] + " "); } Debug.WriteLine(" and the following was thrown:\n{0}", e); } } } serPortProp.VerifyPropertiesAndPrint(com); com.PortName = origPortName; }
public bool OpenFillBuffersClose() { SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName); SerialPortProperties serPortProp = new SerialPortProperties(); bool retValue = true; Console.WriteLine("Calling Open(), fill both trasmit and receive buffers, call Close()"); serPortProp.SetAllPropertiesToOpenDefaults(); serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); //Setting com1 to use Handshake so we can fill read buffer com1.Handshake = Handshake.RequestToSend; com1.Open(); com2.Open(); //BeginWrite is used so we can fill the read buffer then go onto to verify com1.BaseStream.BeginWrite(new Byte[numWriteBytes], 0, numWriteBytes, null, null); com2.Write(new Byte[numReadBytes], 0, numReadBytes); System.Threading.Thread.Sleep(500); serPortProp.SetProperty("Handshake", Handshake.RequestToSend); serPortProp.SetProperty("BytesToWrite", numWriteBytes); serPortProp.SetProperty("BytesToRead", numReadBytes); Console.WriteLine("Verifying properties after port is open and bufferes have been filled"); retValue &= serPortProp.VerifyPropertiesAndPrint(com1); com1.Handshake = Handshake.None; com1.BaseStream.Close(); serPortProp.SetAllPropertiesToDefaults(); serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); Console.WriteLine("Verifying properties after port has been closed"); retValue &= serPortProp.VerifyPropertiesAndPrint(com1); try { com1.Open(); } catch (System.Exception e) { Console.WriteLine("ERROR!!! Open() threw {0}", e.GetType()); retValue = false; } serPortProp.SetAllPropertiesToOpenDefaults(); serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); Console.WriteLine("Verifying properties after port has been opened again"); retValue &= serPortProp.VerifyPropertiesAndPrint(com1); try { if (com1.IsOpen) { com1.Close(); } } catch (Exception) { } if (com2.IsOpen) { com2.Close(); } if (!retValue) { Console.WriteLine("Err_002!!! Verifying calling Open(), fill both trasmit and receive buffers, call Close() FAILED"); } return(retValue); }