Table of Contents

Class SerialPortModule

Namespace
Loehnert.Lisrt.Communication.SerialPort
Assembly
Loehnert.Lisrt.Communication.dll

Represents an ILisrtModule implementation for a serial port.

public class SerialPortModule : CommunicationBase, ICommunication, ILisrtModule, IInitializable, INotifyPropertyChangedEx, INotifyPropertyChanged, IHasServiceView, IDisposable
Inheritance
PropertyChangedBase
SerialPortModule
Implements
INotifyPropertyChangedEx
Inherited Members
PropertyChangedBase.Refresh()
PropertyChangedBase.IsNotifying
PropertyChangedBase.PropertyChanged

Examples

This example creates a serial port module, using ASCII encoding and carriage return linefeed as end delimiter.

new SerialPortModule("ScannerSerialPort")
{
    Encoding = Encoding.ASCII,
    EndDelimiter = Encoding.ASCII.GetBytes("\r\n"),
});

Remarks

For details about message-based communication, see CommunicationBase remarks.

If using a USB-connected serial port with a FTDI chip, the FTDI chip's latency timer should be set to its lowest value. You adapt the Latency Timer via the Windows Device Manager -> Right click on the COM port -> Properties -> Port Settings -> Advanced.

Constructors

SerialPortModule(string)

Initializes a new instance of the SerialPortModule class.

public SerialPortModule(string moduleName)

Parameters

moduleName string

The serial port name.

Properties

BaudRate

Gets or sets the serial baud rate.

[Configuration(9600, Unit = "baud")]
public int BaudRate { get; set; }

Property Value

int

Remarks

Goes direct though the BaudRate.

CDHolding

Gets a value indicating whether the state of the Carrier Detect line for the port is high.

public bool CDHolding { get; }

Property Value

bool

CtsHolding

Gets a value indicating whether the state of the Clear-to-Send line is high.

public bool CtsHolding { get; }

Property Value

bool

DataBits

Gets or sets the standard length of data bits per byte.

[Configuration(8)]
public int DataBits { get; set; }

Property Value

int

Remarks

Goes direct though the DataBits.

DelayAfterSending

Gets or sets a delay (in milliseconds) which is waited after each sending.

[Configuration(0, Unit = "ms", Description = "The delay which is waited after each sending. 0 (zero) disables the delay.")]
public int DelayAfterSending { get; set; }

Property Value

int

Remarks

0 (zero) disables the delay.

DsrHolding

Gets a value indicating whether the state of the Data Set Ready (DSR) signal is high.

public bool DsrHolding { get; }

Property Value

bool

DtrEnable

Gets or sets a value indicating whether the Data Terminal Ready (DTR) signal during serial communication is enabled.

public bool DtrEnable { get; set; }

Property Value

bool

Remarks

Goes direct though the DtrEnable.

Handshake

Gets or sets the handshaking protocol for serial port transmission of data.

[Configuration(SerialHandshake.None)]
public SerialHandshake Handshake { get; set; }

Property Value

SerialHandshake

Remarks

See Handshake.

Icon

Gets the icon which is shown in the module tree.

public override Uri Icon { get; }

Property Value

Uri

IsConnected

Gets a value indicating whether the communication module is connected.

public override bool IsConnected { get; }

Property Value

bool

Parity

Gets or sets the parity-checking protocol.

[Configuration(Parity.None)]
public Parity Parity { get; set; }

Property Value

Parity

Remarks

See Parity.

PortName

Gets or sets the name for the communication port (e. g. 'COM1').

[Configuration("COM1", Description = "e.g. 'COM1'")]
public override string PortName { get; set; }

Property Value

string

RtsEnable

Gets or sets a value indicating whether the Request to Send (RTS) signal is enabled during serial communication.

public bool RtsEnable { get; set; }

Property Value

bool

Remarks

Goes direct though the RtsEnable.

StopBits

Gets or sets the standard number of stop bits per byte.

[Configuration(StopBits.One)]
public StopBits StopBits { get; set; }

Property Value

StopBits

Remarks

Goes direct though the StopBits.

Stream

Gets the base stream.

protected override Stream Stream { get; }

Property Value

Stream

Methods

Close()

Closes the port.

public override void Close()

Open()

Opens a serial port with the configured parameters and clears the input and output buffers.

public override void Open()

Send(byte[], string)

Sends the data.

public override void Send(byte[] data, string logComment = "")

Parameters

data byte[]

The data to send.

logComment string

A comment for the communication log.

Remarks

We override the base method to implement a DTRDSR-Handshake. This means we wait for DsrHolding = true (Timeout). After each Sending there will be a short DelayAfterSending which blocks the sending and gives the device time to change the pin.

Exceptions

PortNotOpenException

Thrown when the port is not open.

InvalidDataException

Thrown when data contain StartDelimiter or EndDelimiter.

TimeoutException

Thrown when DSR pin was not on a high value before timeout.

SendString(string, string)

Sends the data.

public override void SendString(string data, string logComment = "")

Parameters

data string

The data to send.

logComment string

A comment for the communication log.

Remarks

We override the base method to implement a DTRDSR-Handshake. This means we wait for DsrHolding = true (Timeout). After each Sending there will be a short DelayAfterSending which blocks the sending and gives the device time to change the pin.

Exceptions

PortNotOpenException

Thrown when the port is not open.

TimeoutException

Thrown when DSR pin was not on a high value before timeout.

InvalidDataException

Thrown when data contain StartDelimiter or EndDelimiter.

ShowServiceView()

Shows the ServiceViewModel/ServiceView.

public override void ShowServiceView()