Layer: L7 Application ยท Build flags: PROTOCORE_ENABLE_MODBUS, PROTOCORE_ENABLE_MODBUS_MASTER
What this example teaches
ModbusTcp was the slave; this is the master/client side: building read-request ADUs and parsing the responses back into register values. To keep the example self-contained it self-scans - it feeds the request through the on-board slave (protocore_modbus_process_adu) so the build/parse codec is demonstrated end-to-end with no external device. Against a real slave you would send the ADU over a TCP client instead. GET /scan returns the discovered holding registers as JSON.
Build a request, process it, parse the response.
uint8_t req[16], resp[MODBUS_ADU_MAX];
size_t rn = protocore_modbus_build_read(ModbusFunction::MODBUS_FC_READ_HOLDING_REGS, 1, 1, 0, 3, req, sizeof(req));
size_t pn = protocore_modbus_process_adu(req, rn, resp, sizeof(resp));
uint16_t regs[3];
uint8_t ex = 0;
int n = protocore_modbus_parse_response(resp, pn, regs, 3, &ex);
protocore_modbus_build_read(fc, tid, unit, start, count, ...) encodes a read request; protocore_modbus_master_parse_response() returns the count on success or sets the Modbus exception code on a failure response. Swapping protocore_modbus_process_adu for a TCP send/receive is the only change needed to scan a real device.
Build and run
pio ci --board=esp32dev --project-option="framework=arduino" \
--project-option="build_flags=-DPROTOCORE_ENABLE_MODBUS=1 -DPROTOCORE_ENABLE_MODBUS_MASTER=1" \
--lib="." examples/L7-Application/ModbusScan/ModbusScan.ino
curl http://<ip>/scan # {"regs":[1234,5678,4095]}
Annotated source
The complete sketch (ModbusScan.ino), reproduced verbatim with added explanatory comments:
#define PROTOCORE_ENABLE_MODBUS 1
#define PROTOCORE_ENABLE_MODBUS_MASTER 1
#include "protocore.h"
static const char *SSID = "YOUR_SSID";
static const char *PASSWORD = "YOUR_PASSWORD";
PC server;
void setup()
{
Serial.begin(115200);
Physical.wifi->init(SSID, PASSWORD);
while (!Physical.wifi->ready())
delay(250);
Serial.print("IP: ");
uint32_t ip = Physical.link->egress_ip();
Serial.printf("IP: %u.%u.%u.%u\n", (unsigned)(ip & 0xFF), (unsigned)((ip >> 8) & 0xFF),
(unsigned)((ip >> 16) & 0xFF), (unsigned)((ip >> 24) & 0xFF));
protocore_modbus_server_init();
protocore_modbus_set_holding_reg(0, 1234);
protocore_modbus_set_holding_reg(1, 5678);
protocore_modbus_set_holding_reg(2, 4095);
server.listen(502, ProtoConn::PROTO_MODBUS);
uint8_t req[16], resp[MODBUS_ADU_MAX];
size_t rn = protocore_modbus_build_read(ModbusFunction::MODBUS_FC_READ_HOLDING_REGS, 1, 1, 0, 3, req, sizeof(req));
size_t pn = protocore_modbus_process_adu(req, rn, resp, sizeof(resp));
uint16_t regs[3];
uint8_t ex = 0;
int n = protocore_modbus_parse_response(resp, pn, regs, 3, &ex);
char b[96];
if (n > 0)
snprintf(b, sizeof(b), "{\"regs\":[%u,%u,%u]}", regs[0], regs[1], regs[2]);
else
snprintf(b, sizeof(b), "{\"exception\":%u}", ex);
server.send(id, 200, "application/json", b);
});
server.begin(80);
}
void loop()
{
server.handle();
}
@ HTTP_GET
Safe, idempotent read.
Zero-heap Modbus TCP slave/server (Modbus Application Protocol v1.1b3).
Modbus TCP master codec + register scanner (PROTOCORE_ENABLE_MODBUS_MASTER).
Layer 1 (Physical) - link bring-up, the interface registry, and live egress reporting.