Exemplo n.º 1
0
        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);
                }
        }
Exemplo n.º 2
0
    /*
     *  public bool ErrorEvent_TXFull()
     *  {
     *          SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
     *          SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName);
     *          ErrorEventHandler errEventHandler = new ErrorEventHandler(com1);
     *          bool retValue = true;
     *          int elapsedTime;
     *
     *          Console.WriteLine("Verifying TXFull event");
     *
     *          com1.Handshake = Handshake.RequestToSend;
     *          com1.Open();
     *          com2.Open();
     *
     *          com1.ErrorEvent += new SerialErrorEventHandler(errEventHandler.HandleEvent);
     *          com1.BaseStream.BeginWrite(new byte[32767], 0, 32767, null, null);
     *          elapsedTime = 0;
     *
     *          while(1 > errEventHandler.NumEventsHandled && elapsedTime < MAX_TIME_WAIT) {
     *                  System.Threading.Thread.Sleep(ITERATION_TIME_WAIT);
     *                  elapsedTime += ITERATION_TIME_WAIT;
     *          }
     *
     *          retValue &= errEventHandler.Validate(SerialErrors.TxFull, com1.ReceivedBytesThreshold, 0);
     *
     *          if(!retValue) {
     *                  Console.WriteLine("Err_001!!! Verifying TXFull event FAILED");
     *          }
     *
     *          if(com1.IsOpen)
     *                  com1.Close();
     *
     *          if(com2.IsOpen)
     *                  com2.Close();
     *
     *          return retValue;
     *  }
     */
    public bool ErrorEvent_RxOver()
    {
        SerialPort        com1            = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        SerialPort        com2            = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName);
        ErrorEventHandler errEventHandler = new ErrorEventHandler(com1);
        bool retValue = true;

        Console.WriteLine("Verifying RxOver event");

        com1.Handshake = Handshake.RequestToSend;
        com1.BaudRate  = 115200;
        com2.BaudRate  = 115200;
        com1.Open();
        com2.Open();

        //This might not be necessary but it will clear the RTS pin when the buffer is too full
        com1.Handshake = Handshake.RequestToSend;

        com1.ErrorReceived += new SerialErrorReceivedEventHandler(errEventHandler.HandleEvent);

        //This is overkill should find a more reasonable ammount of bytes to write
        com2.BaseStream.Write(new byte[32767], 0, 32767);

        if (!errEventHandler.WaitForEvent(MAX_TIME_WAIT, 1))
        {
            Console.WriteLine("Err_298292haid Event never occured");
            retValue = false;
        }

        while (0 < errEventHandler.NumEventsHandled)
        {
            if (!errEventHandler.Validate(SerialError.RXOver, -1))
            {
                Console.WriteLine("Err_2929ajidz!!! Expected all errors to be RXOver but at least one is:{0}", errEventHandler.EventType[0]);
                retValue = false;
            }
        }

        if (!retValue)
        {
            Console.WriteLine("Err_002!!! Verifying RxOver event FAILED");
        }

        lock (com1)
        {
            if (com1.IsOpen)
            {
                com1.Close();
            }
        }

        if (com2.IsOpen)
        {
            com2.Close();
        }

        return(retValue);
    }
Exemplo n.º 3
0
    /*
     *  public bool ErrorEvent_Overrun()
     *  {
     *      SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
     *      SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName);
     *      ErrorEventHandler errEventHandler = new ErrorEventHandler(com1);
     *      bool retValue = true;
     *      int elapsedTime;
     *
     *      Console.WriteLine("Verifying Overrun event");
     *
     *      com1.Handshake = Handshake.RequestToSend;
     *      com1.BaudRate = 115200;
     *      com2.BaudRate = 115200;
     *      com1.Open();
     *      com2.Open();
     *
     *      com1.ErrorEvent += new SerialErrorEventHandler(errEventHandler.HandleEvent);
     *      com2.BaseStream.Write(new byte[32767], 0, 32767);
     *
     *      elapsedTime = 0;
     *
     *      while(1 > errEventHandler.NumEventsHandled && elapsedTime < MAX_TIME_WAIT) {
     *          System.Threading.Thread.Sleep(ITERATION_TIME_WAIT);
     *          elapsedTime += ITERATION_TIME_WAIT;
     *      }
     *
     *      retValue &= errEventHandler.Validate(SerialErrors.Overrun, com1.ReceivedBytesThreshold, 0);
     *
     *      if(!retValue) {
     *          Console.WriteLine("Err_003!!! Verifying Overrun event FAILED");
     *      }
     *
     *      if(com1.IsOpen)
     *          com1.Close();
     *
     *      if(com2.IsOpen)
     *          com2.Close();
     *
     *      return retValue;
     *  }
     */
    public bool ErrorEvent_RxParity()
    {
        SerialPort        com1            = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        SerialPort        com2            = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName);
        ErrorEventHandler errEventHandler = new ErrorEventHandler(com1);
        bool retValue = true;

        Console.WriteLine("Verifying RxParity event");

        com1.DataBits = 7;
        com1.Parity   = Parity.Mark;

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

        com1.ErrorReceived += new SerialErrorReceivedEventHandler(errEventHandler.HandleEvent);

        for (int i = 0; i < NUM_TRYS; i++)
        {
            Console.WriteLine("Verifying RxParity event try: {0}", i);

            com2.BaseStream.Write(new byte[8], 0, 8);
            errEventHandler.WaitForEvent(MAX_TIME_WAIT, 1);

            while (0 < errEventHandler.NumEventsHandled)
            {
                if (!errEventHandler.Validate(SerialError.RXParity, -1))
                {
                    Console.WriteLine("Err_2929ajidz!!! Expected all errors to be RXParity but at least one is:{0}", errEventHandler.EventType[0]);
                    retValue = false;
                }
            }
        }

        if (!retValue)
        {
            Console.WriteLine("Err_004!!! Verifying RxParity event FAILED");
        }

        lock (com1)
        {
            if (com1.IsOpen)
            {
                com1.Close();
            }
        }

        if (com2.IsOpen)
        {
            com2.Close();
        }

        return(retValue);
    }
Exemplo n.º 4
0
        public void ErrorEvent_Frame()
        {
            using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
                using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName))
                {
                    ErrorEventHandler errEventHandler = new ErrorEventHandler(com1);
                    byte[]            frameErrorBytes = new byte[1];
                    Random            rndGen          = new Random();

                    Debug.WriteLine("Verifying Frame event");
                    com1.DataBits = 7;

                    //com1.StopBits = StopBits.Two;
                    com1.Open();
                    com2.Open();

                    com1.ErrorReceived += errEventHandler.HandleEvent;

                    for (int i = 0; i < frameErrorBytes.Length; i++)
                    {
                        frameErrorBytes[i] = (byte)rndGen.Next(0, 256);
                    }

                    //This should cause a frame error since the 8th bit is not set
                    //and com1 is set to 7 data bits ao the 8th bit will +12v where
                    //com1 expects the stop bit at the 8th bit to be -12v
                    frameErrorBytes[0] = 0x01;

                    for (int i = 0; i < NUM_TRYS; i++)
                    {
                        Debug.WriteLine("Verifying Frame event try: {0}", i);

                        com2.BaseStream.Write(frameErrorBytes, 0, 1);
                        errEventHandler.WaitForEvent(MAX_TIME_WAIT, 1);

                        while (0 < errEventHandler.NumEventsHandled)
                        {
                            errEventHandler.Validate(SerialError.Frame, -1);
                        }
                    }

                    lock (com1)
                    {
                        if (com1.IsOpen)
                        {
                            com1.Close();
                        }
                    }
                }
        }
Exemplo n.º 5
0
    public bool Thread_In_ErrorEvent()
    {
        SerialPort        com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        SerialPort        com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName);
        ErrorEventHandler errorEventHandler = new ErrorEventHandler(com1, false, true);
        Thread            closeThread       = new Thread(delegate() { com1.Close(); });
        bool retValue = true;

        Console.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 += 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);

        if (!errorEventHandler.WaitForEvent(MAX_TIME_WAIT, 1))
        {
            Console.WriteLine("Err_215887ajeid Expected 1 ErrorEvents to be fired and only {0} occured", errorEventHandler.NumEventsHandled);
            retValue = false;
        }

        closeThread.Start();
        Thread.Sleep(5000);

        errorEventHandler.ResumeHandleEvent();
        closeThread.Join();

        if (com1.IsOpen)
        {
            com1.Close();
        }

        if (com2.IsOpen)
        {
            com2.Close();
        }

        return(retValue);
    }
Exemplo n.º 6
0
        public void ErrorEvent_RxOver()
        {
            using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
                using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName))
                {
                    ErrorEventHandler errEventHandler = new ErrorEventHandler(com1);

                    Debug.WriteLine("Verifying RxOver event");

                    com1.Handshake = Handshake.RequestToSend;
                    com1.BaudRate  = 115200;
                    com2.BaudRate  = 115200;
                    com1.Open();
                    com2.Open();

                    //This might not be necessary but it will clear the RTS pin when the buffer is too full
                    com1.Handshake = Handshake.RequestToSend;

                    com1.ErrorReceived += errEventHandler.HandleEvent;

                    //This is overkill should find a more reasonable amount of bytes to write
                    com2.BaseStream.Write(new byte[32767], 0, 32767);

                    Assert.True(errEventHandler.WaitForEvent(MAX_TIME_WAIT, 1), "Event never occurred");

                    while (0 < errEventHandler.NumEventsHandled)
                    {
                        errEventHandler.Validate(SerialError.RXOver, -1);
                    }

                    lock (com1)
                    {
                        if (com1.IsOpen)
                        {
                            com1.Close();
                        }
                    }
                }
        }
Exemplo n.º 7
0
        public void ErrorEvent_RxParity()
        {
            using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
                using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName))
                {
                    ErrorEventHandler errEventHandler = new ErrorEventHandler(com1);

                    Debug.WriteLine("Verifying RxParity event");

                    com1.DataBits = 7;
                    com1.Parity   = Parity.Mark;

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

                    com1.ErrorReceived += errEventHandler.HandleEvent;

                    for (int i = 0; i < NUM_TRYS; i++)
                    {
                        Debug.WriteLine("Verifying RxParity event try: {0}", i);

                        com2.BaseStream.Write(new byte[8], 0, 8);
                        Assert.True(errEventHandler.WaitForEvent(MAX_TIME_WAIT, 1));

                        while (0 < errEventHandler.NumEventsHandled)
                        {
                            errEventHandler.Validate(SerialError.RXParity, -1);
                        }
                    }

                    lock (com1)
                    {
                        if (com1.IsOpen)
                        {
                            com1.Close();
                        }
                    }
                }
        }
Exemplo n.º 8
0
        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();
                }
        }
Exemplo n.º 9
0
    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");
                }
            }
    }
Exemplo n.º 10
0
    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);
    }
Exemplo n.º 11
0
    public bool ErrorEvent_Frame()
    {
        SerialPort        com1            = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        SerialPort        com2            = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName);
        ErrorEventHandler errEventHandler = new ErrorEventHandler(com1);
        bool retValue = true;

        byte[] frameErrorBytes = new byte[1];
        Random rndGen          = new Random();

        Console.WriteLine("Verifying Frame event");
        com1.DataBits = 7;

        //com1.StopBits = StopBits.Two;
        com1.Open();
        com2.Open();

        com1.ErrorReceived += new SerialErrorReceivedEventHandler(errEventHandler.HandleEvent);

        for (int i = 0; i < frameErrorBytes.Length; i++)
        {
            frameErrorBytes[i] = (byte)rndGen.Next(0, 256);
        }

        //This should cause a fame error since the 8th bit is not set
        //and com1 is set to 7 data bits ao the 8th bit will +12v where
        //com1 expects the stop bit at the 8th bit to be -12v
        frameErrorBytes[0] = 0x01;

        for (int i = 0; i < NUM_TRYS; i++)
        {
            Console.WriteLine("Verifying Frame event try: {0}", i);

            com2.BaseStream.Write(frameErrorBytes, 0, 1);
            errEventHandler.WaitForEvent(MAX_TIME_WAIT, 1);


            while (0 < errEventHandler.NumEventsHandled)
            {
                if (!errEventHandler.Validate(SerialError.Frame, -1))
                {
                    Console.WriteLine("Err_25097qpaua!!! Expected all errors to be Frame but at least one is:{0}", errEventHandler.EventType[0]);
                    retValue = false;
                }
            }
        }

        if (!retValue)
        {
            Console.WriteLine("Err_005!!! Verifying Frame event FAILED");
        }

        lock (com1)
        {
            if (com1.IsOpen)
            {
                com1.Close();
            }
        }

        if (com2.IsOpen)
        {
            com2.Close();
        }

        return(retValue);
    }