Class Port
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.
protected Port(SerialPort port, int receivedBytesThreshold = 1, SerialMode mode = SerialMode.RS485)
Parameters
portSerialPortThe serial port.
receivedBytesThresholdintThe number of bytes required before the DataReceived(int) event is fired. Default is 1.
modeSerialModeThe 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.
protected Port(string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits, int receivedBytesThreshold = 1, SerialMode mode = SerialMode.RS485)
Parameters
portNamestringThe name of the serial port.
baudRateintThe baud rate.
parityParityThe parity.
dataBitsintThe number of data bits.
stopBitsStopBitsThe number of stop bits.
receivedBytesThresholdintThe number of bytes required before the DataReceived(int) event is fired. Default is 1.
modeSerialModeThe 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
Logger
Gets or sets the logger instance.
public ILogger Logger { get; set; }
Property Value
PortName
Gets the name of the serial port.
public string PortName { get; }
Property Value
ReadBufferSize
Gets or sets the size of the input buffer.
public int ReadBufferSize { get; set; }
Property Value
ReadTimeout
Gets or sets the read timeout in milliseconds.
public int ReadTimeout { get; set; }
Property Value
WriteBufferSize
Gets or sets the size of the output buffer.
public int WriteBufferSize { get; set; }
Property Value
WriteTimeout
Gets or sets the write timeout in milliseconds.
public int WriteTimeout { get; set; }
Property Value
Methods
CheckOpen()
Checks if the serial port is open and opens it if necessary.
protected bool CheckOpen()
Returns
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
lengthintThe 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
bytesToReadintThe number of bytes available to read.
DataWrite(byte)
Writes a single byte of data to the serial port.
protected void DataWrite(byte value)
Parameters
valuebyteThe byte to write to the serial port.
DataWrite(byte[], int, int)
Writes a specified number of bytes to the serial port.
protected bool DataWrite(byte[] buffer, int offset, int count)
Parameters
bufferbyte[]The buffer that contains the data to write.
offsetintThe zero-based byte offset in the buffer at which to begin writing.
countintThe number of bytes to write.
Returns
Dispose()
Releases the resources used by the serial port.
public void Dispose()
Dispose(bool)
Releases the unmanaged resources used by the serial port and optionally releases the managed resources.
protected virtual void Dispose(bool disposing)
Parameters
disposingbooltrue to release both managed and unmanaged resources; false to release only unmanaged resources.
Format(byte[])
Formats an array of bytes as a string.
protected static string Format(byte[] buffer)
Parameters
bufferbyte[]The buffer to format.
Returns
- string
A string representation of the buffer in hexadecimal format.