0% found this document useful (0 votes)
28 views37 pages

Application Note - Modbus Communication

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)
28 views37 pages

Application Note - Modbus Communication

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/ 37

Table Of Content

1. Hardware Configuration: ..................................................................................................... 3


1.1. Modbus Master:........................................................................................................... 4
1.2.2. Step 1: “General” Tab – ....................................................................................... 4
1.2.3. Step 2: “Addresses” Tab – ................................................................................... 5
1.2.4. Step 3: “Basic Parameters” Tab – ......................................................................... 6
1.2.5. Step 4: “Parameter” Tab –.................................................................................... 7
1.2.6. Step 4.1: “General” Tab – .................................................................................... 9
1.2.7. Step 4.2: “Modbus-Master” Tab – ...................................................................... 11
1.2.8. Step 4.3: “Interface” Tab – ................................................................................. 11
1.2.9. Step 5: Before Load Drivers – ............................................................................ 12
1.2.10. Step 6: Load Drivers – ....................................................................................... 12
1.2. Modbus Slave: ........................................................................................................... 12
1.2.1. Step 1: “General” Tab - ...................................................................................... 12
1.2.2. Step 2: “Addresses” Tab – ................................................................................. 13
1.2.3. Step 3: “Basic Parameters” Tab – ....................................................................... 14
1.2.4. Step 4: “Parameter” Tab –.................................................................................. 15
1.2.5. Step 4.1: “General” Tab – .................................................................................. 17
1.2.6. Step 4.2: “Modbus-Slave” Tab – ........................................................................ 19
1.2.7. Step 4.3: “FC 01,05,15” Tab – ........................................................................... 19
1.2.8. Step 4.4: “FC 02” Tab – ..................................................................................... 20
1.2.9. Step 4.5: “FC 03,06,16” Tab – ........................................................................... 21
1.2.10. Step 4.6: “FC 04” Tab – ..................................................................................... 21
1.2.11. Step 4.7: “Limits” Tab – .................................................................................... 22
1.2.12. Step 4.8: “Interface” Tab – ................................................................................. 24
1.2.13. Step 5: Before Load Drivers – ............................................................................ 24
1.2.14. Step 6: Load Drivers – ....................................................................................... 24
2. Progrmming: ..................................................................................................................... 25
2.1. Query from Modbus Master:...................................................................................... 25
2.2. STL program for Modbus Master: ............................................................................. 26
2.3. STL program for Modbus Slave:................................................................................ 28
Figures

Figure 1: ‘General’ Property of Modbus Master .......................................................................... 5


Figure 2: ‘Addresses’ Property of Modbus Master ...................................................................... 6
Figure 3: ‘Basic Parameters’ Property of Modbus Master............................................................ 7
Figure 4: ‘Assigning Parameters to Point To Point Connection’ Window .................................... 8
Figure 5: Protocol Selection ........................................................................................................ 9
Figure 6: ‘General’ Protocol property of Modbus Master .......................................................... 10
Figure 7: ‘Modbus-Master’ Protocol property of Modbus Master .............................................. 11
Figure 8: ‘Interface’ Protocol property of Modbus Master ......................................................... 11
Figure 9: ‘Load Drivers’ in CP – 341 for Modbus Master ......................................................... 12
Figure 10: ‘General’ Property of Modbus Slave ........................................................................ 13
Figure 11: ‘Addresses’ Property of Modbus Slave ..................................................................... 14
Figure 12: ‘Basic Parameters’ Property of Modbus Slave .......................................................... 15
Figure 13: ‘Assigning Parameters to Point To Point Connection’ Window ................................ 16
Figure 14: Protocol Selection .................................................................................................... 17
Figure 15: ‘General’ Protocol property of Modbus Slave .......................................................... 18
Figure 16: ‘Modbus-Slave’ Protocol property of Modbus Slave ................................................ 19
Figure 17: ‘FC 01,05,15’ Protocol property of Modbus Slave.................................................... 20
Figure 18: ‘FC 02’ Protocol property of Modbus Slave ............................................................. 20
Figure 19: ‘FC 03,06,16’ Protocol property of Modbus Slave.................................................... 21
Figure 20: ‘FC 04’ Protocol property of Modbus Slave ............................................................. 22
Figure 21: ‘Limits’ Protocol property of Modbus Slave ............................................................. 22
Figure 22: Message for Memory Bits Limits ............................................................................. 23
Figure 23: Message for Outputs Limits ..................................................................................... 23
Figure 24: ‘Interface’ Protocol property of Modbus Slave ......................................................... 24
Figure 25: ‘Load Drivers’ in CP – 341 for Modbus Slave .......................................................... 25
Figure 26: Query from Modbus Master ..................................................................................... 26
Figure 27: STL program for Modbus Master ............................................................................. 27
Figure 28: Modbus Master DB to store data from slave ............................................................. 28
Figure 29: STL program for Modbus Slave ............................................................................... 29
Figure 30: Modbus Slave DB for internal data storage .............................................................. 29
MODBUS (RTU) Communication

This application note is covering “MODBUS (RTU) Communication using CP-341


Module”. Basically to communicate between CPU & CP-341 Module we required following
things:
“Dongle” i.e. hardware to make module as MODBUS Master or MODBUS Slave.
“CP PtP” software to allow SIMATIC to parameterize CP-341 module..
Drivers for Modbus Master or Slave as per requirement.
STEP 7 software.
Once you completed above things, you can start with the Modbus communication.
For this first you have to configure hardware in SIMATIC Hardware configuration & then you
have to write FB’s & DB’s for communication.

1. Hardware Configuration:
Open “Hardware Configuration” window from SIMATIC Manager.
Select CP – 341 RS422/485 Module (with the right MLFB No.) from hardware catalog.
Go to Profibus DP ET 200M IM 153 – X CP – 300 Point to Point CP 341
CP 341 RS422/485.
Add these modules in the racks as per requirement or given hardware architecture e.g.
screen shown below.
1.1. Modbus Master:
Go to properties of CP-341 module & do the settings as shown in figures below.

1.2.2. Step 1: “General” Tab –


Figure 1: ‘General’ Property of Modbus Master

1.2.3. Step 2: “Addresses” Tab –


Remember this start address of the module because we are using this address in
the programming. It is recommended to have same start address for input & output as
shown in figure below.
Figure 2: ‘Addresses’ Property of Modbus Master

NOTE:
If ‘CP PtP software’ is not installed Parameter Tab will be disabled.
The OB which used for programming, same should be mentioned in ‘Process
Image’ as shown in above figure.

1.2.4. Step 3: “Basic Parameters” Tab –


Figure 3: ‘Basic Parameters’ Property of Modbus Master

1.2.5. Step 4: “Parameter” Tab –


After clicking on “Parameter” tab one window will open with the heading
“Assigning Parameters to Point To Point Connection”. In this window we have to select
protocol as Modbus Master from the dropdown list as shown in the figure 5.
Figure 4: ‘Assigning Parameters to Point To Point Connection’ Window
Figure 5: Protocol Selection

1.2.6. Step 4.1: “General” Tab –


After opening window shown in figure 4, just double click on picture written as
“Protocol”. One property window named as ‘Protocol’ will pop up as shown in figure
6.
Figure 6: ‘General’ Protocol property of Modbus Master
1.2.7. Step 4.2: “Modbus-Master” Tab –
Keep the settings for Modbus Master same as shown in figure 7.

Figure 7: ‘Modbus-Master’ Protocol property of Modbus Master

1.2.8. Step 4.3: “Interface” Tab –


Keep the Interface settings same as shown in figure 8.

Figure 8: ‘Interface’ Protocol property of Modbus Master


1.2.9. Step 5: Before Load Drivers –
Once all the protocol settings to act CP – 341 as Modbus Master have been
completed, first we have to save that settings & download hardware configuration in
the CPU after compilation.
Again open the window shown in figure 4 by clicking “Parameter” tab in
properties of CP – 341 Module.

1.2.10. Step 6: Load Drivers –


For the loading drivers click on the picture written as ‘Load Drivers’, one window
will open. After that click on the “Load Drivers” tab, it will load driver of Modbus
Master in CP-341.

Figure 9: ‘Load Drivers’ in CP – 341 for Modbus Master


NOTE:
During Load Driver your CPU should be in Manual STOP Mode. In case it is not
then it will prompt that “Put your CPU in STOP mode”

WARNING: Load Drivers only once in CP – 341. It will never change until you load
another driver. If you tried to load same driver again, system will give message that
“Drivers already exist”

Thus we have completed hardware configuration of CP – 341 to act as Modbus


Master.

1.2. Modbus Slave:


Go to properties of CP-341 module & do the settings as shown in figures below.

1.2.1. Step 1: “General” Tab -


Figure 10: ‘General’ Property of Modbus Slave

1.2.2. Step 2: “Addresses” Tab –


Remember this start address of the module because we are using this address in
the programming. It is recommended to have same start address for input & output as
shown in figure below.
Figure 11: ‘Addresses’ Property of Modbus Slave

NOTE:
If ‘CP PtP software’ is not installed Parameter Tab will be disabled.
The OB which used for programming, same should be mentioned in ‘Process
Image’ as shown in above figure.

1.2.3. Step 3: “Basic Parameters” Tab –


Figure 12: ‘Basic Parameters’ Property of Modbus Slave

1.2.4. Step 4: “Parameter” Tab –


After clicking on “Parameter” tab one window will open with the heading
“Assigning Parameters to Point To Point Connection”. In this window we have to select
protocol as Modbus Slave from the dropdown list as shown in the figure 14.
Figure 13: ‘Assigning Parameters to Point To Point Connection’ Window
Figure 14: Protocol Selection

1.2.5. Step 4.1: “General” Tab –


After opening window shown in figure 13, just double click on picture written as
“Protocol”. One property window named as ‘Protocol’ will pop up as shown in figure
15.
Figure 15: ‘General’ Protocol property of Modbus Slave
1.2.6. Step 4.2: “Modbus-Slave” Tab –
Keep the settings for Modbus Slave same as shown in figure 16. Here remember
Slave address which you have to use in programming later.

Figure 16: ‘Modbus-Slave’ Protocol property of Modbus Slave

1.2.7. Step 4.3: “FC 01,05,15” Tab –


FC stands for Function Code. Set the values as shown in figure 17.
Figure 17: ‘FC 01,05,15’ Protocol property of Modbus Slave

1.2.8. Step 4.4: “FC 02” Tab –


Set the values as shown in figure 18.

Figure 18: ‘FC 02’ Protocol property of Modbus Slave


1.2.9. Step 4.5: “FC 03,06,16” Tab –
Set the values as shown in figure 19. Remember DB number (In our case we are
simulating this DB as ‘Holding Register’ of Modbus Data Model)because it will be
used in program later to transfer values to Master when it send query to Slave for
reading data.

Figure 19: ‘FC 03,06,16’ Protocol property of Modbus Slave

1.2.10. Step 4.6: “FC 04” Tab –


Set the values as shown in figure 20. Remember DB number (In our case we are
simulating this DB as ‘Input Register’ of Modbus Data Model)because it will be used
in program later to transfer values to Master when it send query to Slave for reading
data.
Figure 20: ‘FC 04’ Protocol property of Modbus Slave

1.2.11. Step 4.7: “Limits” Tab –


Set the values as shown in figure 21. Here MIN & MAX DB number is same as
DB number used for Function Code FC 03.

Figure 21: ‘Limits’ Protocol property of Modbus Slave


Here we haven’t mentioned any MIN & MAX limits for Memory bits & Outputs (i.e.
we have kept it as zero), so it will give you messages whenever you move to next tab as
shown in figure 22 & 23. Just ignore that message.

Figure 22: Message for Memory Bits Limits

Figure 23: Message for Outputs Limits


1.2.12. Step 4.8: “Interface” Tab –
Keep the Interface settings same as shown in figure 24.

Figure 24: ‘Interface’ Protocol property of Modbus Slave

1.2.13. Step 5: Before Load Drivers –


Once all the protocol settings to act CP – 341 as Modbus Slave have been
completed, first we have to save that settings & download hardware configuration in
the CPU after compilation.
Again open the window shown in figure 13 by clicking “Parameter” tab in
properties of CP – 341 Module.

1.2.14. Step 6: Load Drivers –


For the loading drivers click on the picture written as ‘Load Drivers’, one window
will open. After that click on the “Load Drivers” tab, it will load driver of Modbus
Slave in CP-341.
Figure 25: ‘Load Drivers’ in CP – 341 for Modbus Slave

NOTE:
During Load Driver your CPU should be in Manual STOP Mode. In case it is not
then it will prompt that “Put your CPU in STOP mode”

WARNING: Load Drivers only once in CP – 341. It will never change until you load
another driver. If you tried to load same driver again, system will give message that
“Drivers already exist”

Thus we have completed hardware configuration of CP – 341 to act as Modbus


Slave.

2. Progrmming:
2.1. Query from Modbus Master:
Modbus Master has to send query to read data from Modbus Slave. The query
format is fixed for the Modbus (RTU) communication i.e.

Slave Address Function Code Start address Length

Slave Address [Byte]: This is the address of the Modbus Slave. You are setting this
address in (Figure 16: ‘Modbus-Slave’ Protocol property of Modbus Slave).
Function Code [Byte]: To read data from Modbus Slave you have to give function
Code as B#16#03 & to write data in Modbus Slave you have to give function Code as
B#16#10.
Start Address [Word]: In most cases it is zero. In our case we are considering starting
address of DB.
Length [Int]: Here we have to give number of byte which Modbus Master going to read
or write. In figure below we are reading 10 bytes from Modbus Slave.

Here we have used DB 35 to send Modbus Master Query. Refer figure below.

Figure 26: Query from Modbus Master

2.2. STL program for Modbus Master:


Network 1: Here we are using Memory bit M0.0 to generate pulse using timer T1 & T2.
Refer figure 27 for logic.
Network 2: Here Call FB 8 (i.e. Send data Block) to send query to Modbus Slave
Module. Here allocate all the IO’s as per your requirement, also you can refer figure 27.
Some of the important IO’s – REQ (Assign Memory bit as per used in Network 1) ,
LADDR (This address is same as given in hardware configuration for Modbus Master)
, DB_NO (This DB is the one which contains query of Modbus Master) , LEN (Its
depend upon query – For read operation length is 6 Bytes & For write operation it is not
fixed, its vary as per the number of data bytes to be send)
Network 3: Here Call FB 7 (i.e. Receive data Block) to receive data bytes from Modbus
Slave Module. Here allocate all the IO’s as per your requirement, also you can refer
figure 27. Some of the important IO’s – LADDR (This address is same as given in
Network 2), DB_NO (It can be anything, in our case it’s DB 36. Also refer figure 28).
Figure 27: STL program for Modbus Master
Figure 28: Modbus Master DB to store data from slave

2.3. STL program for Modbus Slave:


Network 1: This logic is used to permanently set M180.0 & reset M180.1. Refer figure
29.
Network 2: Here Call FB 80 (i.e. Modbus 341 Block) for timeout & FB initialization of
Modbus Slave Module. Here allocate all the IO’s as per your requirement, also you can
refer figure 29. Some of the important IO’s – LADDR (This address is same as given in
hardware configuration for Modbus Slave) , START_TIMER & START_TIME (It’s
for timeout initialization) , CP_START (Its for FB initialization & always you have to
set it) , CP_START_FM (Its for FB initialization at the positive edge of CP_START
& always you have to reset it)

NOTE:
Slave DB (e.g. DB 20 in our case) is used for internal data storage of Modbus Slave, so
the bytes which we have to receive in Modbus Master from Modbus Slave should be
mentioned in Slave DB. Also refer figure 19 & 30 for the same.
Figure 29: STL program for Modbus Slave

Figure 30: Modbus Slave DB for internal data storage

Thus all the logic part is completed for Modbus Master & Slave. After this you can
continue with your communication part.
3. Modbus Simulators:
There are basically two simulators we are using during testing of Modbus (RTU)
communication when single Modbus module is available. These two are ModSim32 &
ModScan32.
3.1. ModSim32:
It is used to act as a Modbus Slave module, when you have Modbus Master module
in panel. Now to configure Modbus slave in simulator, below procedure need to be
followed.
First start the ModSim32 simulator.
3.1.1. Step 1: Connection to PC COM Port –
Here first select the COM port for the Modbus communication. Refer figure 31
for the same.

Figure 31: Connection to PC COM Port in ModSim32

3.1.2. Step 2: Setup COM Port 1 –


After connecting to COM1, one window named as “Setup Comm Port 1” will
be open. Refer figure 32.
Here, Protocol must be selected as RTU & other settings are same as Modbus
Master (e.g. refer figure 7).
Figure 32: Setup COM Port 1 for ModSim32

3.1.3. Step 3: Setting in ModSim Window to communicate with Master –


Just open the new ModSim window & configure it as Modbus Slave. For this
you have to make following settings. Also refer figure 33.
Device ID: It should be same as ‘Slave_Address’ mentioned in Modbus Master
Query (e.g. referring figure 26, device ID must be 2).
Address: It is usually 0001.
Length: It should be same as mentioned in Modbus Master Query (e.g. referring
figure 26, Length must be 10).
Modbus Point Type: Here select according to the requirement. Generally it is “03:
Holding Register”.

After these settings, the communication with Modbus Master gets established
successfully.
Figure 33: Setting in ModSim1 window to communicate with Modbus Master

3.1.4. Step 4: Disconnect from PC COM Port –


To disconnect the ModSim32 simulator refer figure 34.

Figure 34: Disconnecting ModSim32 from COM Port

NOTE:
During Modbus (RTU) communication ModSim32 should be open during whole
communication.

3.2. ModScan32:
It is used to act as a Modbus Master module, when you have Modbus Slave module
in panel. Now to configure Modbus Master in simulator, below procedure need to be
followed.
First start the ModScan32 simulator.
3.2.1. Step 1: Connection to PC COM Port –
To establish connection with PC COM Port, first connect the ModScan32. Refer
figure 35.

Figure 35: Connection to PC COM Port in ModScan32

3.2.2. Step 2: Connection Details –


After clicking on “connect”, one window named as “Connection Details” will
pop up. Refer figure 36.
Here, in “Connect Using” select COM Port (usually it is COM1) & in
“Configuration” set all the parameter same as Modbus Slave (e.g. refer figure 16).
Figure 36: Connection Details of ModScan32

3.2.3. Step 3: Modbus Protocol Selection –


To further select protocol for communication click on “Protocol Selections” Tab
shown in figure 36.
“Modbus Protocol Selections” window will pop up. Now in “Transmission
Mode”, select Standard - RTU Protocol. Refer figure 37.

Figure 37: Modbus Protocol Selections for ModScan32

3.2.4. Step 4: Setting in ModScan Window to communicate with Slave –


Just open the new ModScan window & configure it as Modbus Master. For this
you have to make following settings. Also refer figure 38.
Device ID: : It should be same as ‘Slave Address’ mentioned in Modbus Slave
Parameter Property (e.g. referring figure 16, device ID must be 2).
Address: It is usually 0001.
Length: It will be depending on how many bytes has to be read from Modbus Slave.
Modbus Point Type: Here select according to the requirement. Generally it is “03:
Holding Register”.

After these settings, the communication with Modbus Slave gets established
successfully.
Figure 38: Setting in ModSca1 window to communicate with Modbus Slave

3.2.5. Step 5: Disconnect from PC COM Port –


To disconnect the ModScan32 simulator refer figure 39.

Figure 39: Disconnecting ModScan32 from COM Port

NOTE:
During Modbus (RTU) communication ModScan32 should be open during whole
communication.
4. Troubleshooting:
5. XYZ:

You might also like