File syn_modbus.h¶
FileList > proto > syn_modbus.h
Go to the source code of this file
Modbus RTU slave implementation. More...
#include "../common/syn_defs.h"#include "../drivers/syn_uart.h"#include "../port/syn_port_system.h"#include "../util/syn_crc.h"#include <stdbool.h>#include <stddef.h>#include <stdint.h>
Classes¶
| Type | Name |
|---|---|
| struct | SYN_Modbus Modbus RTU slave instance — frame buffer, config, and statistics. |
| struct | SYN_Modbus_Config Modbus RTU slave configuration. |
| struct | SYN_Modbus_DeviceInfo Device Information structure for FC 0x2B / 0x0E. |
Public Types¶
| Type | Name |
|---|---|
| typedef bool(* | SYN_Modbus_ReadFileRecordCallback Callback for reading file records (FC 0x14). |
| typedef bool(* | SYN_Modbus_WriteCallback Called before a write to holding registers is applied. |
| typedef bool(* | SYN_Modbus_WriteFileRecordCallback Callback for writing file records (FC 0x15). |
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. |
Macros¶
| Type | Name |
|---|---|
| define | SYN_MB_EX_DEVICE_FAILURE 0x04 |
| define | SYN_MB_EX_ILLEGAL_ADDR 0x02 |
| define | SYN_MB_EX_ILLEGAL_FUNC 0x01 |
| define | SYN_MB_EX_ILLEGAL_VALUE 0x03 |
| define | SYN_MB_FC_DIAGNOSTICS 0x08 |
| define | SYN_MB_FC_GET_COMM_EVENT_CNT 0x0B |
| define | SYN_MB_FC_GET_COMM_EVENT_LOG 0x0C |
| define | SYN_MB_FC_MASK_WRITE_REGISTER 0x16 |
| define | SYN_MB_FC_READ_COILS 0x01 |
| define | SYN_MB_FC_READ_DEVICE_INFO 0x2B |
| define | SYN_MB_FC_READ_DISCRETE_INPUTS 0x02 |
| define | SYN_MB_FC_READ_EXCEPTION_STATUS 0x07 |
| define | SYN_MB_FC_READ_FIFO_QUEUE 0x18 |
| define | SYN_MB_FC_READ_FILE_RECORD 0x14 |
| define | SYN_MB_FC_READ_HOLDING 0x03 |
| define | SYN_MB_FC_READ_INPUT 0x04 |
| define | SYN_MB_FC_READ_WRITE_MULTIPLE 0x17 |
| define | SYN_MB_FC_REPORT_SERVER_ID 0x11 |
| define | SYN_MB_FC_WRITE_FILE_RECORD 0x15 |
| define | SYN_MB_FC_WRITE_MULTIPLE 0x10 |
| define | SYN_MB_FC_WRITE_MULTIPLE_COILS 0x0F |
| define | SYN_MB_FC_WRITE_SINGLE 0x06 |
| define | SYN_MB_FC_WRITE_SINGLE_COIL 0x05 |
| define | SYN_MB_MEI_TYPE_READ_DEVICE_ID 0x0E |
Detailed Description¶
Implements a Modbus RTU slave using UART, CRC-16/Modbus (in syn_crc), and register/bit maps. Supports all 19 function codes:
- 0x01: Read Coils - 0x0F: Write Multiple Coils
- 0x02: Read Discrete Inputs - 0x10: Write Multiple Registers
- 0x03: Read Holding Registers - 0x11: Report Server ID
- 0x04: Read Input Registers - 0x14: Read File Record
- 0x05: Write Single Coil - 0x15: Write File Record
- 0x06: Write Single Register - 0x16: Mask Write Register
- 0x07: Read Exception Status - 0x17: Read/Write Multiple Registers
- 0x08: Serial Line Diagnostics - 0x18: Read FIFO Queue
- 0x0B: Get Comm Event Counter - 0x2B: Read Device Identification
- 0x0C: Get Comm Event Log
The register/coil maps are application-owned arrays. The Modbus module handles framing, CRC validation, and exception responses.
** **
static uint16_t holding_regs[32];
static uint16_t input_regs[16];
static SYN_Modbus mb;
static uint8_t mb_buf[256];
SYN_Modbus_Config cfg = {
.slave_addr = 1,
.uart = SYN_UART0,
.holding_regs = holding_regs,
.holding_count = 32,
.input_regs = input_regs,
.input_count = 16,
};
syn_modbus_init(&mb, &cfg, mb_buf, sizeof(mb_buf));
// In main loop:
syn_modbus_poll(&mb); // checks UART, processes requests
// Your app writes input registers:
input_regs[0] = adc_value;
// Your app reads holding registers (written by master):
uint16_t setpoint = holding_regs[0];
Public Types Documentation¶
typedef SYN_Modbus_ReadFileRecordCallback¶
Callback for reading file records (FC 0x14).
typedef bool(* SYN_Modbus_ReadFileRecordCallback) (struct SYN_Modbus *mb, uint16_t file_num, uint16_t record_num, uint16_t record_len, uint16_t *record_data, void *ctx);
typedef SYN_Modbus_WriteCallback¶
Called before a write to holding registers is applied.
typedef bool(* SYN_Modbus_WriteCallback) (struct SYN_Modbus *mb, uint16_t addr, uint16_t count, void *ctx);
Parameters:
mbModbus instance.addrRegister start address.countNumber of registers.ctxUser context.
Returns:
true to allow the write, false to reject (exception 0x03).
typedef SYN_Modbus_WriteFileRecordCallback¶
Callback for writing file records (FC 0x15).
typedef bool(* SYN_Modbus_WriteFileRecordCallback) (struct SYN_Modbus *mb, uint16_t file_num, uint16_t record_num, uint16_t record_len, const uint16_t *record_data, void *ctx);
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.
Macro Definition Documentation¶
define SYN_MB_EX_DEVICE_FAILURE¶
Device failure
define SYN_MB_EX_ILLEGAL_ADDR¶
Illegal data address
define SYN_MB_EX_ILLEGAL_FUNC¶
Illegal function code
define SYN_MB_EX_ILLEGAL_VALUE¶
Illegal data value
define SYN_MB_FC_DIAGNOSTICS¶
Serial line diagnostics
define SYN_MB_FC_GET_COMM_EVENT_CNT¶
Get comm event counter
define SYN_MB_FC_GET_COMM_EVENT_LOG¶
Get comm event log
define SYN_MB_FC_MASK_WRITE_REGISTER¶
Mask write register
define SYN_MB_FC_READ_COILS¶
Read coils (bits)
define SYN_MB_FC_READ_DEVICE_INFO¶
Encapsulated interface trans
define SYN_MB_FC_READ_DISCRETE_INPUTS¶
Read discrete inputs (bits)
define SYN_MB_FC_READ_EXCEPTION_STATUS¶
Read exception status
define SYN_MB_FC_READ_FIFO_QUEUE¶
Read FIFO queue
define SYN_MB_FC_READ_FILE_RECORD¶
Read file record
define SYN_MB_FC_READ_HOLDING¶
Read holding registers
define SYN_MB_FC_READ_INPUT¶
Read input registers
define SYN_MB_FC_READ_WRITE_MULTIPLE¶
Read/Write multiple regs
define SYN_MB_FC_REPORT_SERVER_ID¶
Report server ID
define SYN_MB_FC_WRITE_FILE_RECORD¶
Write file record
define SYN_MB_FC_WRITE_MULTIPLE¶
Write multiple registers
define SYN_MB_FC_WRITE_MULTIPLE_COILS¶
Write multiple coils (bits)
define SYN_MB_FC_WRITE_SINGLE¶
Write single register
define SYN_MB_FC_WRITE_SINGLE_COIL¶
Write single coil (bit)
define SYN_MB_MEI_TYPE_READ_DEVICE_ID¶
Read Device Identification
The documentation for this class was generated from the following file src/syntropic/proto/syn_modbus.h