Nexus Embedded Platform 1.0.0
Statically assembled reusable embedded mechanisms
Loading...
Searching...
No Matches
modbus_rtu.h File Reference

Bounded Modbus RTU core and explicit RS485 ports. More...

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

Go to the source code of this file.

Data Structures

struct  nx_modbus_registers_t
struct  nx_modbus_rs485_port_t
struct  nx_modbus_counters_t
struct  nx_modbus_config_t
struct  nx_modbus_rtu_t

Macros

#define NX_MODBUS_ADU_MAX   256u
#define NX_MODBUS_REGISTER_MAX   125u

Enumerations

enum  nx_modbus_register_result_t {
  NX_MODBUS_OK = 0 , NX_MODBUS_ILLEGAL_ADDRESS = 2 , NX_MODBUS_ILLEGAL_VALUE = 3 , NX_MODBUS_DEVICE_FAILURE = 4 ,
  NX_MODBUS_DEVICE_BUSY = 6
}
enum  nx_modbus_link_state_t { NX_MODBUS_RX_IDLE = 0 , NX_MODBUS_RX_FRAME , NX_MODBUS_RX_DISCARD , NX_MODBUS_LINK_FAULTED }

Functions

uint16_t nx_modbus_crc16 (const uint8_t *bytes, size_t length)
 Compute standard Modbus CRC16 over caller bytes.
bool nx_modbus_rtu_init (nx_modbus_rtu_t *server, const nx_modbus_config_t *config)
 Initialize an unused or fully quiesced protocol instance.
bool nx_modbus_rtu_feed (nx_modbus_rtu_t *server, uint8_t byte, uint64_t arrival_us)
 Feed an externally timestamped UART byte.
bool nx_modbus_rtu_poll (nx_modbus_rtu_t *server, uint64_t now_us)
 Service RTU silence and bounded synchronous RS485 response.
void nx_modbus_rtu_rx_error (nx_modbus_rtu_t *server, uint64_t now_us)
 Discard an errored or overrun receive frame.
bool nx_modbus_rtu_recover (nx_modbus_rtu_t *server)
 Settle retained TX then restore receive silence acquisition.
bool nx_modbus_rtu_stop (nx_modbus_rtu_t *server)
 Settle the port before reclaiming protocol storage.

Detailed Description

Bounded Modbus RTU core and explicit RS485 ports.

Author
Nexus Team

Definition in file modbus_rtu.h.

Macro Definition Documentation

◆ NX_MODBUS_ADU_MAX

#define NX_MODBUS_ADU_MAX   256u

Definition at line 17 of file modbus_rtu.h.

◆ NX_MODBUS_REGISTER_MAX

#define NX_MODBUS_REGISTER_MAX   125u

Definition at line 18 of file modbus_rtu.h.

Enumeration Type Documentation

◆ nx_modbus_link_state_t

Enumerator
NX_MODBUS_RX_IDLE 
NX_MODBUS_RX_FRAME 
NX_MODBUS_RX_DISCARD 
NX_MODBUS_LINK_FAULTED 

Definition at line 62 of file modbus_rtu.h.

◆ nx_modbus_register_result_t

Enumerator
NX_MODBUS_OK 
NX_MODBUS_ILLEGAL_ADDRESS 
NX_MODBUS_ILLEGAL_VALUE 
NX_MODBUS_DEVICE_FAILURE 
NX_MODBUS_DEVICE_BUSY 

Definition at line 20 of file modbus_rtu.h.

Function Documentation

◆ nx_modbus_crc16()

uint16_t nx_modbus_crc16 ( const uint8_t * bytes,
size_t length )

Compute standard Modbus CRC16 over caller bytes.

Parameters
[in]bytesInput, NULL only when length is zero
[in]lengthByte count
Returns
CRC16; zero for invalid NULL input with nonzero length

◆ nx_modbus_rtu_feed()

bool nx_modbus_rtu_feed ( nx_modbus_rtu_t * server,
uint8_t byte,
uint64_t arrival_us )

Feed an externally timestamped UART byte.

Parameters
[in,out]serverInitialized instance, serialized by caller
[in]byteWire byte
[in]arrival_usCapture timestamp in port monotonic domain
Returns
True if byte joined the frame, false for discard/error
Note
Task-only. IRQ queues are external, bounded and explicit; queue overflow must call rx_error. No live UART discovery or ISR callback.

◆ nx_modbus_rtu_init()

bool nx_modbus_rtu_init ( nx_modbus_rtu_t * server,
const nx_modbus_config_t * config )

Initialize an unused or fully quiesced protocol instance.

Parameters
[out]serverCaller-owned storage, no existing transport borrow
[in]configExplicit register/RS485 ports and finite budgets
Returns
True when initialized into RX silence acquisition
Note
Task-only; configuration is copied, contexts stay live. First direction(false) failure is reported; no worker or UART is created. Reinitializing a faulted TX borrow is prohibited; stop/recover it first.

◆ nx_modbus_rtu_poll()

bool nx_modbus_rtu_poll ( nx_modbus_rtu_t * server,
uint64_t now_us )

Service RTU silence and bounded synchronous RS485 response.

Parameters
[in,out]serverInitialized instance, single execution owner
[in]now_usSnapshot in the same monotonic domain
Returns
True for successful progress, false for clock/transport error
Note
Task-only, explicit external scheduling. RS485 drain must observe the final stop bit. Failed abort retains TX bytes and asserted DE in LINK_FAULTED; keep instance/port alive until successful recover or stop.

◆ nx_modbus_rtu_recover()

bool nx_modbus_rtu_recover ( nx_modbus_rtu_t * server)

Settle retained TX then restore receive silence acquisition.

Parameters
[in,out]serverInitialized serialized instance with live ports
Returns
True only after actual port abort/drain and receive direction
Note
Explicit task maintenance action, no fabricated recovery.

◆ nx_modbus_rtu_rx_error()

void nx_modbus_rtu_rx_error ( nx_modbus_rtu_t * server,
uint64_t now_us )

Discard an errored or overrun receive frame.

Parameters
[in,out]serverInitialized serialized instance
[in]now_usError timestamp in port's monotonic domain
Note
Task-only. Does not discard a retained faulted TX borrow.

◆ nx_modbus_rtu_stop()

bool nx_modbus_rtu_stop ( nx_modbus_rtu_t * server)

Settle the port before reclaiming protocol storage.

Parameters
[in,out]serverInitialized, exclusively owned instance
Returns
True when no transport borrow remains and instance is stopped; false preserves fault state, TX bytes and all port lifetime obligations
Note
Stop future feed/poll users first. No future use except explicit initialization is allowed after successful stop.