modbus.rtu_request
modbus.rtu_request
Function
Build a Modbus RTU request frame and automatically append CRC16.
Syntax
modbus.rtu_request(config)
Parameters
| Parameters | Type | Required | Default value | Description |
|---|---|---|---|---|
| config | Object | Yes | None | Requests a configuration object. |
Configuration fields
| Field | Type | Required | Default value | Description |
|---|---|---|---|---|
| unit_id | Int | No | 1 | Slave address, 0 to 255. |
| function_code | Int | No | None | Modbus function code. The function string can also be used. |
| function | String | No | None | Function name, for example read_holding_registers. |
| address | Int | Use when reading and writing registers/coils | 0 | Starting address. |
| quantity | Int | When reading, use | 1 | to read the quantity. |
| value | Int/Bool | When writing a single register/coil use | 0 | to write a value. |
| values | Array | Use when writing multiple registers/coils | None | Multi-value writing data. |
| pdu | Bytes/Array | No | None | Original PDU; it will no longer be constructed based on the function code after being passed in. |
| data | Bytes/Array/String | No | None | Unsupported custom function code additional data. |
Return value
| Type | Description |
|---|---|
| Bytes | RTU ADU complete request frame. |
Code Example
packet = modbus.rtu_request({ unit_id: 1, function: 'read_holding_registers', address: 0, quantity: 2 }) // Output: 010300000002c40b print packet.to_hex()
Notes
- RTU CRC is written to the end of the frame with the low byte first.
- Result length is limited by
BT_BYTES_LIMIT.