0% found this document useful (0 votes)
89 views16 pages

AN0104v01-Setup of Modbus TCP Connection in CODESYS On u-OS

Uploaded by

Danny Manuel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views16 pages

AN0104v01-Setup of Modbus TCP Connection in CODESYS On u-OS

Uploaded by

Danny Manuel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Setup of Modbus TCP in CODESYS

Abstract:
This application note helps you creating your first Modbus TCP connection on a u-OS device
with CODESYS. It describes the configuration of both the Modbus TCP Server and the Modbus
TCP client in CODESYS.
Setup of Modbus TCP in CODESYS

Hardware reference
No. Component name Article No. Hardware / Firmware version
1 UC20-WL2000-AC 1334950000 u-OS 2.0.0
2 IoT-GW30 2682620000 u-OS 2.0.0
2682630000

Software reference
Article
No. Software name Software version
No.
1 CODESYS Development System SP18 Patch 4
2 CODESYS Runtime App 4.7.0.0-2
3 CODESYS Control SL for Weidmueller u-OS 4.7.0.0

Contact
Weidmüller Interface GmbH & Co. KG For any further support please contact your
Klingenbergstraße 26 local sales representative:
32758 Detmold, Germany https://www.weidmueller.com/countries
www.weidmueller.com

AN00104v1_2023/05 2
Setup of Modbus TCP in CODESYS

Content
1 Warning and Disclaimer .................................................................................................. 4
2 Introduction ..................................................................................................................... 5
3 Modbus TCP Master (Client) Configuration .................................................................... 6
3.1 Function codes ..............................................................................................................10
4 Modbus TCP Slave (Server) Configuration ....................................................................12
5 Function test ..................................................................................................................16

AN00104v1_2023/05 3
Setup of Modbus TCP in CODESYS

1 Warning and Disclaimer


Warning
Controls may fail in unsafe operating conditions, causing uncontrolled operation of the controlled
devices. Such hazardous events can result in death and / or serious injury and / or property
damage. Therefore, there must be safety equipment provided / electrical safety design or other
redundant safety features that are independent from the automation system.

Disclaimer
This Application Note / Quick Start Guide / Example Program does not relieve you of the obligation
to handle it safely during use, installation, operation and maintenance. Each user is responsible
for the correct operation of his control system. By using this Application Note / Quick Start Guide
/ Example Program prepared by Weidmüller, you accept that Weidmüller cannot be held liable for
any damage to property and / or personal injury that may occur because of the use.

Note
The given descriptions and examples do not represent any customer-specific solutions, they are
simply intended to help for typical tasks. The user is responsible for the proper operation of the
described products. Application notes / Quick Start Guides / Example Programs are not binding
and do not claim to be complete in terms of configuration as well as any contingencies. By using
this Application Note / Quick Start Guide / Example Program, you acknowledge that we cannot
be held liable for any damages beyond the described liability regime. We reserve the right to make
changes to this application note / quick start guide / example at any time without notice. In case
of discrepancies between the proposals Application Notes / Quick Start Guides / Program
Examples and other Weidmüller publications, like manuals, such contents have always more
priority to the examples. We assume no liability for the information contained in this document.
Our liability, for whatever legal reason, for damages caused using the examples, instructions,
programs, project planning and performance data, etc. described in this Application Note / Quick
Start Guide / Example is excluded.

Security notes
In order to protect equipment, systems, machines and networks against cyber threats, it is
necessary to implement (and maintain) a complete state-of-the-art industrial security concept.
The customer is responsible for preventing unauthorized access to his equipment, systems,
machines and networks. Systems, machines and components should only be connected to the
corporate network or the Internet if necessary and appropriate safeguards (such as firewalls and
network segmentation) have been taken.

AN00104v1_2023/05 4
Setup of Modbus TCP in CODESYS

2 Introduction
In most automation systems, sooner or later you are faced with the task of implementing a fieldbus
connection. Among other things, this may be necessary if external sensors are required, or
various I/O modules are to be integrated. In addition, a fieldbus system can be used to exchange
data between different control devices.
This application note explains how to setup a Modbus TCP communication with CODESYS. It
depends on your application if your controller needs to act as Modbus client or Modbus server.
We will describe both variants by providing an example for a Modbus TCP communication
between two CODESYS controllers (Client and Server).

AN00104v1_2023/05 5
Setup of Modbus TCP in CODESYS

3 Modbus TCP Master (Client) Configuration


Open a new CODESYS standard project for the Master application.

1. First add an "Ethernet node" to the CODESYS device in the device tree ⇒ Device (right click)
⇒ Add Device...

Figure 1: Add Device

2. Ethernet Adapter ⇒ Ethernet ⇒ Add Device

Figure 2: Ethernet device

AN00104v1_2023/05 6
Setup of Modbus TCP in CODESYS

3. Then attach the Modbus TCP Master to the previously inserted Ethernet element. In the
device tree ⇒ Ethernet (right click) ⇒ Add device... ⇒ Modbus TCP Master

Figure 3: Add Modbus Master

4. The Modbus TCP Master is now available below the Ethernet node in the device tree. First
the Ethernet must be parameterized. To do this, open the configurator (by double-clicking on
Ethernet) and click on the button Browse… behind the Network interface.

Figure 4: Network interface

AN00104v1_2023/05 7
Setup of Modbus TCP in CODESYS

5. In the following dialogue, select "eth0" or "eth1“ or another suitable adapter for your controller,
depending on which connection is to be used.

Figure 5: eth0

Note:
To configure the Network interface, you must be connected to the controller.

6. You also need to set up the Modbus TCP master: Open the Modbus Master editor and adjust
the settings. It is recommended to activate the option for auto-reconnect in the "General" tab.
All the settings for the master are completed.

7. Now the slave needs to link to the master. Add the slave as a node element under the master
in the device editor. In the device tree ⇒ Modbus_TCP_Master (right click) ⇒ Add device... ⇒
Modbus TCP Slave

AN00104v1_2023/05 8
Setup of Modbus TCP in CODESYS

Figure 6: Modbus slave

8. Now there is a slave element in the device tree. Open the slave device editor and enter the
IP address of the slave device. Also check if the same port is selected for both devices (default
502).

AN00104v1_2023/05 9
Setup of Modbus TCP in CODESYS

Figure 7: IP Address

The General tab has the following settings for Modbus TCP:
- Slave IP Address - the IP address of the slave.
- Response Timeout - the time interval in which the master waits for the response of the
slave device. The timeout specified here overrides the general "Response Timeout"
setting of the associated master.
- Port - Port number of the slave (default 502)

3.1 Function codes


Now the function codes to transmit data needs to be specified. To do this, add the channels in
the tab "Modbus Slave Channel". There are several function codes. Please see the Modbus
specification for more information.

In the example program, the function code 23 (Read/Write Multiple Registers) to read and write
values simultaneously is used, the data length is 4 bytes (2 words), and the register address starts
for reading at 0 and writing at 16#10.

Of course, you need to adjust the values according to the amount of data you need. Click in the
menu Modbus Slave Channel ⇒ Add Channel... and you will see the following dialogue with the
parameters already mentioned above.

AN00104v1_2023/05 10
Setup of Modbus TCP in CODESYS

Figure 8: Add Channel

The project for the Modbus TCP Master application is configured and now ready for download to
the controller.

Note:
If the CODESYS license is not activated, the icon at Modbus TCP device is orange
after download and login. With license the icon should be green.

AN00104v1_2023/05 11
Setup of Modbus TCP in CODESYS

4 Modbus TCP Slave (Server) Configuration


Open a second standard CODESYS project for the Modbus TCP Slave device.

1. Insert an "Ethernet node" in the device tree ⇒ Device (right-click) ⇒ Add device...

Figure 9: Add Ethernet adapter

2. Now add the Modbus TCP slave device to the “ethernet adapter”. ⇒ Ethernet (select) ⇒ right-
click ⇒ Attach Device... ⇒ Modbus ⇒ Modbus TCP Slave Device ⇒ Add Device ⇒ Close.

AN00104v1_2023/05 12
Setup of Modbus TCP in CODESYS

Figure 10: Add Modbus TCP client

3. Edit the network settings in the Ethernet node ⇒ Ethernet (select) ⇒ General ⇒ Network
interface [...]. Select the Modbus communication port of your device.

Figure 11: Edit Network Settings

AN00104v1_2023/05 13
Setup of Modbus TCP in CODESYS

Note:
Use an IP address of the same subnet, but not the same IP Address.

4. Now, some Modbus Slave parameters need to be set. Open the configuration menu by
double-clicking on the entry of the Modbus TCP slave in the device tree.

The tab "General" has the following parameters:

- Watchdog - the maximum amount of time the slave unit expects a Modbus message.
- Slave Port - port number of the slave (default 502).
- Holding Registers - Number of holding registers
- Writeable – Write access to the holding register
- Input Registers - Number of input registers

Edit the settings of the slave device. In the example the parameters were selected to match
the Modbus TCP Master settings.

Figure 12: Modbus Slave settings

AN00104v1_2023/05 14
Setup of Modbus TCP in CODESYS

Depending on which data model you choose, you must adjust the parameters there. If the memory
blocks are separated, you must specify different start addresses for each function code
(depending on the arrangement and size of the sections).
In the example the start addresses remains at "0". Now you can use the individual function codes
to access a WORD or a BIT of your memory area.

AN00104v1_2023/05 15
Setup of Modbus TCP in CODESYS

5 Function test
The configuration is now complete. The program for master and slave can be loaded to the
controllers. To write values to the registers and check the functionality, open the IO-mapping of
both devices and login.

Figure 13: Test

It is possible to force values for writing data to the slave (Holding Register 16 and 17) and it is
also possible to read data from the slave (Register 0 and 1).

AN00104v1_2023/05 16

You might also like