github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/doc/content/en/docs/plugins/modbus/modbus_rtu.md (about) 1 --- 2 title: "Modbus rtu" 3 linkTitle: "modbus rtu" 4 date: 2021-10-20 5 description: > 6 7 --- 8 9 In the **Smart Home** system, there is a Modbus RTU plugin implemented that enables interaction with devices using the Modbus RTU protocol. The plugin provides the `ModbusRtu(FUNC, ADDRESS, COUNT, COMMAND)` method, which allows sending commands and receiving data using the specified protocol. 10 11 Arguments of the `ModbusRtu` method: 12 13 1. `FUNC`: Modbus function code indicating the type of operation to perform with the device. 14 2. `ADDRESS`: Modbus device address to which the command is sent. 15 3. `COUNT`: Number of registers or bits to read or write. 16 4. `COMMAND`: Command to be executed, indicating additional parameters and settings. 17 18 Usage example: 19 20 ```javascript 21 const COMMAND = [] 22 const FUNC = 'ReadHoldingRegisters' 23 const ADDRESS = 0 24 const COUNT = 16 25 26 const response = ModbusRtu(FUNC, ADDRESS, COUNT, COMMAND); 27 console.log(response); 28 ``` 29 30 The `ModbusRtu` method allows sending Modbus RTU commands, performing register reads or writes, and retrieving data from devices. You can use this method in your **Smart Home** project to interact with devices that support the Modbus RTU protocol. 31 32 {{< alert color="warning" >}}To work with a Modbus RTU device, a configured **node** is required.{{< /alert >}} 33 34 ### Configuration: 35 36 * slave_id `1-32` 37 * baud `9600, 19200` 38 * data_bits `5-9` 39 * timeout `milliseconds` 40 * stop_bits `1-2` 41 * sleep `milliseconds` 42 * parity `none, odd, even` 43 44 ### Commands: 45 46 * Custom set 47 48 ### Attributes: 49 50 * Custom set 51 52 ### Status: 53 54 * Custom set 55 56 ---------------- 57 58 ### Available Functions 59 60 **1-bit functions** 61 62 ReadCoils 63 ReadDiscreteInputs 64 WriteSingleCoil 65 WriteMultipleCoils 66 67 **16-bit functions** 68 69 ReadInputRegisters 70 ReadHoldingRegisters 71 ReadWriteMultipleRegisters 72 WriteSingleRegister 73 WriteMultipleRegisters 74 75 ---------------- 76 77 ### JavaScript Properties 78 79 * ENTITY_ID 80 * ModbusRtu 81 * entityAction 82 * Action 83 84 ```coffeescript 85 # Constant with a unique device ID 86 const ENTITY_ID 87 ```` 88 89 ```coffeescript 90 # Execute a command (function) on the device: 91 result = ModbusRtu(func, addr, count, command) 92 ``` 93 94 | Value | Description | 95 |-------------|---------| 96 | func | Function to be executed on the device | 97 | addr | Address of the first register (40108-40001 = 107 = 6B hex) | 98 | count | Number of registers to read (reading 3 registers from 40108 to 40110) | 99 | command | Command | 100 101 102 ```coffeescript 103 # Event handler function for actions: 104 entityAction = (entityId, actionName, args)-> 105 ``` 106 107 | Value | Description | 108 |-------------|---------| 109 | entityId | Unique ID of the device | 110 | actionName | System name of the action | 111 | args | Type: map[string]any | 112 113 {{< alert color="warning" >}}The **Action** object is available in action scripts and scripts attached to the device.{{< /alert >}} 114 ```coffeescript 115 state = { 116 new_state: 'ENABLED', 117 attribute_values: { 118 heat: false 119 }, 120 settings_value: {}, 121 storage_save: true 122 } 123 # Save the state 124 EntitySetState ENTITY_ID, state 125 ``` 126 127 | Value | Description | 128 |-------------|---------| 129 | new_state | Unique system name of the state | 130 | attribute_values | Values of attributes previously defined for the device | 131 | settings_value | Values of settings previously defined for the device | 132 | storage_save | Flag indicating whether to save the state 133 134 snapshot in the database | 135 136 ---------------- 137 138 ### Example CoffeeScript code 139 140 ```coffeescript 141 # ModbusRtu 142 # ################################## 143 "use strict"; 144 145 checkStatus = -> 146 COMMAND = [] 147 FUNC = 'ReadHoldingRegisters' 148 ADDRESS = 0 149 COUNT = 16 150 151 res = ModbusRtu FUNC, ADDRESS, COUNT, COMMAND 152 print res.error 153 print res.result 154 print res.time 155 156 entityAction = (entityId, actionName, args)-> 157 switch actionName 158 when 'ON' then doOnAction() 159 when 'OFF' then doOffAction() 160 when 'CHECK' then checkStatus() 161 when 'ON_WITH_ERR' then doOnErrAction() 162 163 ``` 164 165