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