Skip to content

Commit 0b0f5ad

Browse files
committed
Added docs for write operations
1 parent 0eeaaf2 commit 0b0f5ad

File tree

1 file changed

+80
-6
lines changed

1 file changed

+80
-6
lines changed

README.md

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,17 @@ This includes the basic configuration for the plugin such as poll rate and the c
9191
- connection to thin-edge.io (MQTT broker needs to match the one of tedge)
9292
- log level (e.g. INFO, WARN, ERROR)
9393
- measurement combination (opt-in feature to reduce the amount of created measurements in the cloud)
94+
- subscribe_topics (MQTT topics for receiving write commands)
9495

9596
### devices.toml
9697

9798
This file includes the information for the connection(s) to the Modbus server(s) and how the Modbus Registers and Coils map to thin-edge’s Measurements, Events and Alarms. It's also possible to overwrite the measurement combination on a device level and on every single measurement mapping.
9899

99100
The device config can be managed via Cumulocity IoT or created with the Cloud Fieldbus operations.
100101

102+
Key configuration fields:
103+
- `name` - Required for name-based write operations. Used to match metrics in write commands.
104+
101105
### Updating the config files
102106

103107
A watchdog observer will take care of restarting the MQTT and Modbus connection when either
@@ -172,11 +176,47 @@ For adding a modbus RTU device you need to use unit-ID of the slave device in th
172176

173177
### Write operations
174178

175-
The plugin supports writing to Modbus registers and coils through Cumulocity IoT operations.
179+
The plugin supports writing to Modbus registers and coils through thin-edge.io commands (modbus_SetRegister/modbus_SetCoil) or Cumulocity IoT operations (c8y_SetRegister/c8y_SetCoil).
180+
181+
#### thin-edge.io Commands
182+
183+
The plugin subscribes to MQTT topics for receiving write commands:
184+
- `te/device/+///cmd/modbus_SetRegister/+` - for writing to registers
185+
- `te/device/+///cmd/modbus_SetCoil/+` - for writing to coils
186+
187+
These commands are processed directly by the modbus reader service. The Cumulocity operations (c8y_SetRegister/c8y_SetCoil) are converted to thin-edge.io commands via operation templates.
188+
189+
#### Payload Formats
190+
191+
The plugin supports two payload formats for write operations:
192+
193+
1. **Explicit address format** - directly specify register/coil address and parameters
194+
2. **Name-based format** - use metrics array with name field to match configuration in devices.toml
195+
196+
For the name-based format, the `name` field in devices.toml registers/coils configuration is required to match the metrics. The matching uses prefix matching: if the metric name starts with the configured name, it will match. For example, a metric name `"test_test_setPoint"` will match a register with `name = "test_test"` in devices.toml.
197+
198+
**Command Status Flow:**
199+
- Commands with `"status": "init"` are automatically converted to `"executing"` by the plugin
200+
- The plugin processes the command and updates status to `"successful"` or `"failed"`
201+
- The topic format must be: `te/device/<device-name>///cmd/modbus_SetRegister/<mapper-id>` where `<device-name>` must match a device name in devices.toml
202+
203+
Example name-based payload input for modbus plugin:
204+
```json
205+
{
206+
"status": "init",
207+
"metrics": [{
208+
"name": "Test_Int16_xxxxxxxx",
209+
"timestamp": "2025-09-23T01:00:00Z",
210+
"value": 123
211+
}]
212+
}
213+
```
214+
215+
#### Write to Register (c8y_SetRegister / modbus_SetRegister)
176216

177-
#### Write to Register (c8y_SetRegister)
217+
Write values to Modbus registers. The plugin supports both integer and float register writes.
178218

179-
Write values to Modbus registers requires the following json to be included in the operation:
219+
**Explicit Address Format:**
180220

181221
```json
182222
{
@@ -192,6 +232,26 @@ Write values to Modbus registers requires the following json to be included in t
192232
}
193233
```
194234

235+
236+
**Integer Register Writes:**
237+
- For integer registers, the plugin performs a read-modify-write operation
238+
- It first reads the current register value, applies bit masking based on `startBit` and `noBits`, then writes the updated value
239+
- This ensures that only the specified bits are modified while preserving other bits in the register
240+
241+
**Float Register Writes (Name-based format only):**
242+
- Float values are supported for registers configured with `datatype = "float"` in devices.toml
243+
- Supported float sizes: 16-bit (half precision), 32-bit (single precision), 64-bit (double precision)
244+
- The plugin automatically handles endianness conversion based on the device's `littlewordendian` setting
245+
- Float writes require the name-based format and proper configuration in devices.toml
246+
247+
**Protocol Support:**
248+
- TCP/IP Modbus: Specify `ipAddress` and ensure the device is reachable
249+
- RTU Modbus: Device must be configured in devices.toml with serial port settings
250+
251+
**Error Handling:**
252+
- If the Modbus device is offline or unreachable, the command will immediately fail with status `"failed"`
253+
- No automatic retry mechanism is implemented; failed commands must be resent
254+
195255
To create an operation to write a value to a Modbus register using Cumulocity, you can use the [go-c8y-cli](https://goc8ycli.netlify.app/docs/cli/c8y/operations/c8y_operations_create/) tool:
196256

197257
```
@@ -205,10 +265,11 @@ In the Cumulocity UI, the widget '[Asset table](https://cumulocity.com/docs/cock
205265
![Image](./doc/write-register-asset-table.png)
206266

207267

208-
#### Write to Coil (c8y_SetCoil)
268+
#### Write to Coil (c8y_SetCoil / modbus_SetCoil)
269+
270+
Write values to Modbus coils. Coils are single-bit outputs that can be set to 0 or 1.
209271

210-
Write values to Modbus coils requires the following json to be included in the operation:
211-
:
272+
**Explicit Address Format:**
212273

213274
```json
214275
{
@@ -222,6 +283,19 @@ Write values to Modbus coils requires the following json to be included in the o
222283
}
223284
```
224285

286+
**Name-based Format:**
287+
- Coils can be written using the name-based format with metrics array
288+
- The coil `name` in devices.toml must match the metric name (using prefix matching)
289+
- The metric value must be 0 or 1
290+
291+
**Protocol Support:**
292+
- TCP/IP Modbus: Specify `ipAddress` and ensure the device is reachable
293+
- RTU Modbus: Device must be configured in devices.toml with serial port settings
294+
295+
**Error Handling:**
296+
- If the Modbus device is offline or unreachable, the command will immediately fail with status `"failed"`
297+
- No automatic retry mechanism is implemented; failed commands must be resent
298+
225299
To create an operation to write a value to a Modbus coil using Cumulocity, you can use the [go-c8y-cli](https://goc8ycli.netlify.app/docs/cli/c8y/operations/c8y_operations_create/) tool:
226300

227301
```

0 commit comments

Comments
 (0)