There are quite a number of customizable properties related to the serial port. Once you've instantiated the serial port, you can configure most properties until it is opened.

Instantiation

You can set the BaudRate, DataBits, Parity and StopBits when you instantiate a new SerialPortStream object.

You can instantiate the SerialPortStream in a number of ways:

Default Constructor

SerialPortStream s = new SerialPortStream();

This sets the serial port to be undefined. The following parameters are set:
  • PortName is null (i.e. not defined). You must ensure you set this before calling Open()
  • BaudRate is 115200
  • DataBits is 8
  • Parity is Parity.None
  • ParityReplace is set to 0 (disabled)
  • StopBits is StopBits.One
  • HandShake is Handshake.None
  • TxContinueOnXoff is set to false
  • XonLimit is set to 2048
  • XOffLimit is set to 512

Defining the Port

You can provide basic parameters, such as the PortName as part of the constructor:

SerialPortStream s1 = new SerialPortStream("COM1");
SerialPortStream s2 = new SerialPortStream("COM2", 115200);
SerialPortStream s3 = new SerialPortStream("COM3", 115200, 8, Parity.None, StopBits.One);

All other properties receive their default values as in the default constructor. The serial port settings are first set when the Open() method is called.

The following properties are set regardless of Windows settings
  • HandShake is Handshake.None
  • Binary mode is set (not configurable)
  • AbortOnError is cleared (not configurable)
  • EventChar is 0x1A (not configurable)
  • EofChar is 0x1A (not configurable)
  • RtsControl is checked for consistency with the properties
  • XonChar is set to 17 (not configurable)
  • XoffChar is set to 19 (not configurable)

Serial Properties

Standard Properties

The following basic properties are provided
  • BaudRate - The baudrate of the serial port. The actual baudrate used is dependent on the driver. Usually a driver attempts to configure to the nearest baudrate within 5%. Not all baudrates are supported on all devices.
  • DataBits - Supports the standard windows databits of 5, 6, 7 or 8 bits. 16 bits is also support, but not the 16X mode.
  • StopBits - Supports the three standard stop bit settings of 1, 1.5 or 2 stop bits
  • Parity - Parity to use. Supports the Windows standard settings of None, Odd, Even and the less used Mark and Space parity.
    • ParityReplace - In case that a parity error is detected, you can set this property to provide a character that indicates a parity error. If this property is set to zero, then parity replace is disabled.

Handshaking

There are some features of handshaking that are not provided by the MS implementation. The SerialPortStream object supports RTS/CTS (hardware flow control) handshaking and Xon/Xoff (software flow control) handshaking. In addition, the SerialPortStream also supports DTR/DTS handshaking.

The property is called Handshake. You can set the following:
  • None - No handshaking
  • XON - Software Flow Control
  • RTS - Hardware Flow Control
  • DTR - Data Terminal hardware flow control
  • RTS_XON or the "or" combination RTS | XON
  • DTR_XON or the "or" combination DTR | XON
  • DTR_RTS or the "or" combination DTR | RTS
  • DTR_RTS_XON or the "or" combination DTR | RTS | XON

Software Flow Control

The Xon and Xoff characters are fixed at 17 and 19 respectively. You can define when the Xon character will be sent by the XOnLimit property. The Xon character is sent when the driver buffer DriverInQueue is within XOnLimit bytes of being empty. The Xoff character is sent when the driver buffer DriverInQueue is within XOffLimit bytes of being full.

There is also an interesting feature provided by Windows, the TxContinueOnXoff property. Setting this to true allows transmission of data after the XON character has been sent (which is normally the case). However, there are some embedded devices that take the reception of any character to imply XOFF (not just the XOFF character). So by setting this property to false actually disables the transmission of data also when XOFF is sent.

This feature is implemented by the Windows driver subsystem.

Less Used Properties

Less common serial port properties supported are:
  • DiscardNull - You should generally not use this setting, but it is available if you need it.

Modem Information

You can get the state of the Modem pins through the following properties
  • CDHolding - Get the state of the Carrier Detect line
  • CtsHolding - Get the state of the Clear To Send (CTS) line
  • DsrHolding - Get the state of the Data Set Ready (DSR) line
  • RingHolding - Get the state of the Ring Indicator (RI) line

To get the current state of the output pins, you can use the properties
  • DtrEnable - Get the Data Terminal Ready (DTR) line state. You can set this if handshaking is disabled
  • RtsEnable - Get the Request To Send (RTS) line state. You can set this if handshaking is disabled

To control the "break" state of the serial line, you can use the BreakState property.

Driver Configuration

In and Out Buffers

The MS SerialPort implementation uses the properties ReadBufferSize and WriteBufferSize to tell the driver what buffer sizes it should use. This has a direct influence on software flow control (how many bytes should be free when transmitting the XON or XOFF characters).

This implementation has a different interpretation for ReadBufferSize and WriteBufferSize. However, you can still provide the size of the driver buffers with the two properties:
  • DriverInQueue (equivalent to System.IO.Ports.SerialPort.ReadBufferSize)
  • DriverOutQueue (equivalent to System.IO.Ports.SerialPort.WriteBufferSize)

If you rely on software flow control, this is an incompatible difference when moving to the SerialPortStream component.

Last edited Sep 23, 2012 at 2:16 PM by jmcurl, version 5

Comments

No comments yet.