Alicat BASIS-2 Driver 1.0.0
API reference
Loading...
Searching...
No Matches
Functions | Variables
alicat_basis2::modbus Namespace Reference

Functions

uint16_t Crc16 (const uint8_t *data, std::size_t length) noexcept
 Compute the Modbus-RTU CRC-16 over a byte buffer.
 
std::size_t AppendCrc (uint8_t *frame, std::size_t length) noexcept
 Append the CRC-16 to frame[length..length+1] and return new length.
 
bool CheckCrc (const uint8_t *frame, std::size_t length) noexcept
 Verify the CRC at frame[length-2..length-1] matches the body.
 
std::size_t BuildReadHolding (uint8_t unit_addr, uint16_t reg_addr, uint16_t reg_count, uint8_t *out, std::size_t out_capacity) noexcept
 Build a Function-03 (Read Holding Registers) request.
 
std::size_t BuildWriteSingle (uint8_t unit_addr, uint16_t reg_addr, uint16_t value, uint8_t *out, std::size_t out_capacity) noexcept
 Build a Function-06 (Write Single Register) request.
 
std::size_t BuildWriteMultiple (uint8_t unit_addr, uint16_t reg_addr, const uint16_t *regs, uint8_t count, uint8_t *out, std::size_t out_capacity) noexcept
 Build a Function-16 (Write Multiple Registers) request.
 
constexpr uint32_t InterFrameGapUs (uint32_t bps) noexcept
 Minimum idle time t3.5 between two Modbus-RTU frames, in microseconds.
 
constexpr uint32_t InterFrameGapMs (uint32_t bps) noexcept
 InterFrameGapUs(bps) rounded up to whole milliseconds (for UartInterface::delay_ms()).
 
bool IsExceptionReply (const uint8_t *frame, std::size_t length) noexcept
 
DriverError ParseException (const uint8_t *frame, std::size_t length, uint8_t expected_addr, uint8_t expected_fn, uint8_t *code) noexcept
 Parse a 5-byte exception reply (Modbus Application Protocol V1.1b3, section 7).
 
DriverError ParseReadHolding (const uint8_t *frame, std::size_t length, uint8_t expected_addr, uint8_t expected_count, uint16_t *regs) noexcept
 Parse a Function-03 response into regs[].
 
DriverError ParseWriteSingleEcho (const uint8_t *frame, std::size_t length, const uint8_t *request, std::size_t req_len) noexcept
 Validate the echo of a Function-06 (Write Single) request against the just-sent request.
 
DriverError ParseWriteMultipleEcho (const uint8_t *frame, std::size_t length, uint8_t expected_addr, uint16_t expected_reg_addr, uint16_t expected_count) noexcept
 Validate the response of a Function-16 (Write Multiple) request.
 

Variables

constexpr uint32_t kRtuBitsPerChar = 11
 Bits per RTU character used for the timing: start + 8 data + parity/stop + stop (section 2.5.1).
 
constexpr uint32_t kFixedGapAboveBps = 19200
 Above this baud rate the spec fixes t3.5 at 1750 us instead of 3.5 character times.
 
constexpr uint32_t kFixedGapUs = 1750
 The fixed t3.5 used above 19200 baud, in microseconds.
 
constexpr std::size_t kExceptionFrameLength = 5
 Length of a Modbus-RTU exception reply: address, function | 0x80, exception code, CRC-16.
 
constexpr std::size_t kReplyHeaderLength = 3
 Number of reply bytes a receiver reads before it can tell an exception reply (bit 7 of the function code set) from a normal one: address, function code, and one more byte.
 

Function Documentation

◆ AppendCrc()

std::size_t alicat_basis2::modbus::AppendCrc ( uint8_t *  frame,
std::size_t  length 
)
inlinenoexcept

Append the CRC-16 to frame[length..length+1] and return new length.

◆ BuildReadHolding()

std::size_t alicat_basis2::modbus::BuildReadHolding ( uint8_t  unit_addr,
uint16_t  reg_addr,
uint16_t  reg_count,
uint8_t *  out,
std::size_t  out_capacity 
)
inlinenoexcept

Build a Function-03 (Read Holding Registers) request.

Returns the total frame length (header + payload + CRC).

◆ BuildWriteMultiple()

std::size_t alicat_basis2::modbus::BuildWriteMultiple ( uint8_t  unit_addr,
uint16_t  reg_addr,
const uint16_t *  regs,
uint8_t  count,
uint8_t *  out,
std::size_t  out_capacity 
)
inlinenoexcept

Build a Function-16 (Write Multiple Registers) request.

regs carries count 16-bit values in big-endian-on-the-wire order.

◆ BuildWriteSingle()

std::size_t alicat_basis2::modbus::BuildWriteSingle ( uint8_t  unit_addr,
uint16_t  reg_addr,
uint16_t  value,
uint8_t *  out,
std::size_t  out_capacity 
)
inlinenoexcept

Build a Function-06 (Write Single Register) request.

◆ CheckCrc()

bool alicat_basis2::modbus::CheckCrc ( const uint8_t *  frame,
std::size_t  length 
)
inlinenoexcept

Verify the CRC at frame[length-2..length-1] matches the body.

◆ Crc16()

uint16_t alicat_basis2::modbus::Crc16 ( const uint8_t *  data,
std::size_t  length 
)
inlinenoexcept

Compute the Modbus-RTU CRC-16 over a byte buffer.

Parameters
dataPointer to the bytes to checksum.
lengthNumber of bytes.
Returns
Little-endian CRC-16 (low byte first when transmitted).

◆ InterFrameGapMs()

constexpr uint32_t alicat_basis2::modbus::InterFrameGapMs ( uint32_t  bps)
constexprnoexcept

InterFrameGapUs(bps) rounded up to whole milliseconds (for UartInterface::delay_ms()).

◆ InterFrameGapUs()

constexpr uint32_t alicat_basis2::modbus::InterFrameGapUs ( uint32_t  bps)
constexprnoexcept

Minimum idle time t3.5 between two Modbus-RTU frames, in microseconds.

3.5 character times of 11 bits at bps, rounded up; a fixed 1750 us above 19200 baud (Modbus over Serial Line V1.02, section 2.5.1.1). bps == 0 is treated as "unknown" and gets the 4800-baud (longest) gap.

◆ IsExceptionReply()

bool alicat_basis2::modbus::IsExceptionReply ( const uint8_t *  frame,
std::size_t  length 
)
inlinenoexcept
Returns
true when frame (at least 2 bytes) is an exception reply (function code bit 7 set).

◆ ParseException()

DriverError alicat_basis2::modbus::ParseException ( const uint8_t *  frame,
std::size_t  length,
uint8_t  expected_addr,
uint8_t  expected_fn,
uint8_t *  code 
)
inlinenoexcept

Parse a 5-byte exception reply (Modbus Application Protocol V1.1b3, section 7).

Parameters
expected_fnFunction code of the request (the reply carries expected_fn | 0x80).
[out]codeException code, written only when the frame is a valid exception reply.
Returns
ModbusException for a valid exception reply; InvalidFrame for a wrong length, BadCrc for a CRC mismatch, ResponseMismatch for another address or function.

◆ ParseReadHolding()

DriverError alicat_basis2::modbus::ParseReadHolding ( const uint8_t *  frame,
std::size_t  length,
uint8_t  expected_addr,
uint8_t  expected_count,
uint16_t *  regs 
)
inlinenoexcept

Parse a Function-03 response into regs[].

expected_count is the number of 16-bit registers the master asked for.

◆ ParseWriteMultipleEcho()

DriverError alicat_basis2::modbus::ParseWriteMultipleEcho ( const uint8_t *  frame,
std::size_t  length,
uint8_t  expected_addr,
uint16_t  expected_reg_addr,
uint16_t  expected_count 
)
inlinenoexcept

Validate the response of a Function-16 (Write Multiple) request.

◆ ParseWriteSingleEcho()

DriverError alicat_basis2::modbus::ParseWriteSingleEcho ( const uint8_t *  frame,
std::size_t  length,
const uint8_t *  request,
std::size_t  req_len 
)
inlinenoexcept

Validate the echo of a Function-06 (Write Single) request against the just-sent request.

The slave must echo the full request.

Variable Documentation

◆ kExceptionFrameLength

constexpr std::size_t alicat_basis2::modbus::kExceptionFrameLength = 5
inlineconstexpr

Length of a Modbus-RTU exception reply: address, function | 0x80, exception code, CRC-16.

◆ kFixedGapAboveBps

constexpr uint32_t alicat_basis2::modbus::kFixedGapAboveBps = 19200
inlineconstexpr

Above this baud rate the spec fixes t3.5 at 1750 us instead of 3.5 character times.

◆ kFixedGapUs

constexpr uint32_t alicat_basis2::modbus::kFixedGapUs = 1750
inlineconstexpr

The fixed t3.5 used above 19200 baud, in microseconds.

◆ kReplyHeaderLength

constexpr std::size_t alicat_basis2::modbus::kReplyHeaderLength = 3
inlineconstexpr

Number of reply bytes a receiver reads before it can tell an exception reply (bit 7 of the function code set) from a normal one: address, function code, and one more byte.

◆ kRtuBitsPerChar

constexpr uint32_t alicat_basis2::modbus::kRtuBitsPerChar = 11
inlineconstexpr

Bits per RTU character used for the timing: start + 8 data + parity/stop + stop (section 2.5.1).