-
Notifications
You must be signed in to change notification settings - Fork 199
Events raised by SerialPortStream
There are three events in the system, DataReceived, ErrorReceived and PinChanged. All events are programmed so they are serialized (no two events shall be called in your application software simultaneously). Events do occur however on different threads to your main program, so if you are updating elements of your UI, you should be careful to marshal your UI updates via the Control.Invoke methods.
As a general guideline, you should not perform too much logic in your event handler code. While it will not stop reception of new data and you could theoretically spend a long period of time in your event (e.g. 2 seconds or so), no new event will be raised until your existing event handlers are finished.
This is by design, to simply users code. If events were to happen truly asynchronously, the user would have to ensure that access to the SerialPortStream object is serialized by using the C# lock construct or something similar (the SerialPortStream is not generally threadsafe).
When data is avalable to read in the read buffer, the DataReceived event is triggered.
If an EOF character (0x1A) is found, the DataReceived event is triggered with the enum value SerialData.Eof. The trigger of this event is independent of the ReceivedBytesThreshold. This event is raised when the serial driver indicates the event EV_RXFLAG and the next read operation is finished. It could be possible that the EV_RXFLAG is triggered and a ReadFile() operation occurs that doesn't read in the data, but occurs shortly after. So on trigger of this event, it might not necessarily be that an Eof character is read by your buffer.
The enum value SerialData.Chars is given in the event every time a ReadFile() operation is complete. It is indirectly related to the event EV_RXCHAR, but indicates that new data is available in the read buffer. This event is raised independently if there was previously data in the read buffer or not, so you can use it with methods like ReadTo().
On the reception of each character (or on the EV_ERR event from the driver) the serial port error status is checked. if there is an error detected (which is compatible with the MS implementation), this event shall be raised.
Defined events are:
- SerialError.RXOver - Buffer has reached 80% full (CE_RXOVER)
- SerialError.Overrun - Buffer overrun has occurred in the driver, resulting in lost bytes (CE_OVERRUN)
- SerialError.RXParity - Parity error has occurred (CE_RXPARITY)
- SerialError.Frame - A framing error has been detected (CE_FRAME)
- SerialError.TXFull - Transmit buffer is full (CE_TXFULL)
If the driver detects a state change of the modem pins, an event is raised
Defined events are:
- PinChanged.CtsChanged - The CTS pin has changed. Use CtsHolding to get the state
- PinChanged.DsrChanged - The DSR pin has changed. Use DsrHolding to get the state
- PinChanged.CDChanged - The Carrier Detect (CD) pin has changed. Use CDHolding to get the state
- PinChanged.Break - A break condition has been detected
- PinChanged.Ring - The Ring Indicator pin has changed. Use RingHolding to get the state.