Using Modbus Library With Step7 Siemens Plcs
Using Modbus Library With Step7 Siemens Plcs
SIEMENS PLCs
Under Supervision of
Dr. Abdelrahman Ali Karar
By
Mohammed Mutwakil Ahmed Abushama
To
Department of Electrical and Electronic Engineering
Faculty of Engineering and Architecture
University of Khartoum
July 2009
TABLE OF CONTENTS
Dedication ............................................................................................................. I
Acknowledgement ...............................................................................................II
Abstract (English version) ................................................................................. III
Abstract (Arabic Version) ................................................................................. IV
Table of Contents....V
List of Figures ................................................................................................. VIII
List of Tables ...................................................................................................... X
Abbreviations ..................................................................................................... XI
Chapter 1: Introduction
1.1 Programmable logic controller (PLC) .............................................. 1
1.2 Fieldbus protocols ........................................................................... 1
1.3 Project Objectives ............................................................................ 2
1.4 Thesis Layout .................................................................................... 2
Chapter 2: Theory
2.1 Siemens SIMATIC S7-200 PLC ...................................................... 3
2.1.1 S7-200 CPU ............................................................................ 3
2.1.2 STEP 7-Micro/WIN Programming Package ......................... 3
2.1.3 S7-200 Operation & Execution .............................................. 5
2.1.4 Operation Mode ...................................................................... 7
2.2 Modbus Protocol .............................................................................. 7
2.2.1 Protocol description ............................................................... 8
2.2.2 Modbus transaction .............................................................. 10
2.2.3 Data Encoding ...................................................................... 11
2.2.4 Data Model ........................................................................... 11
2.2.4.1 Four Main Function Codes ....................................... 12
2.2.5 Byte Format in RTU mode ................................................... 12
2.2.6 Modbus Message RTU Framing .......................................... 13
2.3 S7-200 Communications ............................................................ 14
2.3.1 Using the Modbus protocol library in Micro/WIN ............. 14
2.3.1.1 Modbus Addressing .................................................. 15
V
2.3.1.2 MBUS-INIT Instruction............................................ 15
2.3.1.3 MBUS-SLAVE Instruction....................................... 17
2.4 Serial data communication ............................................................ 17
2.4.1 Interface standards ............................................................... 18
2.4.1.1 RS-232 standard ........................ ..18
2.4.1.2 RS-485 standard ....................................................... 20
2.4.1.3 Serial interface converter ......................................... 21
2.4.2 Data Acquisition ................................................................... 21
VI
References ......................................................................................................... 37
Appendix A: Devices figures .............................................................................
Appendix B : Micro/Win ladder diagram ........................................................
Appendix B: VB program code...............
VII
[1] S7-200 Programmable Controller Systems Manual [book] / auth. AG
Siemens.-Nuernberg : Siemens Aktiengesellschaft,2004.
37
LIST OF FIGURES
IX
LIST OF TABLES
X
To my dear parents, who gave me the support during the
I
PLC Programmable Logical Controller.
VB Visual Basic
I/O Input/Output.
DB Data Base
XI
I would like to thank Dr. Abdelrahman Ali Karar who was the
supervisor of my project.
cooperation.
II
The main objective of this project is to give a basic idea about exchanging data between
programmable logical controllers (PLC) and the computer, based on Modbus protocol and serial
data interface.
This project focused gets the features of PLC (flexibility, and reliability), features of
computer (reliability, and processing speed), and the features of Modbus protocol (flexibility,
and low development cost).
In order to achieve our objective, we first simulate the communication between master and
slave devices.
Then a real-time process has been developing by using visual basic program represent the
master, and a programmable logic controller (PLC) represents a slave device.
The serial interfaces used are RS-232 and RS-485.
III
Chapter 1 Introduction
Chapter 1
Introduction
2
Chapter 2 Theory
Chapter 2
Theory
3
Chapter 2 Theory
Form the above figure, the Micro/Win program is based on a three convenient
working spaces for crating the control program:
1) Navigation bar
Presents groups of icons for accessing different programming features of STEP7-
Micro/Win.
2) Instruction tree
Displays all of the project objects and the instructions for creating the program.
3) Program editor
Contains the program logic and a local variable table where you can assign
symbolic names for temporary local variables. Subroutines and interrupt routines appear as tabs at
the bottom of the program editor window.
STEP 7-Micro/WIN provides three editors for creating your program: Ladder
Logic (LAD), Statement List (STL), and Function Block Diagram (FBD). The STL editor displays
the program as a text-based language, were The LAD editor displays the program as a graphical
representation similar to electrical wiring diagrams, and The FBD editor displays the program as a
graphical representation that resembles common logic gate diagrams [1].
4
Chapter 2 Theory
The S7-200 executes a series of tasks repetitively. This cyclical execution of tasks is called
the scan cycle. As shown in Figure 2-4, the S7-200 performs most or all of the following tasks
during a scan cycle:
Reading the inputs: The S7-200 copies the state of the physical inputs to the process-
image input register.
If the inputs are digital inputs, each scan cycle begins by reading the current value of the
digital inputs and then writing these values to the process-image input register. If analog, The S7-
200 does not update analog inputs from expansion modules as part of the normal scan cycle unless
filtering of analog inputs is enabled. An analog filter is provided to have a more stable signal. You
can enable the analog filter for each analog input point. When analog input filtering is enabled for an
analog input, the S7-200 updates that analog input once per scan cycle, performs the filtering
function, and stores the filtered value internally. The filtered value is then supplied each time your
program accesses the analog input. When analog filtering is not enabled, the S7-200 reads the value
of the analog input from expansion modules each time your program accesses the analog input.
5
Chapter 2 Theory
Executing the control logic in the program: The S7-200 executes the instructions of the
program and stores the values in the various memory areas.
During the execution phase of the scan cycle, the S7-200 executes program, starting with
the first instruction and proceeding to the end instruction. The immediate I/O instructions give you
immediate access to inputs and outputs during the execution of either the program or an interrupt
routine.
Processing any communications requests: The S7-200 performs any tasks required for
communications.
During the message-processing phase of the scan cycle, the S7-200 processes any messages
that were received from the communications port or intelligent I/O modules.
Executing the CPU self-test diagnostics: The S7-200 ensures that the firmware, the
program memory, and any expansion modules are working properly.
During this phase of the scan cycle, the S7-200 checks for proper operation of the CPU and
for the status of any expansion modules.
Writing to the outputs: The values stored in the process-image output register are
written to the physical outputs.
At the end of every scan cycle, the S7-200 writes the values stored in the process-image
output register to the digital outputs. (Analog outputs are updated immediately, independently from
the scan cycle.)
The execution of the user program is dependent upon whether the S7-200 is in STOP mode
or in RUN mode. In RUN mode, your program is executed; in STOP mode, your program is
not executed [1].
6
Chapter 2 Theory
7
Chapter 2 Theory
Standard Modbus ports on Modicon controllers use an RS-232compatible serial interface that
defines connector pinouts, cabling, signal levels, transmission baud rates, and parity checking.
Controllers can be networked directly or via modems.
Controllers communicate using a master-slave technique, in which only one device (the
master) can initiate transactions (queries). The other devices (the slaves) respond by supplying the
requested data to the master, or by taking the action requested in the query. Typical master devices
include host processors and programming panels. Typical slaves include programmable controllers.
The master can address individual slaves, or can initiate a broadcast message to all slaves.
Slaves return a message (response) to queries that are addressed to them individually. Responses are
not returned to broadcast queries from the master.
The Modbus protocol has two serial transmission modes:
ASCII transmission mode:
When controllers are setup to communicate on a modbus network using ASCII(American
Standard Code for Information Interchange) mode, each eight-bit byte in a message is sent as two
ASCII characters. The main advantage of this mode is that it allows time intervals of up to one
second to occur between characters without causing an error.
RTU transmission mode
When controllers are setup to communicate on a Modbus network using RTU (Remote
Terminal Unit) mode, each eight-bit byte in a message contains two fourbit hexadecimal
characters. The main advantage of this mode is that its greater character density allows better
data throughput than ASCII for the same baud rate.
8
Chapter 2 Theory
The modbus application data unit is built by the client that initiates a modbus transaction.
The modbus application protocol establishes the format of a request initiated by a client.
From figure 2-5 we can see that there are four main fields:
1) Address Field:
Valid slave device addresses are in the range of (0 ... 247) decimal. The individual slave
devices are assigned addresses in the range of (1 ... 247).Value 0 is reserved for broadcast messages.
Request :
A master addresses a slave by placing the slave address in the address field of the message.
Response :
When the slave sends its response, it places its own address in this address field
of the response to let the master know which slave is responding.
2) Function code Field:
The function code field of a Modbus data unit is coded in one byte. Valid codes are in the
range of (1 ... 255) decimal (128 255 reserved for exception responses). When a message is sent
from a Client to a Server device the function code field tells the server what kind of action to
perform.
Request :
The function code field tells the slave what kind of action to perform.
Response :
For a normal response, the slave simply echoes the original function code. For an exception
response; the slave returns a code that is equivalent to the original function code with its most
significant bit set to logic 1.
3) Data Field:
Valid size is in the range of 0 ... 252 Byte.
Request :
The data field contains additional information which the slave must use to take the action
defined by the function code.
This includes items like register addresses, quantity of items, etc...
Response :
If no error occurs, the data field contains the data requested. If an error occurs, the field
contains an exception code that the master application can use to determine the next action to be
taken.
9
Chapter 2 Theory
In ASCII mode, the error checking field contains two ASCII characters. The error check
characters are the result of a Longitudinal Redundancy Check (LRC) calculation that is
performed on the message contents, exclusive of the beginning colon and terminating CRLF
characters [4].
In RTU mode, the error checking field contains a 16-bit value implemented as two eight-bit
bytes. The error check value is the result of a Cyclical Redundancy Check (CRC) calculation
performed on the message contents [4].
For an exception response, the server returns a code that is equivalent to the original
function code from the request PDU with its most significant bit set to logic 1.See figure 2-7.
10
Chapter 2 Theory
11
Chapter 2 Theory
Modbus bases its data model on a series of tables that have distinguishing characteristics.
The four primary tables are shown in table:
Table 2-1 Modbus Data Model
For each of the primary tables, the protocol allows individual selection of 65536 data items.
Its obvious that all the data handled via Modbus (bits, registers) must be located in device
application memory.
Modus logical reference numbers, which are used in Modbus functions, are unsigned integer
indices starting at zero [3].
12
Chapter 2 Theory
Even parity is required; other modes (odd parity, no parity) may also be used .In order to
ensure a maximum compatibility with other products.
Remark : the use of no parity requires 2 stop bits.
In RTU mode, message frames are separated by a silent interval of at least 3.5 characters as shown
in figure 2-9.
The entire message frame must be transmitted as a continuous stream of characters.
If a silent interval of more than 1.5 character times occurs between two characters, the
message frame is declared incomplete and should be discarded by the receiver.
In the ASCII mode, messages start with a colon (:) character, and end with a carriage return-
line feed (CRLF) pair.
13
Chapter 2 Theory
14
Chapter 2 Theory
15
Chapter 2 Theory
16
Chapter 2 Theory
17
Chapter 2 Theory
18
Chapter 2 Theory
19
Chapter 2 Theory
20
Chapter 2 Theory
As we mentioned before, we used the SIMATIC S7-200 RS232/PPI Multi-Master cable for
communication, shown in figure 2-17. It is an RS232/RS485 converter, allows a direct
communication between computer and the S7-200 plc. The RS232 is connecting to the
computer serial port, and RS485 is to the PLC.
22
Chapter 2 Theory
23
Chapter 3 Design and Implementation
Chapter 3
Design and Implementation
3.1.1 MBUS_INIT:
We configured the MBUS_INIT as shown in figure 3-1
Mode: set to 1,enables modbus protocol through port0.
Add: define the address starting at 2.
Baud: set the baud rate (which is the number of
symbols per second transferred) at 9600.
Parity: set the value 2 which refers to even
parity.
Delay: 0 because its a wired communication.
MaxIQ: set the maximum number of inputs and
outputs.
MaxAI: set the number of word input to 16
which typically for CPU-222XP.
MaxHold: set the number of holding registers Figure 3-1 MBUS-INIT Settings
HoldSt~: starting of holding register.
Done: staring execution.
Error: contains the result of executing the instruction
3.1.2 MBUS_SLAVE:
And the MBUS_SLAVE as shown in figure 3-2:
EN: the instruction is executed on each scan.
Done: respond to the Modbus request.
Error: contains the execution result.
Figure 3-2 MBUS-SLAVE Setting
24
Chapter 3 Design and Implementation
25
Chapter 3 Design and Implementation
26
Chapter 3 Design and Implementation
27
Chapter 3 Design and Implementation
Start
No
Is Button Pressed
yes
Continuously appdating of
reading every 1 second &
desplay in screen
No
Read
A Error exist ?
Coils
yes
Error
message
End
28
Chapter 3 Design and Implementation
As it was mentioned before, the computer supports communication via RS232. It has
fixed settings for this mode of communication:
9600 Baud rate.
Even parity.
1 stop bit.
8 data bits.
So, these settings were applied to our VB program in order to communicate with the
PLC. Then, the connection was opened and finally the output coils - which were mentioned
above were read.
29
Chapater 4 Tests and Results
Chapter 4
Tests and Results
30
Chapater 4 Tests and Results
31
Chapater 4 Tests and Results
(a)
(b)
Figure 4-3 (a) Write Single Coil; (b) Check the response
32
Chapater 4 Tests and Results
33
Chapater 4 Tests and Results
Then, we change the mode to the manual mode, so we can control these motors by
sending ON value (value 1) to the PLC as shown in figure below:
The set of registers above contains the basic measured values of the PLC output coils.
We designed the VB software with an adjustable scan rate so it can detect simultaneously
any changes in the measured values. Also the parameters like
Slave definition (slave ID, starting Address, Register Size), so this software can
communicate with any Modbus salve and reading its coils.
The VB software full code is contained in Appendix.
34
Appendix A
Appendix A
1A
Appendix A
2A
Appendix B
Appendix B
B1
Appendix B
B2
Appendix B
B3
Appendix B
B4
Appendix B
B5
Appendix B
B6
Appendix B
B7
Appendix B
B8
Appendix B
B9
Appendix B
B10
Appendix C
Appendix C
Dim e As Boolean
End Sub
End Sub
C1
Appendix C
C2