Table of Contents

Class Port

Namespace
Iot.Device.Modbus
Assembly
Iot.Device.Modbus.dll

Represents a serial port used for Modbus communication.

public abstract class Port
Inheritance
Port
Derived

Constructors

Port(SerialPort, int, SerialMode)

Initializes a new instance of the Port class with the specified serial port.

public Port(SerialPort port, int receivedBytesThreshold = 1, SerialMode mode = SerialMode.RS485)

Parameters

port SerialPort

The serial port.

receivedBytesThreshold int

The number of bytes required before the DataReceived event is fired. Default is 1.

mode SerialMode

The mode of serial port, default is RS485.

Port(string, int, Parity, int, StopBits, int, SerialMode)

Initializes a new instance of the Port class with the specified port settings.

public Port(string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits, int receivedBytesThreshold = 1, SerialMode mode = SerialMode.RS485)

Parameters

portName string

The name of the serial port.

baudRate int

The baud rate.

parity Parity

The parity.

dataBits int

The number of data bits.

stopBits StopBits

The number of stop bits.

receivedBytesThreshold int

The number of bytes required before the DataReceived event is fired. Default is 1.

mode SerialMode

The mode of serial port, default is RS485.

Properties

IsOpen

Gets a value indicating whether the serial port is open.

public bool IsOpen { get; }

Property Value

bool

PortName

Gets the name of the serial port.

public string PortName { get; }

Property Value

string

ReadBufferSize

Gets or sets the size of the input buffer.

public int ReadBufferSize { get; set; }

Property Value

int

ReadTimeout

Gets or sets the read timeout in milliseconds.

public int ReadTimeout { get; set; }

Property Value

int

WriteBufferSize

Gets or sets the size of the output buffer.

public int WriteBufferSize { get; set; }

Property Value

int

WriteTimeout

Gets or sets the write timeout in milliseconds.

public int WriteTimeout { get; set; }

Property Value

int

Methods

CheckOpen()

Checks if the serial port is open and opens it if necessary.

protected bool CheckOpen()

Returns

bool

True if the serial port is open, false otherwise.

DataRead()

Reads a single byte of data from the serial port.

protected byte DataRead()

Returns

byte

The byte read from the serial port.

DataRead(int)

Reads a specified number of bytes from the serial port.

protected byte[] DataRead(int length)

Parameters

length int

The number of bytes to read.

Returns

byte[]

An array of bytes read from the serial port.

DataReceived(int)

Event handler for the DataReceived event.

protected virtual void DataReceived(int bytesToRead)

Parameters

bytesToRead int

The number of bytes available to read.

DataWrite(byte)

Writes a single byte of data to the serial port.

protected void DataWrite(byte value)

Parameters

value byte

The byte to write to the serial port.

DataWrite(byte[], int, int)

Writes a specified number of bytes to the serial port.

protected void DataWrite(byte[] buffer, int offset, int count)

Parameters

buffer byte[]

The buffer that contains the data to write.

offset int

The zero-based byte offset in the buffer at which to begin writing.

count int

The number of bytes to write.

Dispose()

Releases the resources used by the serial port.

public void Dispose()

Format(byte[])

Formats an array of bytes as a string.

protected static string Format(byte[] buffer)

Parameters

buffer byte[]

The buffer to format.

Returns

string

A string representation of the buffer in hexadecimal format.