public void PinChangedEvent_DsrChanged() { using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName)) { PinChangedEventHandler eventHandler = new PinChangedEventHandler(com1); // Some null-modem cables have a connection between CD and CSR/CTR, so we need to discard CDChanged events eventHandler.EventFilter = eventType => eventType != SerialPinChange.CDChanged; Debug.WriteLine("Verifying DsrChanged event"); com1.PinChanged += eventHandler.HandleEvent; com1.Open(); com2.Open(); for (int i = 0; i < NUM_TRYS; i++) { Debug.WriteLine("Verifying when DtrEnable set to true on remote port {0}", i); com2.DtrEnable = true; Assert.True(eventHandler.WaitForEvent(MAX_TIME_WAIT, 1), "Initial event missing"); eventHandler.Validate(SerialPinChange.DsrChanged, 0); Assert.Equal(0, eventHandler.NumEventsHandled); com2.DtrEnable = false; eventHandler.WaitForEvent(MAX_TIME_WAIT, 1); eventHandler.Validate(SerialPinChange.DsrChanged, 0); Assert.Equal(0, eventHandler.NumEventsHandled); } } }
public void PinChangedEvent_CtsChanged() { using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName)) { PinChangedEventHandler eventHandler = new PinChangedEventHandler(com1); Debug.WriteLine("Verifying CtsChanged event"); com1.PinChanged += eventHandler.HandleEvent; com1.Open(); com2.Open(); for (int i = 0; i < NUM_TRYS; i++) { Debug.WriteLine("Verifying when RtsEnable set to true on remote port try: {0}", i); com2.RtsEnable = true; Assert.True(eventHandler.WaitForEvent(MAX_TIME_WAIT, 1), "Initial event missing"); eventHandler.Validate(SerialPinChange.CtsChanged, 0); Assert.Equal(0, eventHandler.NumEventsHandled); com2.RtsEnable = false; eventHandler.WaitForEvent(MAX_TIME_WAIT, 1); eventHandler.Validate(SerialPinChange.CtsChanged, 0); Assert.Equal(0, eventHandler.NumEventsHandled); } } }
public bool PinChangedEvent_CtsChanged() { SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName); PinChangedEventHandler eventHandler = new PinChangedEventHandler(com1); bool retValue = true; Console.WriteLine("Verifying CtsChanged event"); com1.PinChanged += new SerialPinChangedEventHandler(eventHandler.HandleEvent); com1.Open(); com2.Open(); for (int i = 0; i < NUM_TRYS; i++) { com2.RtsEnable = true; Console.WriteLine("Verifying when RtsEnable set to true on remote port try: {0}", i); eventHandler.WaitForEvent(MAX_TIME_WAIT, 1); if (!eventHandler.Validate(SerialPinChange.CtsChanged, 0)) { Console.WriteLine("Err_1351adsf!!! CtsChanged event not fired"); retValue = false; } if (0 != eventHandler.NumEventsHandled) { Console.WriteLine("Err_4217qyza!!! unexpected event fired"); retValue = false; } com2.RtsEnable = false; eventHandler.WaitForEvent(MAX_TIME_WAIT, 1); if (!eventHandler.Validate(SerialPinChange.CtsChanged, 0)) { Console.WriteLine("Err_24597aqqoo!!! CtsChanged event not fired"); retValue = false; } if (0 != eventHandler.NumEventsHandled) { Console.WriteLine("Err_4309714qaoya!!! unexpected event fired"); retValue = false; } } if (com1.IsOpen) { com1.Close(); } if (com2.IsOpen) { com2.Close(); } return(retValue); }
public void PinChangedEvent_Multiple() { using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName)) { PinChangedEventHandler eventHandler = new PinChangedEventHandler(com1); SerialPinChangedEventHandler pinchangedEventHandler = eventHandler.HandleEvent; Debug.WriteLine("Verifying multiple PinChangedEvents"); com1.PinChanged += pinchangedEventHandler; com1.Open(); com2.Open(); com2.BreakState = true; System.Threading.Thread.Sleep(100); com2.DtrEnable = true; System.Threading.Thread.Sleep(100); com2.RtsEnable = true; //On my machine it looks like the CDChanged event is getting fired as //well as the three that we expect here. So we will wait until 4 events //are fired. If other machines do not fire this event the test will //still pass we will just wait MAX_TIME_WAITms. eventHandler.WaitForEvent(MAX_TIME_WAIT, 4); if (!eventHandler.Validate(SerialPinChange.Break, 0)) { Fail("Verifying Break State FAILED"); } if (!eventHandler.Validate(SerialPinChange.DsrChanged, 0)) { Fail("Verifying DsrChanged FAILED"); } if (!eventHandler.Validate(SerialPinChange.CtsChanged, 0)) { Fail("Verifying CtsCahnged FAILED"); } com1.PinChanged -= pinchangedEventHandler; com2.BreakState = false; com2.DtrEnable = false; com2.RtsEnable = false; } }
public void PinChangedEvent_Break() { using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName)) { PinChangedEventHandler eventHandler = new PinChangedEventHandler(com1); Debug.WriteLine("Verifying Break event"); com1.PinChanged += eventHandler.HandleEvent; com1.Open(); com2.Open(); for (int i = 0; i < NUM_TRYS; i++) { Debug.WriteLine("Verifying when Break set to true on remote port try: {0}", i); com2.BreakState = true; Assert.True(eventHandler.WaitForEvent(MAX_TIME_WAIT, 1), "Initial event missing"); eventHandler.Validate(SerialPinChange.Break, 0); Assert.Equal(0, eventHandler.NumEventsHandled); com2.BreakState = false; // This will always time-out, because we don't expect an event here eventHandler.WaitForEvent(MAX_TIME_WAIT, 1); Assert.Equal(0, eventHandler.NumEventsHandled); } } }
public void PinChangedEvent_Break() { using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName)) { PinChangedEventHandler eventHandler = new PinChangedEventHandler(com1); Debug.WriteLine("Verifying Break event"); com1.PinChanged += eventHandler.HandleEvent; com1.Open(); com2.Open(); for (int i = 0; i < NUM_TRYS; i++) { com2.BreakState = true; Fail("Verifying when Break set to true on remote port try: {0}", i); eventHandler.WaitForEvent(MAX_TIME_WAIT, 1); if (!eventHandler.Validate(SerialPinChange.Break, 0)) { Fail("Err_67894ahlead!!! Break event not fired"); } Assert.Equal(0, eventHandler.NumEventsHandled); com2.BreakState = false; eventHandler.WaitForEvent(MAX_TIME_WAIT, 1); Assert.Equal(0, eventHandler.NumEventsHandled); } } }
public void PinChangedEvent_DsrChanged() { using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName)) { PinChangedEventHandler eventHandler = new PinChangedEventHandler(com1); Debug.WriteLine("Verifying DsrChanged event"); com1.PinChanged += eventHandler.HandleEvent; com1.Open(); com2.Open(); for (int i = 0; i < NUM_TRYS; i++) { com2.DtrEnable = true; Fail("Verifying when DtrEnable set to true on remote port {0}", i); eventHandler.WaitForEvent(MAX_TIME_WAIT, 2); if (!eventHandler.Validate(SerialPinChange.DsrChanged, 0)) { Fail("Err_5239aopz!!! DsrChanged event not fired"); } eventHandler.Validate(SerialPinChange.CDChanged, 0); //This is necessary becuase DSR pin is connected CTS pin in my null cable Assert.Equal(0, eventHandler.NumEventsHandled); com2.DtrEnable = false; eventHandler.WaitForEvent(MAX_TIME_WAIT, 2); if (!eventHandler.Validate(SerialPinChange.DsrChanged, 0)) { Fail("Err_1520qhoa!!! DsrChanged event not fired"); } eventHandler.Validate(SerialPinChange.CDChanged, 0); //This is necessary becuase DSR pin is connected CTS pin in my null cable Assert.Equal(0, eventHandler.NumEventsHandled); } } }
public void PinChangedEvent_CtsChanged() { using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName)) { PinChangedEventHandler eventHandler = new PinChangedEventHandler(com1); Debug.WriteLine("Verifying CtsChanged event"); com1.PinChanged += eventHandler.HandleEvent; com1.Open(); com2.Open(); for (int i = 0; i < NUM_TRYS; i++) { com2.RtsEnable = true; Debug.WriteLine("Verifying when RtsEnable set to true on remote port try: {0}", i); eventHandler.WaitForEvent(MAX_TIME_WAIT, 1); if (!eventHandler.Validate(SerialPinChange.CtsChanged, 0)) { Fail("Err_1351adsf!!! CtsChanged event not fired"); } if (0 != eventHandler.NumEventsHandled) { Fail("Err_4217qyza!!! unexpected event fired"); } com2.RtsEnable = false; eventHandler.WaitForEvent(MAX_TIME_WAIT, 1); if (!eventHandler.Validate(SerialPinChange.CtsChanged, 0)) { Fail("Err_24597aqqoo!!! CtsChanged event not fired"); } if (0 != eventHandler.NumEventsHandled) { Fail("Err_4309714qaoya!!! unexpected event fired"); } } } }
public bool PinChangedEvent_Break() { SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName); PinChangedEventHandler eventHandler = new PinChangedEventHandler(com1); bool retValue = true; Console.WriteLine("Verifying Break event"); com1.PinChanged += new SerialPinChangedEventHandler(eventHandler.HandleEvent); com1.Open(); com2.Open(); for (int i = 0; i < NUM_TRYS; i++) { com2.BreakState = true; Console.WriteLine("Verifying when Break set to true on remote port try: {0}", i); eventHandler.WaitForEvent(MAX_TIME_WAIT, 1); if (!eventHandler.Validate(SerialPinChange.Break, 0)) { Console.WriteLine("Err_67894ahlead!!! Break event not fired"); retValue = false; } if (0 != eventHandler.NumEventsHandled) { Console.WriteLine("Err_5784dahed!!! unexpected events({0}) fired ", eventHandler.NumEventsHandled); retValue = false; } com2.BreakState = false; eventHandler.WaitForEvent(MAX_TIME_WAIT, 1); if (0 != eventHandler.NumEventsHandled) { Console.WriteLine("Err_56189awjhaos!!! unexpected event fired"); retValue = false; } } if (com1.IsOpen) { com1.Close(); } if (com2.IsOpen) { com2.Close(); } return(retValue); }
public void PinChangedEvent_Multiple() { using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName)) { PinChangedEventHandler eventHandler = new PinChangedEventHandler(com1); // Some null-modem cables have a connection between CD and CSR/CTR, so we need to discard CDChanged events eventHandler.EventFilter = eventType => eventType != SerialPinChange.CDChanged; SerialPinChangedEventHandler pinchangedEventHandler = eventHandler.HandleEvent; Debug.WriteLine("Verifying multiple PinChangedEvents"); com1.PinChanged += pinchangedEventHandler; com1.Open(); com2.Open(); com2.BreakState = true; Thread.Sleep(100); com2.DtrEnable = true; Thread.Sleep(100); com2.RtsEnable = true; eventHandler.WaitForEvent(MAX_TIME_WAIT, 3); eventHandler.Validate(SerialPinChange.Break, 0); eventHandler.Validate(SerialPinChange.DsrChanged, 0); eventHandler.Validate(SerialPinChange.CtsChanged, 0); com1.PinChanged -= pinchangedEventHandler; com2.BreakState = false; com2.DtrEnable = false; com2.RtsEnable = false; } }
public void EventHandlers_CalledSerially() { using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName)) { PinChangedEventHandler pinChangedEventHandler = new PinChangedEventHandler(com1, false, true); ReceivedEventHandler receivedEventHandler = new ReceivedEventHandler(com1, false, true); ErrorEventHandler errorEventHandler = new ErrorEventHandler(com1, false, true); int numPinChangedEvents = 0, numErrorEvents = 0, numReceivedEvents = 0; int iterationWaitTime = 100; /*************************************************************** * Scenario Description: All of the event handlers should be called sequentially never * at the same time on multiple thread. Basically we will block each event handler caller thread and verify * that no other thread is in another event handler * ***************************************************************/ Debug.WriteLine("Verifying that event handlers are called serially"); com1.WriteTimeout = 5000; com2.WriteTimeout = 5000; com1.Open(); com2.Open(); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); com1.PinChanged += pinChangedEventHandler.HandleEvent; com1.DataReceived += receivedEventHandler.HandleEvent; 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); Debug.Print("ERROREvent Triggered"); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); //This should cause PinChangedEvent to be fired with SerialPinChanges.DsrChanged //since we are setting DtrEnable to true com2.DtrEnable = true; Debug.WriteLine("PinChange Triggered"); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); //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); Debug.WriteLine("RxEvent Triggered"); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); //This should cause a frame error since the 8th bit is not set, //and com1 is set to 7 data bits so the 8th bit will +12v where //com1 expects the stop bit at the 8th bit to be -12v com1.DataBits = 7; com1.Parity = Parity.None; com2.BaseStream.Write(new byte[] { 0x01 }, 0, 1); Debug.WriteLine("FrameError Triggered"); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); //This should cause PinChangedEvent to be fired with SerialPinChanges.CtsChanged //since we are setting RtsEnable to true com2.RtsEnable = true; Debug.WriteLine("PinChange Triggered"); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); //This should cause ReceivedEvent to be fired with EofReceived //since we are writing the EOF char com1.DataBits = 8; com1.Parity = Parity.None; com2.BaseStream.Write(new byte[] { 26 }, 0, 1); Debug.WriteLine("RxEOF Triggered"); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); //This should cause PinChangedEvent to be fired with SerialPinChanges.Break //since we are setting BreakState to true com2.BreakState = true; Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); bool threadFound = true; Stopwatch sw = Stopwatch.StartNew(); while (threadFound && sw.ElapsedMilliseconds < MAX_TIME_WAIT) { threadFound = false; for (int i = 0; i < MAX_TIME_WAIT / iterationWaitTime; ++i) { Debug.WriteLine("Event counts: PinChange {0}, Rx {1}, error {2}", numPinChangedEvents, numReceivedEvents, numErrorEvents); Debug.WriteLine("Waiting for pinchange event {0}ms", iterationWaitTime); if (pinChangedEventHandler.WaitForEvent(iterationWaitTime, numPinChangedEvents + 1)) { // A thread is in PinChangedEvent: verify that it is not in any other handler at the same time if (receivedEventHandler.NumEventsHandled != numReceivedEvents) { Fail("Err_191818ahied A thread is in PinChangedEvent and ReceivedEvent"); } if (errorEventHandler.NumEventsHandled != numErrorEvents) { Fail("Err_198119hjaheid A thread is in PinChangedEvent and ErrorEvent"); } ++numPinChangedEvents; pinChangedEventHandler.ResumeHandleEvent(); threadFound = true; break; } Debug.WriteLine("Waiting for rx event {0}ms", iterationWaitTime); if (receivedEventHandler.WaitForEvent(iterationWaitTime, numReceivedEvents + 1)) { // A thread is in ReceivedEvent: verify that it is not in any other handler at the same time if (pinChangedEventHandler.NumEventsHandled != numPinChangedEvents) { Fail("Err_2288ajed A thread is in ReceivedEvent and PinChangedEvent"); } if (errorEventHandler.NumEventsHandled != numErrorEvents) { Fail("Err_25158ajeiod A thread is in ReceivedEvent and ErrorEvent"); } ++numReceivedEvents; receivedEventHandler.ResumeHandleEvent(); threadFound = true; break; } Debug.WriteLine("Waiting for error event {0}ms", iterationWaitTime); if (errorEventHandler.WaitForEvent(iterationWaitTime, numErrorEvents + 1)) { // A thread is in ErrorEvent: verify that it is not in any other handler at the same time if (pinChangedEventHandler.NumEventsHandled != numPinChangedEvents) { Fail("Err_01208akiehd A thread is in ErrorEvent and PinChangedEvent"); } if (receivedEventHandler.NumEventsHandled != numReceivedEvents) { Fail("Err_1254847ajied A thread is in ErrorEvent and ReceivedEvent"); } ++numErrorEvents; errorEventHandler.ResumeHandleEvent(); threadFound = true; break; } } } if (!pinChangedEventHandler.WaitForEvent(MAX_TIME_WAIT, 3)) { Fail("Err_2288ajied Expected 3 PinChangedEvents to be fired and only {0} occured", pinChangedEventHandler.NumEventsHandled); } if (!receivedEventHandler.WaitForEvent(MAX_TIME_WAIT, 2)) { Fail("Err_122808aoeid Expected 2 ReceivedEvents to be fired and only {0} occured", receivedEventHandler.NumEventsHandled); } if (!errorEventHandler.WaitForEvent(MAX_TIME_WAIT, 2)) { Fail("Err_215887ajeid Expected 3 ErrorEvents to be fired and only {0} occured", errorEventHandler.NumEventsHandled); } //[] Verify all PinChangedEvents should have occured pinChangedEventHandler.Validate(SerialPinChange.DsrChanged, 0); pinChangedEventHandler.Validate(SerialPinChange.CtsChanged, 0); pinChangedEventHandler.Validate(SerialPinChange.Break, 0); //[] Verify all ReceivedEvent should have occured receivedEventHandler.Validate(SerialData.Chars, 0); receivedEventHandler.Validate(SerialData.Eof, 0); //[] Verify all ErrorEvents should have occured errorEventHandler.Validate(SerialError.RXParity, 0); errorEventHandler.Validate(SerialError.Frame, 0); // It's important that we close com1 BEFORE com2 (the using() block would do this the other way around normally) // This is because we have our special blocking event handlers hooked onto com1, and closing com2 is likely to // cause a pin-change event which then hangs and prevents com1 from closing. // An alternative approach would be to unhook all the event-handlers before leaving the using() block. com1.Close(); } }
public void EventHandler_ThrowsException() { using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName)) { PinChangedEventHandler pinChangedEventHandler = new PinChangedEventHandler(com1, true); ReceivedEventHandler receivedEventHandler = new ReceivedEventHandler(com1, true); ErrorEventHandler errorEventHandler = new ErrorEventHandler(com1, true); /*************************************************************** * Scenario Description: All of the event handler are going to throw and we are * going to verify that the event handler will still get called. * * We want to verify that throwing does not cause the thread calling the event * handlers to die. ***************************************************************/ Debug.WriteLine("Verifying where the event handlers throws"); com1.Open(); com2.Open(); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); com1.PinChanged += pinChangedEventHandler.HandleEvent; com1.DataReceived += receivedEventHandler.HandleEvent; 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); //This should cause PinChangedEvent to be fired with SerialPinChanges.DsrChanged //since we are setting DtrEnable to true com2.DtrEnable = true; Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); //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); //This should cause a fame error since the 8th bit is not set, //and com1 is set to 7 data bits so the 8th bit will +12v where //com1 expects the stop bit at the 8th bit to be -12v com1.DataBits = 7; com1.Parity = Parity.None; com2.BaseStream.Write(new byte[] { 0x01 }, 0, 1); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); //This should cause PinChangedEvent to be fired with SerialPinChanges.CtsChanged //since we are setting RtsEnable to true com2.RtsEnable = true; Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); //This should cause ReceivedEvent to be fired with EofReceived //since we are writing the EOF char com1.DataBits = 8; com1.Parity = Parity.None; com2.BaseStream.Write(new byte[] { 26 }, 0, 1); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); //This should cause PinChangedEvent to be fired with SerialPinChanges.Break //since we are setting BreakState to true com2.BreakState = true; Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); if (!pinChangedEventHandler.WaitForEvent(MAX_TIME_WAIT, 3)) { Fail("Err_28282haied Expected 3 PinChangedEvents to be fired and only {0} occured", pinChangedEventHandler.NumEventsHandled); } if (!receivedEventHandler.WaitForEvent(MAX_TIME_WAIT, 2)) { Fail("Err_2912hsie Expected 2 ReceivedEvents to be fired and only {0} occured", receivedEventHandler.NumEventsHandled); } if (!errorEventHandler.WaitForEvent(MAX_TIME_WAIT, 2)) { Fail("Err_191291jaied Expected 3 ErrorEvents to be fired and only {0} occured", errorEventHandler.NumEventsHandled); } //[] Verify all PinChangedEvents should have occured if (!pinChangedEventHandler.Validate(SerialPinChange.DsrChanged, 0)) { Fail("Err_24597aqqoo!!! PinChangedEvent DsrChanged event not fired"); } if (!pinChangedEventHandler.Validate(SerialPinChange.CtsChanged, 0)) { Fail("Err_144754ajied!!! PinChangedEvent CtsChanged event not fired"); } if (!pinChangedEventHandler.Validate(SerialPinChange.Break, 0)) { Fail("Err_15488ahied!!! PinChangedEvent Break event not fired"); } //[] Verify all ReceivedEvent should have occured if (!receivedEventHandler.Validate(SerialData.Chars, 0)) { Fail("Err_54552aheied!!! ReceivedEvent ReceivedChars event not fired"); } if (!receivedEventHandler.Validate(SerialData.Eof, 0)) { Fail("Err_4588ajeod!!! ReceivedEvent EofReceived event not fired"); } //[] Verify all ErrorEvents should have occured if (!errorEventHandler.Validate(SerialError.RXParity, 0)) { Fail("Err_1051ajheid!!! ErrorEvent RxParity event not fired"); } if (!errorEventHandler.Validate(SerialError.Frame, 0)) { Fail("Err_61805aheud!!! ErrorEvent Frame event not fired"); } } }
public bool EventHandlers_CalledSerially() { SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName); PinChangedEventHandler pinChangedEventHandler = new PinChangedEventHandler(com1, false, true); ReceivedEventHandler receivedEventHandler = new ReceivedEventHandler(com1, false, true); ErrorEventHandler errorEventHandler = new ErrorEventHandler(com1, false, true); bool retValue = true; int numPinChangedEvents = 0, numErrorEvents = 0, numReceivedEvents = 0; int iterationWaitTime = 100; bool threadFound; /*************************************************************** * Scenario Description: All of the event handlers should be called sequentiall never * at the same time on multiple thread. Basically we will block a thread and verify * that no other thread is in another event handler * ***************************************************************/ Console.WriteLine("Verifying where the event handlers are called serially"); com1.Open(); com2.Open(); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); com1.PinChanged += new SerialPinChangedEventHandler(pinChangedEventHandler.HandleEvent); com1.DataReceived += new SerialDataReceivedEventHandler(receivedEventHandler.HandleEvent); com1.ErrorReceived += new SerialErrorReceivedEventHandler(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); //This should cause PinChangedEvent to be fired with SerialPinChanges.DsrChanged //since we are setting DtrEnable to true com2.DtrEnable = true; Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); //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); //This should cause a fame error since the 8th bit is not set, //and com1 is set to 7 data bits so the 8th bit will +12v where //com1 expects the stop bit at the 8th bit to be -12v com1.DataBits = 7; com1.Parity = Parity.None; com2.BaseStream.Write(new byte[] { 0x01 }, 0, 1); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); //This should cause PinChangedEvent to be fired with SerialPinChanges.CtsChanged //since we are setting RtsEnable to true com2.RtsEnable = true; Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); //This should cause ReceivedEvent to be fired with EofReceived //since we are writing the EOF char com1.DataBits = 8; com1.Parity = Parity.None; com2.BaseStream.Write(new byte[] { 26 }, 0, 1); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); //This should cause PinChangedEvent to be fired with SerialPinChanges.Break //since we are setting BreakState to true com2.BreakState = true; Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); threadFound = true; while (threadFound) { threadFound = false; for (int i = 0; i < MAX_TIME_WAIT / iterationWaitTime; ++i) { if (pinChangedEventHandler.WaitForEvent(iterationWaitTime, numPinChangedEvents + 1)) {//A thread is in PinChangedEvent verify that it is not in any other if (receivedEventHandler.NumEventsHandled != numReceivedEvents) { Console.WriteLine("Err_191818ahied A thread is in PinChangedEvent and ReceivedEvent"); retValue = false; } if (errorEventHandler.NumEventsHandled != numErrorEvents) { Console.WriteLine("Err_198119hjaheid A thread is in PinChangedEvent and ErrorEvent"); retValue = false; } ++numPinChangedEvents; pinChangedEventHandler.ResumeHandleEvent(); threadFound = true; break; } if (receivedEventHandler.WaitForEvent(iterationWaitTime, numReceivedEvents + 1)) {//A thread is in ReceivedEvent verify that it is not in any other if (pinChangedEventHandler.NumEventsHandled != numPinChangedEvents) { Console.WriteLine("Err_2288ajed A thread is in ReceivedEvent and PinChangedEvent"); retValue = false; } if (errorEventHandler.NumEventsHandled != numErrorEvents) { Console.WriteLine("Err_25158ajeiod A thread is in ReceivedEvent and ErrorEvent"); retValue = false; } ++numReceivedEvents; receivedEventHandler.ResumeHandleEvent(); threadFound = true; break; } if (errorEventHandler.WaitForEvent(iterationWaitTime, numErrorEvents + 1)) {//A thread is in ErrorEvent verify that it is not in any other if (pinChangedEventHandler.NumEventsHandled != numPinChangedEvents) { Console.WriteLine("Err_01208akiehd A thread is in ErrorEvent and PinChangedEvent"); retValue = false; } if (receivedEventHandler.NumEventsHandled != numReceivedEvents) { Console.WriteLine("Err_1254847ajied A thread is in ErrorEvent and ReceivedEvent"); retValue = false; } ++numErrorEvents; errorEventHandler.ResumeHandleEvent(); threadFound = true; break; } } } if (!pinChangedEventHandler.WaitForEvent(MAX_TIME_WAIT, 3)) { Console.WriteLine("Err_2288ajied Expected 3 PinChangedEvents to be fired and only {0} occured", pinChangedEventHandler.NumEventsHandled); retValue = false; } if (!receivedEventHandler.WaitForEvent(MAX_TIME_WAIT, 2)) { Console.WriteLine("Err_122808aoeid Expected 2 ReceivedEvents to be fired and only {0} occured", receivedEventHandler.NumEventsHandled); retValue = false; } if (!errorEventHandler.WaitForEvent(MAX_TIME_WAIT, 2)) { Console.WriteLine("Err_215887ajeid Expected 3 ErrorEvents to be fired and only {0} occured", errorEventHandler.NumEventsHandled); retValue = false; } //[] Verify all PinChangedEvents should have occured if (!pinChangedEventHandler.Validate(SerialPinChange.DsrChanged, 0)) { Console.WriteLine("Err_258087aieid!!! PinChangedEvent DsrChanged event not fired"); retValue = false; } if (!pinChangedEventHandler.Validate(SerialPinChange.CtsChanged, 0)) { Console.WriteLine("Err_5548ajhied!!! PinChangedEvent CtsChanged event not fired"); retValue = false; } if (!pinChangedEventHandler.Validate(SerialPinChange.Break, 0)) { Console.WriteLine("Err_25848ajiied!!! PinChangedEvent Break event not fired"); retValue = false; } //[] Verify all ReceivedEvent should have occured if (!receivedEventHandler.Validate(SerialData.Chars, 0)) { Console.WriteLine("Err_0211558ajoied!!! ReceivedEvent ReceivedChars event not fired"); retValue = false; } if (!receivedEventHandler.Validate(SerialData.Eof, 0)) { Console.WriteLine("Err_215588zahid!!! ReceivedEvent EofReceived event not fired"); retValue = false; } //[] Verify all ErrorEvents should have occured if (!errorEventHandler.Validate(SerialError.RXParity, 0)) { Console.WriteLine("Err_515188ahjid!!! ErrorEvent RxParity event not fired"); retValue = false; } if (!errorEventHandler.Validate(SerialError.Frame, 0)) { Console.WriteLine("Err_55874884ajie!!! ErrorEvent Frame event not fired"); retValue = false; } if (com1.IsOpen) { com1.Close(); } if (com2.IsOpen) { com2.Close(); } return(retValue); }
public bool PinChangedEvent_DsrChanged() { SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName); PinChangedEventHandler eventHandler = new PinChangedEventHandler(com1); bool retValue = true; Console.WriteLine("Verifying DsrChanged event"); com1.PinChanged += new SerialPinChangedEventHandler(eventHandler.HandleEvent); com1.Open(); com2.Open(); for (int i = 0; i < NUM_TRYS; i++) { com2.DtrEnable = true; Console.WriteLine("Verifying when DtrEnable set to true on remote port {0}", i); eventHandler.WaitForEvent(MAX_TIME_WAIT, 2); if (!eventHandler.Validate(SerialPinChange.DsrChanged, 0)) { Console.WriteLine("Err_5239aopz!!! DsrChanged event not fired"); retValue = false; } eventHandler.Validate(SerialPinChange.CDChanged, 0); //This is necessary becuase DSR pin is connected CTS pin in my null cable if (0 != eventHandler.NumEventsHandled) { Console.WriteLine("Err_1431qpzy!!! unexpected event fired"); retValue = false; } com2.DtrEnable = false; eventHandler.WaitForEvent(MAX_TIME_WAIT, 2); if (!eventHandler.Validate(SerialPinChange.DsrChanged, 0)) { Console.WriteLine("Err_1520qhoa!!! DsrChanged event not fired"); retValue = false; } eventHandler.Validate(SerialPinChange.CDChanged, 0); //This is necessary becuase DSR pin is connected CTS pin in my null cable if (0 != eventHandler.NumEventsHandled) { Console.WriteLine("Err_2500qarf!!! unexpected event fired"); retValue = false; } } if (!retValue) { Console.WriteLine("Err_002!!! Verifying DsrChanged event FAILED"); } if (com1.IsOpen) { com1.Close(); } if (com2.IsOpen) { com2.Close(); } return(retValue); }