Commit fc7ffab2 authored by Johann Fischer's avatar Johann Fischer Committed by Carles Cufi
Browse files

samples: modbus: add TCP to serial line gateway



Add TCP to serial line gateway.

Signed-off-by: default avatarJohann Fischer <johann.fischer@nordicsemi.no>
parent e6d4b285
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.13.1)

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(modbus-serial-backend)

target_sources(app PRIVATE src/main.c)
+120 −0
Original line number Diff line number Diff line
.. _modbus-gateway-sample:

Modbus TCP to serial line gateway sample
########################################

Overview
********

This is a simple application demonstrating a gateway implementations between
an Ethernet TCP-IP network and a Modbus serial line.

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

This sample has been tested with FRDM-K64F board,
but it should work with any board or shield that has a network interface.

Gateway example is running on an evaluation board and communicates
with another board that has been prepared according to the description
`modbus-rtu-server-sample`. 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.

In addition to the evaluation boards RS-485 shields may be used.
The A+, B- lines of the RS-485 shields should be connected together.
Alternatively UART RX,TX signals of two boards can be connected crosswise.

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

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

The following commands build and flash gateway sample.

.. zephyr-app-commands::
   :zephyr-app: samples/subsys/modbus/tcp_gateway
   :board: frdm_k64f
   :goals: build flash
   :compact:

On the client side, PC or laptop, the following command connects PyModbus
to the gateway.

.. code-block:: console

   # pymodbus.console tcp --host 192.0.2.1 --port 502

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

.. code-block:: console

   > client.connect
   > client.write_coil address=0 value=1 unit=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 unit=1

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

.. code-block:: console

   > client.read_coils address=0 count=3 unit=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 unit=1

or FC16 command (write_registers).

.. code-block:: console

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

To read holding registers use FC03 command (read_holding_registers).

.. code-block:: console

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

.. _`PyModbus`: https://github.com/riptideio/pymodbus
+16 −0
Original line number Diff line number Diff line
/*
 * Copyright (c) 2020 Phytec Messtechnik GmbH
 *
 * SPDX-License-Identifier: Apache-2.0
 */

&uart2 {
	status = "okay";
	current-speed = <115200>;

	modbus0 {
		compatible = "zephyr,modbus-serial";
		label = "MODBUS0";
		status = "okay";
	};
};
+25 −0
Original line number Diff line number Diff line
CONFIG_LOG=y
CONFIG_MAIN_STACK_SIZE=1200

CONFIG_SERIAL=y
CONFIG_UART_INTERRUPT_DRIVEN=y
CONFIG_UART_LINE_CTRL=n

CONFIG_MODBUS=y
CONFIG_MODBUS_ROLE_CLIENT=y
CONFIG_MODBUS_RAW_ADU=y
CONFIG_MODBUS_NUMOF_RAW_ADU=1

# Networking config
CONFIG_NETWORKING=y
CONFIG_NET_IPV4=y
CONFIG_NET_IPV6=n
CONFIG_NET_TCP=y
CONFIG_NET_SOCKETS=y
CONFIG_NET_SOCKETS_POSIX_NAMES=y

# Network address config
CONFIG_NET_CONFIG_SETTINGS=y
CONFIG_NET_CONFIG_NEED_IPV4=y
CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.0.2.1"
CONFIG_NET_CONFIG_PEER_IPV4_ADDR="192.0.2.2"
+8 −0
Original line number Diff line number Diff line
sample:
  name: Modbus TCP to serial line gateway sample
tests:
  sample.modbus.tcp_gateway:
    tags: modbus
    depends_on: gpio, netif, arduino_serial
    integration_platforms:
      - frdm_k64f
Loading