File syn_modbus.c¶
FileList > proto > syn_modbus.c
Go to the source code of this file
Modbus RTU slave implementation.
#include "../util/syn_assert.h"#include "syn_modbus.h"#include <string.h>#include "../util/syn_pack.h"
Public Functions¶
| Type | Name |
|---|---|
| void | syn_modbus_feed (SYN_Modbus * mb, uint8_t byte) Feed a byte to the Modbus receiver. |
| void | syn_modbus_init (SYN_Modbus * mb, const SYN_Modbus_Config * cfg, uint8_t * buf, uint16_t buf_size) Initialize the Modbus RTU slave. |
| bool | syn_modbus_poll (SYN_Modbus * mb) Poll for incoming Modbus requests and process them. |
| bool | syn_modbus_process (SYN_Modbus * mb) Process a complete frame (call after feed + silence detection). |
| void | syn_modbus_reset (SYN_Modbus * mb) Reset the receiver state. |
Public Static Functions¶
| Type | Name |
|---|---|
| void | append_crc (uint8_t * buf, uint16_t len) Append CRC-16 Modbus to a frame buffer. |
| bool | check_crc (const uint8_t * buf, uint16_t len) Verify the CRC-16 Modbus trailer. |
| bool | get_bit (const uint8_t * bits, uint16_t idx) Helper to get bit from array. |
| void | handle_diagnostics (SYN_Modbus * mb) Handle Diagnostics (FC 0x08). |
| void | handle_get_comm_event_cnt (SYN_Modbus * mb) Handle Get Comm Event Counter (FC 0x0B). |
| void | handle_get_comm_event_log (SYN_Modbus * mb) Handle Get Comm Event Log (FC 0x0C). |
| void | handle_mask_write_register (SYN_Modbus * mb) Handle Mask Write Register (FC 0x16). |
| void | handle_read_bits (SYN_Modbus * mb, uint16_t frame_len, const uint8_t * bits, uint16_t total_bits) Handle Read Coils (FC 0x01) and Read Discrete Inputs (FC 0x02). |
| void | handle_read_device_info (SYN_Modbus * mb) Handle Modbus read device identification (FC 0x2B / MEI 0x0E). |
| void | handle_read_exception_status (SYN_Modbus * mb) Handle Modbus read exception status (FC 0x07). |
| void | handle_read_fifo_queue (SYN_Modbus * mb) Handle Read FIFO Queue (FC 0x18). |
| void | handle_read_file_record (SYN_Modbus * mb) Handle Modbus read file record (FC 0x14). |
| void | handle_read_regs (SYN_Modbus * mb, uint16_t frame_len, const uint16_t * regs, uint16_t reg_count) Handle Modbus read holding/input register request. |
| void | handle_read_write_multiple (SYN_Modbus * mb) Handle Modbus read/write multiple registers (FC 0x17). |
| void | handle_report_server_id (SYN_Modbus * mb) Handle Report Server ID (FC 0x11). |
| void | handle_write_file_record (SYN_Modbus * mb) Handle Modbus write file record (FC 0x15). |
| void | handle_write_multiple (SYN_Modbus * mb, uint16_t frame_len) Handle Modbus write multiple registers request. |
| void | handle_write_multiple_coils (SYN_Modbus * mb, uint16_t frame_len) Handle Write Multiple Coils (FC 0x0F). |
| void | handle_write_single (SYN_Modbus * mb, uint16_t frame_len) Handle Modbus write single register request. |
| void | handle_write_single_coil (SYN_Modbus * mb, uint16_t frame_len) Handle Write Single Coil (FC 0x05). |
| uint16_t | read_u16 (const uint8_t * buf) |
| void | send_exception (SYN_Modbus * mb, uint8_t func, uint8_t ex_code) Send a Modbus exception response. |
| void | send_response (SYN_Modbus * mb, uint16_t len) Append CRC and transmit a response frame. |
| void | set_bit (uint8_t * bits, uint16_t idx, bool val) Helper to set bit in array. |
| void | write_u16 (uint8_t * buf, uint16_t val) |
Macros¶
| Type | Name |
|---|---|
| define | MB_MAX_PDU_DATA 252 |
| define | MB_MIN_FRAME_LEN 4 |
| define | MB_SILENCE_MS 5 |
Public Functions Documentation¶
function syn_modbus_feed¶
Feed a byte to the Modbus receiver.
Alternative to poll() — call from UART ISR if you want interrupt-driven reception.
Parameters:
mbModbus instance.byteReceived byte.
function syn_modbus_init¶
Initialize the Modbus RTU slave.
void syn_modbus_init (
SYN_Modbus * mb,
const SYN_Modbus_Config * cfg,
uint8_t * buf,
uint16_t buf_size
)
Parameters:
mbModbus instance.cfgConfiguration.bufFrame buffer (must be at least 256 bytes).buf_sizeBuffer capacity.
function syn_modbus_poll¶
Poll for incoming Modbus requests and process them.
Call from your main loop. This reads bytes from UART, detects frame boundaries (3.5 character times silence), validates CRC, and processes the request.
Parameters:
mbModbus instance.
Returns:
true if a request was processed.
function syn_modbus_process¶
Process a complete frame (call after feed + silence detection).
Parameters:
mbModbus instance.
Returns:
true if a response was sent.
function syn_modbus_reset¶
Reset the receiver state.
Parameters:
mbModbus instance.
Public Static Functions Documentation¶
function append_crc¶
Append CRC-16 Modbus to a frame buffer.
Parameters:
bufFrame buffer.lenFrame length (excluding CRC).
function check_crc¶
Verify the CRC-16 Modbus trailer.
Parameters:
bufFrame buffer.lenTotal frame length (including CRC).
Returns:
true if CRC matches.
function get_bit¶
Helper to get bit from array.
function handle_diagnostics¶
Handle Diagnostics (FC 0x08).
function handle_get_comm_event_cnt¶
Handle Get Comm Event Counter (FC 0x0B).
function handle_get_comm_event_log¶
Handle Get Comm Event Log (FC 0x0C).
function handle_mask_write_register¶
Handle Mask Write Register (FC 0x16).
function handle_read_bits¶
Handle Read Coils (FC 0x01) and Read Discrete Inputs (FC 0x02).
static void handle_read_bits (
SYN_Modbus * mb,
uint16_t frame_len,
const uint8_t * bits,
uint16_t total_bits
)
function handle_read_device_info¶
Handle Modbus read device identification (FC 0x2B / MEI 0x0E).
Parameters:
mbModbus instance.
function handle_read_exception_status¶
Handle Modbus read exception status (FC 0x07).
Parameters:
mbModbus instance.
function handle_read_fifo_queue¶
Handle Read FIFO Queue (FC 0x18).
function handle_read_file_record¶
Handle Modbus read file record (FC 0x14).
Parameters:
mbModbus instance.
function handle_read_regs¶
Handle Modbus read holding/input register request.
static void handle_read_regs (
SYN_Modbus * mb,
uint16_t frame_len,
const uint16_t * regs,
uint16_t reg_count
)
Parameters:
mbModbus instance.regsRegister array.reg_countTotal register count.
function handle_read_write_multiple¶
Handle Modbus read/write multiple registers (FC 0x17).
Parameters:
mbModbus instance.
function handle_report_server_id¶
Handle Report Server ID (FC 0x11).
function handle_write_file_record¶
Handle Modbus write file record (FC 0x15).
Parameters:
mbModbus instance.
function handle_write_multiple¶
Handle Modbus write multiple registers request.
Parameters:
mbModbus instance.
function handle_write_multiple_coils¶
Handle Write Multiple Coils (FC 0x0F).
function handle_write_single¶
Handle Modbus write single register request.
Parameters:
mbModbus instance.
function handle_write_single_coil¶
Handle Write Single Coil (FC 0x05).
function read_u16¶
function send_exception¶
Send a Modbus exception response.
Parameters:
mbModbus instance.funcFunction code that caused the exception.ex_codeException code.
function send_response¶
Append CRC and transmit a response frame.
Parameters:
mbModbus instance.lenPDU length (excluding CRC).
function set_bit¶
Helper to set bit in array.
function write_u16¶
Macro Definition Documentation¶
define MB_MAX_PDU_DATA¶
Max PDU data bytes.
define MB_MIN_FRAME_LEN¶
Minimum: addr + func + CRC16.
define MB_SILENCE_MS¶
Inter-frame silence (ms).
The documentation for this class was generated from the following file src/syntropic/proto/syn_modbus.c