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.
public Port(SerialPort port, int receivedBytesThreshold = 1, SerialMode mode = SerialMode.RS485)
Parameters
port
SerialPortThe serial port.
receivedBytesThreshold
intThe number of bytes required before the DataReceived event is fired. Default is 1.
mode
SerialModeThe 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
stringThe name of the serial port.
baudRate
intThe baud rate.
parity
ParityThe parity.
dataBits
intThe number of data bits.
stopBits
StopBitsThe number of stop bits.
receivedBytesThreshold
intThe number of bytes required before the DataReceived event is fired. Default is 1.
mode
SerialModeThe 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
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
- 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
intThe 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
intThe 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
byteThe 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
intThe zero-based byte offset in the buffer at which to begin writing.
count
intThe 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.