Class Modbus
Modbus RTU framing for RS485 field devices.
public static class Modbus
- Inheritance
-
Modbus
- Inherited Members
Remarks
Modbus over RS485 is what cheap industrial sensing speaks: energy meters, soil probes, water-quality transmitters, pump controllers. Each request builder returns a complete frame with its CRC, ready to write to a port, and a reply comes back through ParseFrame(ReadOnlySpan<byte>) as an object that reads its own values.
Methods
Crc16(ReadOnlySpan<byte>)
Computes the CRC-16/MODBUS that every RTU frame ends with.
public static ushort Crc16(ReadOnlySpan<byte> bytes)
Parameters
bytesReadOnlySpan<byte>The frame contents, without the trailing checksum.
Returns
- ushort
The checksum.
ParseFrame(ReadOnlySpan<byte>)
Parses a received RTU frame, verifying its CRC.
public static ModbusFrame ParseFrame(ReadOnlySpan<byte> bytes)
Parameters
bytesReadOnlySpan<byte>The frame as it came off the wire, checksum included.
Returns
- ModbusFrame
The validated frame, which reads its own registers and coils.
Exceptions
- PamojaException
The frame is truncated, oversized, or its CRC does not match its contents.
Raw(byte, byte, ReadOnlySpan<byte>)
Builds a request from a raw function code and data.
public static byte[] Raw(byte address, byte functionCode, ReadOnlySpan<byte> data)
Parameters
addressbyteThe unit address to send to.
functionCodebyteThe function code byte.
dataReadOnlySpan<byte>The bytes that follow it, used verbatim.
Returns
- byte[]
The frame to send.
Remarks
The escape hatch for the function codes this SDK does not name.
Exceptions
- PamojaException
The data is longer than a PDU may be.
ReadCoils(byte, ushort, ushort)
Builds a read-coils request (function 0x01).
public static byte[] ReadCoils(byte address, ushort start, ushort count)
Parameters
addressbyteThe unit address to ask.
startushortThe address of the first coil.
countushortHow many coils to read.
Returns
- byte[]
The frame to send.
Exceptions
- PamojaException
The native call failed.
ReadDiscreteInputs(byte, ushort, ushort)
Builds a read-discrete-inputs request (function 0x02).
public static byte[] ReadDiscreteInputs(byte address, ushort start, ushort count)
Parameters
addressbyteThe unit address to ask.
startushortThe address of the first input.
countushortHow many inputs to read.
Returns
- byte[]
The frame to send.
Exceptions
- PamojaException
The native call failed.
ReadHoldingRegisters(byte, ushort, ushort)
Builds a read-holding-registers request (function 0x03).
public static byte[] ReadHoldingRegisters(byte address, ushort start, ushort count)
Parameters
addressbyteThe unit address to ask.
startushortThe address of the first register.
countushortHow many registers to read.
Returns
- byte[]
The frame to send.
Exceptions
- PamojaException
The native call failed.
ReadHoldingRegistersReply(byte, ReadOnlySpan<ushort>)
Builds the reply a device sends to a read-holding-registers request.
public static byte[] ReadHoldingRegistersReply(byte address, ReadOnlySpan<ushort> values)
Parameters
addressbyteThe unit address the reply comes from.
valuesReadOnlySpan<ushort>The register values the device reports, in address order.
Returns
- byte[]
The frame the device would send.
Remarks
This is the answering half of ReadHoldingRegisters(byte, ushort, ushort), so a client can be written and tested against what a device sends without a device on the line.
Exceptions
- PamojaException
There are no values, or too many for one frame.
ReadInputRegisters(byte, ushort, ushort)
Builds a read-input-registers request (function 0x04).
public static byte[] ReadInputRegisters(byte address, ushort start, ushort count)
Parameters
addressbyteThe unit address to ask.
startushortThe address of the first register.
countushortHow many registers to read.
Returns
- byte[]
The frame to send.
Exceptions
- PamojaException
The native call failed.
ReadInputRegistersReply(byte, ReadOnlySpan<ushort>)
Builds the reply a device sends to a read-input-registers request.
public static byte[] ReadInputRegistersReply(byte address, ReadOnlySpan<ushort> values)
Parameters
addressbyteThe unit address the reply comes from.
valuesReadOnlySpan<ushort>The register values the device reports, in address order.
Returns
- byte[]
The frame the device would send.
Exceptions
- PamojaException
There are no values, or too many for one frame.
WriteMultipleCoils(byte, ushort, ReadOnlySpan<bool>)
Builds a write-multiple-coils request (function 0x0F).
public static byte[] WriteMultipleCoils(byte address, ushort start, ReadOnlySpan<bool> values)
Parameters
addressbyteThe unit address to write to.
startushortThe address of the first coil.
valuesReadOnlySpan<bool>One state per coil, at most 1968 of them.
Returns
- byte[]
The frame to send.
Exceptions
- PamojaException
There are no values, or more than one request can carry.
WriteMultipleRegisters(byte, ushort, ReadOnlySpan<ushort>)
Builds a write-multiple-registers request (function 0x10).
public static byte[] WriteMultipleRegisters(byte address, ushort start, ReadOnlySpan<ushort> values)
Parameters
addressbyteThe unit address to write to.
startushortThe address of the first register.
valuesReadOnlySpan<ushort>The 16-bit values, at most 123 of them.
Returns
- byte[]
The frame to send.
Exceptions
- PamojaException
There are no values, or more than one request can carry.
WriteSingleCoil(byte, ushort, bool)
Builds a write-single-coil request (function 0x05).
public static byte[] WriteSingleCoil(byte address, ushort coil, bool on)
Parameters
addressbyteThe unit address to write to.
coilushortThe coil address.
onboolThe state to write.
Returns
- byte[]
The frame to send.
Exceptions
- PamojaException
The native call failed.
WriteSingleRegister(byte, ushort, ushort)
Builds a write-single-register request (function 0x06).
public static byte[] WriteSingleRegister(byte address, ushort register, ushort value)
Parameters
addressbyteThe unit address to write to.
registerushortThe register address.
valueushortThe 16-bit value to write.
Returns
- byte[]
The frame to send.
Exceptions
- PamojaException
The native call failed.