• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..--

boards/29-Dec-2025-1411

src/29-Dec-2025-166121

CMakeLists.txtD29-Dec-2025200 95

README.rstD29-Dec-20254.4 KiB151107

app.overlayD29-Dec-2025269 1613

cdc-acm.overlayD29-Dec-2025267 1714

overlay-cdc-acm.confD29-Dec-2025166 54

prj.confD29-Dec-2025143 97

sample.yamlD29-Dec-20251.1 KiB3736

README.rst

1.. zephyr:code-sample:: modbus-rtu-server
2   :name: Modbus RTU server
3   :relevant-api: modbus
4
5   Implement a Modbus RTU server exposing Modbus commands to control LEDs.
6
7Overview
8********
9
10This is a simple application demonstrating a Modbus RTU server implementation
11in Zephyr RTOS.
12
13Requirements
14************
15
16This sample has been tested with the nRF52840-DK and FRDM-K64F boards,
17but it should work with any board that has a free UART interface or USB
18device controller. Additionally the board should have three LEDs.
19
20RTU server example is running on an evaluation board. Client is running
21on a PC or laptop.
22
23The description of this sample uses `PyModbus`_ (Pymodbus REPL).
24The user can of course try out other client implementations with this sample.
25
26Using RS-485 transceiver
27========================
28
29It is the default configuration of this sample.
30In addition to the evaluation board, an USB to RS-485 bus adapter and
31a RS-485 shield are required. The shield converts UART TX, RX signals to RS-485.
32An Arduino header compatible shield like `joy-it RS-485 shield for Arduino`_
33can be used. This example uses DE signal, which is controlled by pin D9
34on the JOY-IT shield. For other shields, ``de-gpios`` property must be adapted
35or removed in the application overlay file
36:zephyr_file:`samples/subsys/modbus/rtu_server/app.overlay`
37
38The USB to RS-485 adapter connects to the USB port of a computer.
39The two A+, B- lines should be connected to the RS-485 shield.
40
41Using CDC ACM UART
42==================
43
44Only an evaluation board with supported USB device controller is required.
45USB device port should be connected to the USB port of a computer.
46Although it is only a point to point connection and does not represent a bus,
47it can, apart from testing the server implementation, also be used practically
48for example to control relays or to read ADC values via USB connection without
49implementing custom USB class or driver.
50
51Building and Running
52********************
53
54The following commands build and flash RTU server sample.
55
56.. zephyr-app-commands::
57   :zephyr-app: samples/subsys/modbus/rtu_server
58   :board: nrf52840dk/nrf52840
59   :goals: build flash
60   :compact:
61
62The following commands build and flash RTU server sample using CDC ACM UART.
63
64.. zephyr-app-commands::
65   :zephyr-app: samples/subsys/modbus/rtu_server
66   :board: nrf52840dk/nrf52840
67   :goals: build flash
68   :gen-args: -DDTC_OVERLAY_FILE=cdc-acm.overlay -DEXTRA_CONF_FILE=overlay-cdc-acm.conf
69   :compact:
70
71On the client side, PC or laptop, the following command connects PyModbus
72to the RTU server. If CDC ACM UART is used, ttyUSB should be replaced by a
73matching ttyACM device.
74
75.. code-block:: console
76
77   # pymodbus.console serial --port /dev/ttyUSB0 --baudrate 19200
78                             --bytesize 8 --parity N --stopbits 2
79
80The LEDs on the board are controlled by Modbus commands FC01, FC05, FC15.
81For example, to set LED0 on use FC01 command (write_coil).
82
83.. code-block:: console
84
85   > client.write_coil address=0 value=1 slave=1
86
87Client should confirm successful communication and LED0 should light.
88
89.. code-block:: console
90
91   {
92       "address": 0,
93       "value": true
94   }
95
96To set LED0 off but LED1 and LED2 on use FC15 command (write_coils).
97
98.. code-block:: console
99
100   > client.write_coils address=0 values=0,1,1 slave=1
101
102To read LED0, LED1, LED2 state FC05 command (read_coils) can be used.
103
104.. code-block:: console
105
106   > client.read_coils address=0 count=3 slave=1
107   {
108       "bits": [
109           false,
110           true,
111           true,
112           false,
113           false,
114           false,
115           false,
116           false
117       ]
118   }
119
120It is also possible to write and read the holding registers.
121This however does not involve any special interaction
122with the peripherals on the board yet.
123
124To write single holding registers use FC06 command (write_register),
125
126.. code-block:: console
127
128   > client.write_register address=0 value=42 slave=1
129
130or FC16 command (write_registers).
131
132.. code-block:: console
133
134   > client.write_registers address=0 values=42,42,42 slave=1
135
136To read holding registers use FC03 command (read_holding_registers).
137
138.. code-block:: console
139
140   > client.read_holding_registers address=0 count=3 slave=1
141   {
142       "registers": [
143           42,
144           42,
145           42
146       ]
147   }
148
149.. _`joy-it RS-485 shield for Arduino`: https://joy-it.net/en/products/ARD-RS485
150.. _`PyModbus`: https://github.com/pymodbus-dev/pymodbus
151