BASIS 2 Driver 0.1.0
API reference
Loading...
Searching...
No Matches
Classes | Functions | Variables
Modbus RTU codec

CRC-16, builders, parser, validator. More...

Classes

struct  alicat_basis2::modbus::Response
 A parsed Modbus response. More...
 

Functions

constexpr uint16_t alicat_basis2::modbus::Crc16 (std::span< const uint8_t > data) noexcept
 Modbus CRC-16 (polynomial 0xA001 reflected, initial value 0xFFFF).
 
Result< Request > alicat_basis2::modbus::BuildRead (uint8_t address, uint16_t first, uint16_t count) noexcept
 Build a function 3 request.
 
Result< Request > alicat_basis2::modbus::BuildWriteSingle (uint8_t address, uint16_t target, uint16_t value) noexcept
 Build a function 6 request (one register).
 
Result< Request > alicat_basis2::modbus::BuildWriteMultiple (uint8_t address, uint16_t target, uint32_t value) noexcept
 Build a function 16 request for a two-register item (32-bit value, high register first).
 
bool alicat_basis2::modbus::IsLegalModbusRequest (std::span< const uint8_t > bytes) noexcept
 Whether bytes is exactly one request the driver may send: a valid CRC, an address 1 to 247, function 3 on readable registers, function 6 or 16 on writable registers with a legal value.
 
std::size_t alicat_basis2::modbus::ReplyLength (std::span< const char > bytes) noexcept
 How long the reply frame is, as far as the bytes received so far tell.
 
Error alicat_basis2::modbus::ParseResponse (std::span< const char > frame, uint8_t request_address, uint8_t request_function, std::span< const uint8_t > request_bytes, Response &out) noexcept
 Parse and validate a response frame.
 

Variables

constexpr uint8_t alicat_basis2::modbus::kReadHolding = 3
 Function code: read holding registers.
 
constexpr uint8_t alicat_basis2::modbus::kWriteSingle = 6
 Function code: write single register.
 
constexpr uint8_t alicat_basis2::modbus::kWriteMultiple = 16
 Function code: write multiple registers.
 

Detailed Description

CRC-16, builders, parser, validator.

Function Documentation

◆ BuildRead()

Result< Request > alicat_basis2::modbus::BuildRead ( uint8_t  address,
uint16_t  first,
uint16_t  count 
)
inlinenoexcept

Build a function 3 request.

Parameters
addressModbus address, 1 to 247.
firstFirst register.
countNumber of registers, 1 to reg::kMaxRead, all on readable registers of the table.
Returns
The request, or InvalidArgument (nothing is formed).

◆ BuildWriteMultiple()

Result< Request > alicat_basis2::modbus::BuildWriteMultiple ( uint8_t  address,
uint16_t  target,
uint32_t  value 
)
inlinenoexcept

Build a function 16 request for a two-register item (32-bit value, high register first).

Parameters
addressModbus address, 1 to 247.
targetThe first register of a writable two-register row.
valueThe combined 32-bit value, within the row's range.
Returns
The request, or InvalidArgument.

◆ BuildWriteSingle()

Result< Request > alicat_basis2::modbus::BuildWriteSingle ( uint8_t  address,
uint16_t  target,
uint16_t  value 
)
inlinenoexcept

Build a function 6 request (one register).

Parameters
addressModbus address, 1 to 247.
targetThe register, a writable one-register row of the table.
valueThe value, within the row's range, or exactly its magic number.
Returns
The request, or InvalidArgument.

◆ Crc16()

constexpr uint16_t alicat_basis2::modbus::Crc16 ( std::span< const uint8_t >  data)
constexprnoexcept

Modbus CRC-16 (polynomial 0xA001 reflected, initial value 0xFFFF).

Parameters
dataThe bytes.
Returns
The CRC; it is sent low byte first.

◆ IsLegalModbusRequest()

bool alicat_basis2::modbus::IsLegalModbusRequest ( std::span< const uint8_t >  bytes)
inlinenoexcept

Whether bytes is exactly one request the driver may send: a valid CRC, an address 1 to 247, function 3 on readable registers, function 6 or 16 on writable registers with a legal value.

Independent of the builders: it parses the bytes back and checks them against the register table.

Parameters
bytesThe candidate wire bytes.
Returns
true when legal.

◆ ParseResponse()

Error alicat_basis2::modbus::ParseResponse ( std::span< const char >  frame,
uint8_t  request_address,
uint8_t  request_function,
std::span< const uint8_t >  request_bytes,
Response &  out 
)
inlinenoexcept

Parse and validate a response frame.

Parameters
frameThe received bytes.
request_addressThe address that was sent to.
request_functionThe function code that was sent.
request_bytesThe request that was sent: the echo of a write is checked against it.
outThe response; untouched on failure except exception of a Rejected reply.
Returns
Ok, BadReply (too short, wrong length, wrong byte count, wrong function), Crc, DeviceIdMismatch (another address) or Rejected (an exception reply; the code is in out.exception).

◆ ReplyLength()

std::size_t alicat_basis2::modbus::ReplyLength ( std::span< const char >  bytes)
inlinenoexcept

How long the reply frame is, as far as the bytes received so far tell.

Parameters
bytesWhat has arrived.
Returns
The full length (5 for an exception reply, 5 + byte count for function 3, 8 for 6 and 16), or 0 while it is not known yet; kMaxLine + 1 for a function code the driver never sends.