Implementing a MODBUS Server for Serial Display Using AWTK
Model Name: modbus_server
Purpose: This model enables service provision via the MODBUS protocol, allowing remote clients (masters) to acces data.
Instantiation
A model instance is created using modbus_server.
Example:
<window v-model="modbus_server" name="main_view">
Configuration file location:
design/default/data/modbus.json
Configuration File Structure
The configuration uses a JSON format.
Basic Settings
- url: Defines the connection endpoint for the slave device.
- unit_id: Identifier for the slave device (not required for TCP).
Example entry:
"url": "tcp://localhost:502"
Channel Definitions
Multiple channels can be defined within the channels array.
Each channel supports specific MODBUS function codes based on its type:
bits: Accessible with function code 1 (read) and 15 (write).input_bits: Read-only using function code 2.registers: Read with function code 3; write with function code 16.input_registers: Read-only with function code 4.
Parameters include:
- name: Identifier used in UI bindings.
- start: Starting address of the register block.
- length: Number of registers/bits.
Sample definition:
"channels": [
{
"name": "bits",
"writable": true,
"start": 0,
"length": 100
},
{
"name": "input_bits",
"start": 0,
"length": 200
},
{
"name": "registers",
"writable": true,
"start": 0,
"length": 300
},
{
"name": "input_registers",
"start": 0,
"length": 400
}
]
Variable Mapping
To enhance usability, variables can map to specific addresses with in channels.
Variable declaration example:
"variables": {
"temperature_current": "input_registers.word[0]",
"humidity_current": "input_registers.word[1]",
"temperature_target": "registers.word[0]",
"humidity_target": "registers.word[1]"
}
Usage in UI components:
<label text="Connection Endpoint" />
<label v-data:value="{url}" />
<label text="Device Identifier" />
<label v-data:value="{unit_id}" />
<label text="Current Temperature" />
<edit input_type="int" v-data:text="{temperature_current}" />
<label text="Current Humidity" />
<edit input_type="int" v-data:text="{humidity_current}" />
<label text="Target Temperature (modified by client)" />
<label v-data:text="{temperature_target}" />
<label text="Target Humidity (modified by client)" />
<label v-data:text="{humidity_target}" />
Data Access
Refer to documentation on modbus_client for accessing channel data.
Predefined Properties
| Property | Type | Description |
|---|---|---|
| url | string | Connection URI |
| unit_id | int | Device identifier |
Example usage:
<label text="Endpoint" />
<label v-data:value="{url}" />
<label text="ID" />
<label v-data:value="{unit_id}" />
Available Commands
None available.
Reference Implementation
See demo project: demo_modbus_server_registers