import modbus
The modbus module provides profile-driven synchronous Modbus RTU reads. A profile is a JSON string that describes named registers, coils, scaling, units, and writes. After the profile is loaded, scripts work with names such as ``modbus.get("temperature")`` instead of hand-reading register addresses. Use ``system.schedule`` when a profile value should be read periodically.
Quick example
import modbus
import system
profile = '''
{
"slave": 1,
"entries": [
{"name": "temperature", "function": "input", "address": 0, "type": "int", "scale": 0.1, "unit": "C"},
{"name": "run_command", "function_codes": [3, 6], "address": 10, "type": "uint", "access": "read/write"}
]
}
'''
modbus.begin(baud=9600, tx=17, rx=16)
modbus.load_profile(profile)
def read_temperature():
print(modbus.get("temperature"))
system.schedule("temperature", 5000, 5000, -1, read_temperature)
importmodbus
Industrial Modbus native Berry module.
The modbus module provides profile-driven synchronous Modbus RTU reads. A profile is a JSON string that describes named registers, coils, scaling, units, and writes. After the profile is loaded, scripts work with names such as ``modbus.get("temperature")`` instead of hand-reading register addresses. Use ``system.schedule`` when a profile value should be read periodically.
modbus.begin(baud: int, tx: int, rx: int, de: int = None, uart: int = None, timeout_ms: int = None, retries: any = None, delay_ms: int = None, offset: float = None) -> bool
modbus.begin(baud: int, tx: int, rx: int, de: int = None, uart: int = None, timeout_ms: int = None, retries: any = None, delay_ms: int = None, offset: float = None) -> boolInitialize Modbus RTU on a UART bus.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
baud |
int |
positional or keyword | Yes | Serial baud rate, such as 9600 or 19200. |
tx |
int |
positional or keyword | Yes | GPIO pin used for UART transmit. |
rx |
int |
positional or keyword | Yes | GPIO pin used for UART receive. |
de |
int |
positional or keyword | No | Optional RS485 driver-enable GPIO pin. Defaults to no DE pin. |
uart |
int |
positional or keyword | No | Optional ESP-IDF UART number. Defaults to UART1. |
timeout_ms |
int |
positional or keyword | No | Optional response timeout in milliseconds. |
retries |
any |
positional or keyword | No | Optional number of retry attempts per request. |
delay_ms |
int |
positional or keyword | No | Optional delay between profile reads in milliseconds. |
offset |
float |
positional or keyword | No | Optional register address offset applied to all reads and writes. |
bool True when the UART configuration is accepted.
modbus.load_profile(profile: str) -> bool
modbus.load_profile(profile: str) -> boolLoad a JSON Modbus profile.
The JSON may use an ``entries`` array, a ``registers`` array, or a register-reference object such as ``{"30001": {"name": "temperature"}}``. Entry names are normalized to lowercase snake_case.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
profile |
str |
positional or keyword | Yes | JSON string containing the Modbus profile. |
bool True when at least one readable entry is loaded.
modbus.get(name: str) -> bool
modbus.get(name: str) -> boolSynchronously read and return one named profile entry.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
name |
str |
positional or keyword | Yes | Profile entry name, such as "temperature". |
bool Bool, int, or float value from the current Modbus read. Returns None when the name is missing or the read fails.
modbus.get_all() -> dict
modbus.get_all() -> dictSynchronously read and return all profile values.
dict Map of profile entry names to bool, int, or float values from the current Modbus reads.
modbus.info(name: str) -> dict
modbus.info(name: str) -> dictReturn status metadata for one profile entry.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
name |
str |
positional or keyword | Yes | Profile entry name. |
dict Map containing found, name, value, unit, type, slave, function, address, count, valid, writable, age_ms, and last_error.
modbus.write(name: str, value: any = None) -> bool
modbus.write(name: str, value: any = None) -> boolWrite one named profile entry.
If the profile entry includes a fixed write value, the value argument can be omitted. Otherwise pass the value to write.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
name |
str |
positional or keyword | Yes | Writable profile entry name. |
value |
any |
positional or keyword | No | Optional value to write. Required unless the profile entry defines ``value`` or ``write_value``. |
bool True when the Modbus write request succeeds.
modbus.status() -> dict
modbus.status() -> dictReturn module and profile status.
dict Map containing loaded, mode, entries, success, failed, timeout_ms, retries, and last_error.
modbus.read_raw(slave: any, function: any, address: int, count: int) -> list
modbus.read_raw(slave: any, function: any, address: int, count: int) -> listRead a raw Modbus address range.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
slave |
any |
positional or keyword | Yes | Modbus slave id. |
function |
any |
positional or keyword | Yes | Read function code: 1 coils, 2 discrete inputs, 3 holding registers, or 4 input registers. |
address |
int |
positional or keyword | Yes | Zero-based register or coil address. |
count |
int |
positional or keyword | Yes | Number of values to read. |
list List of booleans for function 1 or 2, or register integers for function 3 or 4. Returns None when the request fails.
modbus.write_raw(slave: any, function: any, address: int, value: list) -> bool
modbus.write_raw(slave: any, function: any, address: int, value: list) -> boolWrite a raw Modbus value.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
slave |
any |
positional or keyword | Yes | Modbus slave id. |
function |
any |
positional or keyword | Yes | Write function code: 5 coil, 6 single register, or 16 multiple registers. |
address |
int |
positional or keyword | Yes | Zero-based register or coil address. |
value |
list |
positional or keyword | Yes | Boolean, integer, or list of register integers to write. |
bool True when the Modbus write request succeeds.