Add modbus-tcp support

This commit is contained in:
cpq authored and robert committed 2026-04-06 03:54:58 -04:00
1 parent 1bb85799ce
commit d67b30aba3
7 files changed
+885 -4

No files matched your search

+35
View File
@@ -1660,6 +1660,7 @@ enum {
MG_EV_WAKEUP, // mg_wakeup() data received struct mg_str *data
MG_EV_MDNS_REQ, // mDNS request struct mg_mdns_req *
MG_EV_MDNS_RESP, // mDNS response struct mg_mdns_resp *
MG_EV_MODBUS_REQ, // ModBus request struct mg_modbus_cmd *
MG_EV_USER // Starting ID for user events
};
@@ -3024,6 +3025,40 @@ bool mg_mdns_query(struct mg_connection *, const char *, unsigned int);
// Functions
#define MG_MODBUS_FUNC_READ_COILS 1
#define MG_MODBUS_FUNC_READ_DISCRETE_INPUTS 2
#define MG_MODBUS_FUNC_READ_HOLDING_REGISTERS 3
#define MG_MODBUS_FUNC_READ_INPUT_REGISTERS 4
#define MG_MODBUS_FUNC_WRITE_SINGLE_COIL 5
#define MG_MODBUS_FUNC_WRITE_SINGLE_REGISTER 6
#define MG_MODBUS_FUNC_WRITE_MULTIPLE_COILS 15
#define MG_MODBUS_FUNC_WRITE_MULTIPLE_REGISTERS 16
// Error codes
#define MG_MODBUS_ERR_NONE 0
#define MG_MODBUS_ERR_ILLEGAL_FUNCTION 1
#define MG_MODBUS_ERR_ILLEGAL_ADDRESS 2
#define MG_MODBUS_ERR_ILLEGAL_VALUE 3
#define MG_MODBUS_ERR_DEVICE_FAILURE 4
struct mg_modbus_req {
uint8_t func; // Function code, one of MG_MODBUS_FUNC_*
uint8_t error; // Error code user handler can set
uint16_t addr; // Address
union {
bool *bits;
uint16_t *regs;
} u;
uint16_t len; // Number of registers or bits
};
struct mg_connection *mg_modbus_listen(struct mg_mgr *mgr, const char *url,
mg_event_handler_t fn, void *fn_data);
#ifndef MG_JSON_MAX_DEPTH
#define MG_JSON_MAX_DEPTH 30