Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Modbus

Kconfig: CONFIG_MODULE_MODBUS — GClasses: C_PROT_MODBUS_M

Modbus protocol master — reads/writes device registers (coils, discrete inputs, input/holding registers). For industrial IoT.

C_PROT_MODBUS_M

Modbus master protocol — supports four object types:

ObjectAccessSize
Coilsread-write1 bit
Discrete Inputsread-only1 bit
Input Registersread-only16 bits
Holding Registersread-write16 bits

Address range 0x0000–0xFFFF. Configuration-driven slave device mapping with data-format conversion and multiplier support.

The master runs over C_TCP ("modbus_protocol": "TCP"). The slave/register map is JSON config: a slaves array, each with its register definitions (type, address, format, multiplier). The master polls them on a timer.

Commands: dump_data (current register snapshot), set-poll-timeout.

dump_data takes slave_id (an integer, -1 all slaves), address and size (-1 all data). address and size are DTP_JSON since SDK 7.27.0: a number or its text, as the register maps write them ("address": "4096"). They were DTP_STRING, which the command parser now holds to a string, so a number from a SPA would be refused. An address above 0xFFFF answers -1 “Address out of range”.

/*  the address as a register map writes it, the size as a number  */
json_t *resp = gobj_command(gobj_modbus, "dump_data",
    json_pack("{s:i, s:s, s:i}", "slave_id", 1, "address", "4096", "size", 16),
    gobj
);

Trace levels: messages, traffic (raw bytes), polling, decode, send.