Skip to content

Commit c7bca5c

Browse files
carlessoleswoboda1337
authored andcommitted
feat(modbus_controller): Add courtesy response configuration for Modbus server (#5199)
1 parent 599861e commit c7bca5c

File tree

1 file changed

+52
-41
lines changed

1 file changed

+52
-41
lines changed

content/components/modbus_controller.md

Lines changed: 52 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,18 @@ On the bus side, you need 120 Ohm termination resistors at the ends of the bus c
6868
6969
- **max_cmd_retries** (*Optional*, integer): How many times a command will be retried if no response is received. It doesn't include the initial transmition. Defaults to 4.
7070
71+
- **server_courtesy_response** (*Optional*): Configuration block to enable the courtesy response feature when the device is acting as a Modbus server.
72+
73+
- **enabled** (*Optional*, boolean): Whether to enable the courtesy response feature.
74+
Defaults to `false`.
75+
- **register_last_address** (*Optional*, integer): The highest Modbus register address (inclusive) up to which undefined registers are allowed to be read and will be padded with a default value.
76+
Any read request that includes undefined registers within this range will return the value specified by `register_value` instead of triggering an exception.
77+
Defaults to `65535`
78+
- **register_value** (*Optional*, integer): The 16-bit value (range: 0–65535) to return for undefined registers within the address range defined by `register_last_address`.
79+
Defaults to `0`.
80+
7181
- **server_registers** (*Optional*): A list of registers that are responded to when acting as a server.
82+
7283
- **address** (**Required**, integer): start address of the first register in a range
7384
- **value_type** (*Optional*): datatype of the mod_bus register data. The default data type for ModBUS is a 16 bit integer in **big endian** format (network byte order, MSB first)
7485
@@ -215,20 +226,20 @@ Some devices use decimal values in read registers to show multiple binary states
215226
| ---------- | ---------------- | --------- | --------- |
216227
| bit 0 | Binary Sensor 0 | 1 | 1 |
217228
| bit 1 | Binary Sensor 1 | 2 | 2 |
218-
| bit 2 | Binary Sensor 2 | 4 | 4 |
219-
| bit 3 | Binary Sensor 3 | 8 | 8 |
220-
| bit 4 | Binary Sensor 4 | 16 | 10 |
221-
| bit 5 | Binary Sensor 5 | 32 | 20 |
222-
| bit 6 | Binary Sensor 6 | 64 | 40 |
223-
| bit 7 | Binary Sensor 7 | 128 | 80 |
224-
| bit 8 | Binary Sensor 8 | 256 | 100 |
225-
| bit 9 | Binary Sensor 9 | 512 | 200 |
226-
| bit 10 | Binary Sensor 10 | 1024 | 400 |
227-
| bit 11 | Binary Sensor 11 | 2048 | 800 |
228-
| bit 12 | Binary Sensor 12 | 4096 | 1000 |
229-
| bit 13 | Binary Sensor 13 | 8192 | 2000 |
230-
| bit 14 | Binary Sensor 14 | 16384 | 4000 |
231-
| bit 15 | Binary Sensor 15 | 32768 | 8000 |
229+
| bit 2 | Binary Sensor 2 | 4 | 4 |
230+
| bit 3 | Binary Sensor 3 | 8 | 8 |
231+
| bit 4 | Binary Sensor 4 | 16 | 10 |
232+
| bit 5 | Binary Sensor 5 | 32 | 20 |
233+
| bit 6 | Binary Sensor 6 | 64 | 40 |
234+
| bit 7 | Binary Sensor 7 | 128 | 80 |
235+
| bit 8 | Binary Sensor 8 | 256 | 100 |
236+
| bit 9 | Binary Sensor 9 | 512 | 200 |
237+
| bit 10 | Binary Sensor 10 | 1024 | 400 |
238+
| bit 11 | Binary Sensor 11 | 2048 | 800 |
239+
| bit 12 | Binary Sensor 12 | 4096 | 1000 |
240+
| bit 13 | Binary Sensor 13 | 8192 | 2000 |
241+
| bit 14 | Binary Sensor 14 | 16384 | 4000 |
242+
| bit 15 | Binary Sensor 15 | 32768 | 8000 |
232243
233244
In the example below, register `15`, holds several binary values. It stores the decimal value `12288`, which is the sum of `4096` + `8192`, meaning the corresponding bits `12` and `13` are `1`, the other bits are `0`.
234245
@@ -492,40 +503,40 @@ The response is mapped to the sensor based on `register_count` and offset in byt
492503
| --------- | -------------------------------------- |
493504
| 0x1 (01) | device address |
494505
| 0x4 (04) | function code 4 (Read Input Registers) |
495-
| 0x30 (48) | start address high byte |
496-
| 0x0 (00) | start address low byte |
497-
| 0x0 (00) | number of registers to read high byte |
498-
| 0x9 (09) | number of registers to read low byte |
499-
| 0x3f (63) | crc |
500-
| 0xc (12) | crc |
506+
| 0x30 (48) | start address high byte |
507+
| 0x0 (00) | start address low byte |
508+
| 0x0 (00) | number of registers to read high byte |
509+
| 0x9 (09) | number of registers to read low byte |
510+
| 0x3f (63) | crc |
511+
| 0xc (12) | crc |
501512
502513
**Response:**
503514
504515
| offset | data | value (type) | description |
505516
| ------ | ---------- | ------------------ | ------------------------------------------ |
506517
| H | 0x1 (01) | | device address |
507518
| H | 0x4 (04) | | function code |
508-
| H | 0x12 (18) | | byte count |
509-
| 0 | 0x27 (39) | U_WORD | array_rated_voltage high byte |
510-
| 1 | 0x10 (16) | 0x2710 (100000) | array_rated_voltage low byte |
511-
| 2 | 0x7 (7) | U_WORD | array_rated_current high byte |
512-
| 3 | 0xd0 (208) | 0x7d0 (2000) | array_rated_current low byte |
513-
| 4 | 0xcb (203) | U_DWORD_R | array_rated_power high byte of low word |
514-
| 5 | 0x20 (32) | spans 2 register | array_rated_power low byte of low word |
515-
| 6 | 0x0 (0) | | array_rated_power high byte of high word |
516-
| 7 | 0x0 (0) | 0x0000CB20 (52000) | array_rated_power low byte of high word |
517-
| 8 | 0x9 (09) | U_WORD | battery_rated_voltage high byte |
518-
| 9 | 0x60 (96) | 0x960 (2400) | battery_rated_voltage low byte |
519-
| 10 | 0x7 (07) | U_WORD | battery_rated_current high word |
520-
| 11 | 0xd0 (208) | 0x7d0 (2000) | battery_rated_current high word |
521-
| 12 | 0xcb (203) | U_DWORD_R | battery_rated_power high byte of low word |
522-
| 13 | 0x20 (32) | spans 2 register | battery_rated_power low byte of low word |
523-
| 14 | 0x0 (0) | | battery_rated_power high byte of high word |
524-
| 15 | 0x0 (0) | 0x0000CB20 (52000) | battery_rated_power low byte of high word |
525-
| 16 | 0x0 (0) | U_WORD | charging_mode high byte |
526-
| 17 | 0x2 (02) | 0x2 (MPPT) | charging_mode low byte |
527-
| C | 0x2f (47) | | crc |
528-
| C | 0x31 (49) | | crc |
519+
| H | 0x12 (18) | | byte count |
520+
| 0 | 0x27 (39) | U_WORD | array_rated_voltage high byte |
521+
| 1 | 0x10 (16) | 0x2710 (100000) | array_rated_voltage low byte |
522+
| 2 | 0x7 (7) | U_WORD | array_rated_current high byte |
523+
| 3 | 0xd0 (208) | 0x7d0 (2000) | array_rated_current low byte |
524+
| 4 | 0xcb (203) | U_DWORD_R | array_rated_power high byte of low word |
525+
| 5 | 0x20 (32) | spans 2 register | array_rated_power low byte of low word |
526+
| 6 | 0x0 (0) | | array_rated_power high byte of high word |
527+
| 7 | 0x0 (0) | 0x0000CB20 (52000) | array_rated_power low byte of high word |
528+
| 8 | 0x9 (09) | U_WORD | battery_rated_voltage high byte |
529+
| 9 | 0x60 (96) | 0x960 (2400) | battery_rated_voltage low byte |
530+
| 10 | 0x7 (07) | U_WORD | battery_rated_current high word |
531+
| 11 | 0xd0 (208) | 0x7d0 (2000) | battery_rated_current high word |
532+
| 12 | 0xcb (203) | U_DWORD_R | battery_rated_power high byte of low word |
533+
| 13 | 0x20 (32) | spans 2 register | battery_rated_power low byte of low word |
534+
| 14 | 0x0 (0) | | battery_rated_power high byte of high word |
535+
| 15 | 0x0 (0) | 0x0000CB20 (52000) | battery_rated_power low byte of high word |
536+
| 16 | 0x0 (0) | U_WORD | charging_mode high byte |
537+
| 17 | 0x2 (02) | 0x2 (MPPT) | charging_mode low byte |
538+
| C | 0x2f (47) | | crc |
539+
| C | 0x31 (49) | | crc |
529540
530541
> [!NOTE]
531542
> Write support is only implemented for switches and selects; however, the C++ code provides the required API to write to a Modbus device.

0 commit comments

Comments
 (0)