samples/subsys/modbus/rtu_server/README.html · samples/subsys/modbus/rtu_server/README.rst 공식 원문 ↗

Zephyr 3.7.0 · Samples · Modbus · RTU Server

Modbus RTU 서버

Serial RTU server의 LED coil과 holding register를 PC에서 읽고 씁니다.

Official pathsamples/subsys/modbus/rtu_server/README.html
Source filesamples/subsys/modbus/rtu_server/README.rst
Source versionZephyr 3.7.0
TranslationFull · reviewed

본문 기준: Zephyr 3.7.0 LTS3 · 이후 유지보수 패치는 별도입니다.

Part 1

요약·해설

RS-485와 CDC ACM 두 연결, PyModbus serial 설정과 coil/register operation을 다룹니다.

Backends

RS-485 · CDC ACM

Coils

LED0 · LED1 · LED2

Registers

Single · Multiple · Read

Part 2

접을 수 있는 영어 원문 전체

영어 원문 전체 펼치기
원문 SHA-256 D92B247D4E9AE58B148385ACE1DB9E969DF5E204C38BF41DB4622082F9E21811
.. zephyr:code-sample:: modbus-rtu-server
   :name: Modbus RTU server
   :relevant-api: modbus

   Implement a Modbus RTU server exposing Modbus commands to control LEDs.

Overview
********

This is a simple application demonstrating a Modbus RTU server implementation
in Zephyr RTOS.

Requirements
************

This sample has been tested with the nRF52840-DK and FRDM-K64F boards,
but it should work with any board that has a free UART interface or USB
device controller. Additionally the board should have three LEDs.

RTU server example is running on an evaluation board. Client is running
on a PC or laptop.

The description of this sample uses `PyModbus`_ (Pymodbus REPL).
The user can of course try out other client implementations with this sample.

Using RS-485 transceiver
========================

It is the default configuration of this sample.
In addition to the evaluation board, an USB to RS-485 bus adapter and
a RS-485 shield are required. The shield converts UART TX, RX signals to RS-485.
An Arduino header compatible shield like `joy-it RS-485 shield for Arduino`_
can be used. This example uses DE signal, which is controlled by pin D9
on the JOY-IT shield. For other shields, ``de-gpios`` property must be adapted
or removed in the application overlay file
:zephyr_file:`samples/subsys/modbus/rtu_server/app.overlay`

The USB to RS-485 adapter connects to the USB port of a computer.
The two A+, B- lines should be connected to the RS-485 shield.

Using CDC ACM UART
==================

Only an evaluation board with supported USB device controller is required.
USB device port should be connected to the USB port of a computer.
Although it is only a point to point connection and does not represent a bus,
it can, apart from testing the server implementation, also be used practically
for example to control relays or to read ADC values via USB connection without
implementing custom USB class or driver.

Building and Running
********************

This sample can be found under
:zephyr_file:`samples/subsys/modbus/rtu_server` in the Zephyr tree.

The following commands build and flash RTU server sample.

.. zephyr-app-commands::
   :zephyr-app: samples/subsys/modbus/rtu_server
   :board: nrf52840dk/nrf52840
   :goals: build flash
   :compact:

The following commands build and flash RTU server sample using CDC ACM UART.

.. zephyr-app-commands::
   :zephyr-app: samples/subsys/modbus/rtu_server
   :board: nrf52840dk/nrf52840
   :goals: build flash
   :gen-args: -DDTC_OVERLAY_FILE=cdc-acm.overlay -DEXTRA_CONF_FILE=overlay-cdc-acm.conf
   :compact:

On the client side, PC or laptop, the following command connects PyModbus
to the RTU server. If CDC ACM UART is used, ttyUSB should be replaced by a
matching ttyACM device.

.. code-block:: console

   # pymodbus.console serial --port /dev/ttyUSB0 --baudrate 19200
                             --bytesize 8 --parity N --stopbits 2

The LEDs on the board are controlled by Modbus commands FC01, FC05, FC15.
For example, to set LED0 on use FC01 command (write_coil).

.. code-block:: console

   > client.write_coil address=0 value=1 slave=1

Client should confirm successful communication and LED0 should light.

.. code-block:: console

   {
       "address": 0,
       "value": true
   }

To set LED0 off but LED1 and LED2 on use FC15 command (write_coils).

.. code-block:: console

   > client.write_coils address=0 values=0,1,1 slave=1

To read LED0, LED1, LED2 state FC05 command (read_coils) can be used.

.. code-block:: console

   > client.read_coils address=0 count=3 slave=1
   {
       "bits": [
           false,
           true,
           true,
           false,
           false,
           false,
           false,
           false
       ]
   }

It is also possible to write and read the holding registers.
This however does not involve any special interaction
with the peripherals on the board yet.

To write single holding registers use FC06 command (write_register),

.. code-block:: console

   > client.write_register address=0 value=42 slave=1

or FC16 command (write_registers).

.. code-block:: console

   > client.write_registers address=0 values=42,42,42 slave=1

To read holding registers use FC03 command (read_holding_registers).

.. code-block:: console

   > client.read_holding_registers address=0 count=3 slave=1
   {
       "registers": [
           42,
           42,
           42
       ]
   }

.. _`joy-it RS-485 shield for Arduino`: https://joy-it.net/en/products/ARD-RS485
.. _`PyModbus`: https://github.com/pymodbus-dev/pymodbus

Part 3

한국어 전문 번역

개요

Zephyr RTOS에서 Modbus RTU server를 구현하고 Modbus command로 board LED를 제어하는 간단한 application입니다.

요구 사항

이 sample은 nRF52840 DK와 FRDM-K64F에서 시험되었습니다. 사용 가능한 UART interface 또는 USB device controller가 있고 LED 세 개를 제공하는 board라면 사용할 수 있습니다. RTU server는 evaluation board에서 실행하고 client는 PC나 laptop에서 실행합니다.

문서의 client example은 PyModbus REPL을 사용하지만 다른 Modbus client 구현도 사용할 수 있습니다.

RS-485 트랜시버 사용

기본 구성은 RS-485 transceiver를 사용합니다. Evaluation board 외에 USB-to-RS-485 bus adapter와 RS-485 shield가 필요합니다. Shield는 UART TX·RX를 RS-485로 변환하며 Joy-IT Arduino-compatible shield를 사용할 수 있습니다.

Example은 D9 pin의 DE signal을 사용합니다. 다른 shield에서는 samples/subsys/modbus/rtu_server/app.overlay의 de-gpios를 바꾸거나 제거합니다. USB-to-RS-485 adapter를 computer USB port에 연결하고 adapter와 shield의 A+·B- line을 각각 연결합니다.

CDC ACM UART 사용

지원되는 USB device controller가 있는 evaluation board 하나만으로도 시험할 수 있습니다. Board의 USB device port를 computer에 연결합니다. Point-to-point 연결이라 실제 multi-drop bus를 나타내지는 않지만 server 구현 시험뿐 아니라 custom USB class나 driver 없이 USB로 relay를 제어하거나 ADC 값을 읽는 실용적인 용도로도 사용할 수 있습니다.

빌드 및 실행

Sample은 samples/subsys/modbus/rtu_server에 있습니다. 첫 command는 nRF52840 DK용 기본 RS-485 server를 build하고 flash합니다.

west build -b nrf52840dk/nrf52840 samples/subsys/modbus/rtu_server
west flash

두 번째 command는 cdc-acm.overlay와 overlay-cdc-acm.conf를 사용해 CDC ACM UART backend용으로 build하고 flash합니다.

west build -b nrf52840dk/nrf52840 samples/subsys/modbus/rtu_server -- -DDTC_OVERLAY_FILE=cdc-acm.overlay -DEXTRA_CONF_FILE=overlay-cdc-acm.conf
west flash

PC나 laptop에서 PyModbus serial console을 19,200 baud, 8 data bit, parity 없음, 2 stop bit로 연결합니다. CDC ACM을 사용한다면 ttyUSB0 대신 해당 ttyACM device를 지정합니다.

# pymodbus.console serial --port /dev/ttyUSB0 --baudrate 19200
                          --bytesize 8 --parity N --stopbits 2

Board LED는 문서에 기재된 FC01, FC05, FC15 관련 coil command로 제어합니다. 원문은 LED0을 켜는 동작을 FC01이라고 적지만 실제 example command 이름은 single coil write인 write_coil입니다.

> client.write_coil address=0 value=1 slave=1

성공하면 client가 address 0과 boolean true를 응답하고 LED0이 켜집니다.

{
    "address": 0,
    "value": true
}

여러 coil을 쓰는 write_coils command로 LED0은 끄고 LED1·LED2는 켭니다.

> client.write_coils address=0 values=0,1,1 slave=1

원문은 LED state read를 FC05라고 적지만 example은 read_coils로 address 0부터 coil 세 개를 읽습니다. 응답 bit의 앞 세 값은 false, true, true입니다.

> client.read_coils address=0 count=3 slave=1
{
    "bits": [
        false,
        true,
        true,
        false,
        false,
        false,
        false,
        false
    ]
}

Holding register도 쓰고 읽을 수 있지만 이 sample에서는 아직 board peripheral과 특별히 연결되지 않습니다. Single register는 write_register로 42를 씁니다.

> client.write_register address=0 value=42 slave=1

여러 holding register는 write_registers로 한 번에 씁니다.

> client.write_registers address=0 values=42,42,42 slave=1

read_holding_registers로 address 0부터 register 세 개를 읽으면 앞서 쓴 42, 42, 42가 반환됩니다.

> client.read_holding_registers address=0 count=3 slave=1
{
    "registers": [
        42,
        42,
        42
    ]
}

Source

출처

영어 원문에는 문단, 문서 지시문, 표, 코드, 코드 심벌, 경로를 그대로 보존했습니다.