2. 8.1 INTRODUCTION
• MODBUS is an application-layer messaging protocol, positioned at level 7 of the Open
Systems Interconnection (OSI) model. It provides client/server communication between
devices connected on different types of buses or networks.
• The de facto industrial serial standard since 1979, MODBUS continues to enable
millions of automation devices to communicate. Today, support for the simple and
elegant structure of MODBUS continues to grow. The Internet community can access
MODBUS at a reserved system port 502 on the TCP/IP stack.
• MODBUS is a request/reply protocol and offers services specified by function codes.
MODBUS function codes are elements of MODBUS request/reply PDUs. This protocol
specification document describes the function codes used within the framework of
MODBUS transactions.
4. 8.1 MODBUS APPLICATION PROTOCOL(MBAP) SPECIFICATION
• MODBUS is an application layer messaging protocol for client/server communication
between devices connected on different types of buses or networks. It is currently
implemented using:
• TCP/IP over Ethernet. See MODBUS Messaging Implementation Guide V1.0a.
• Asynchronous serial transmission over a variety of media (wire : EIA/TIA-232-E,
EIA-422, EIA/TIA-485-A; fiber, radio, etc.)
• MODBUS PLUS, a high speed token passing network.
5. 8.1 MODBUS APPLICATION PROTOCOL SPECIFICATION
• The MODBUS protocol allows an easy communication within all types of network
architectures.
• Every type of devices (PLC, HMI, Control Panel, Driver, Motion control, I/O Device…)
can use MODBUS protocol to initiate a remote operation. The same communication can
be done as well on serial line as on an Ethernet TCP/IP networks. Gateways allow a
communication between several types of buses or network using the MODBUS protocol.
6. 8.1 MODBUS APPLICATION PROTOCOL SPECIFICATION
• The MODBUS protocol defines a simple protocol data unit (PDU) independent of the
underlying communication layers. The mapping of MODBUS protocol on specific buses
or network can introduce some additional fields on the application data unit (ADU).
■ PROTOCOL DESCRIPTION
8. 8.1 MODBUS APPLICATION PROTOCOL SPECIFICATION
• The size of the MODBUS PDU is limited by the size constraint inherited from the first
MODBUS implementation on Serial Line network (max. RS485 ADU = 256 bytes).
• Therefore: MODBUS PDU for serial line communication = 256 - Server address (1 byte)
- CRC (2 bytes) = 253 bytes.
• Consequently: RS232 / RS485 ADU = 253 bytes + Server address (1 byte) + CRC (2
bytes) = 256 bytes. TCP MODBUS ADU = 253 bytes + MBAP (7 bytes) = 260 bytes.
• The MODBUS protocol defines three PDUs. They are :
MODBUS Request PDU, mb_req_pdu
MODBUS Response PDU, mb_rsp_pdu
MODBUS Exception Response PDU, mb_excep_rsp_pdu
■ PROTOCOL DESCRIPTION
9. 8.1 MODBUS APPLICATION PROTOCOL SPECIFICATION
• MODBUS uses a ‘big-Endian’ representation for addresses and data items. This means
that when a numerical quantity larger than a single byte is transmitted, the most
significant byte is sent first.
• For example:
Register size Value
16 - bits 0x1234 the first byte sent is 0x12 then 0x34
■ DATA ENCODING
10. 8.1 MODBUS APPLICATION PROTOCOL SPECIFICATION
• MODBUS bases its data model on a series of tables that have distinguishing
characteristics. The four primary tables are:
■ MODBUS DATA MODEL
11. 8.1 MODBUS APPLICATION PROTOCOL SPECIFICATION
• The MODBUS application protocol defines precisely PDU addressing rules.
• In a MODBUS PDU each data is addressed from 0 to 65535. In the MODBUS data
Model each element within a data block is numbered from 1 to n.
• The pre-mapping between the MODBUS data model and the device application is totally
vendor device specific.
■ MODBUS ADDRESSING MODEL
13. 8.1 MODBUS APPLICATION PROTOCOL SPECIFICATION
■ FUNCTION CODES
There are three categories of MODBUS Functions codes. They are : Public Function Codes,
User-Defined Function Codes, Reserved Function Codes
15. 8.2 MODBUS MESSAGING ON TCP/IP IMPLEMENTATION GUIDE V1.0b
❖ The MODBUS messaging service provides a Client/Server communication between
devices connected on an Ethernet TCP/IP network. This client / server model is based on
four type of messages:
• MODBUS Request,
• MODBUS Confirmation,
• MODBUS Indication,
• MODBUS Response
■ CLIENT / SERVER MODEL
16. 8.2 MODBUS MESSAGING ON TCP/IP IMPLEMENTATION GUIDE V1.0b
❖ A communicating system over MODBUS TCP/IP may include different types of device:
• A MODBUS TCP/IP Client and Server devices connected to a TCP/IP network
• The Interconnection devices like bridge, router or gateway for interconnection
between the TCP/IP network and a serial line sub-network which permit connections
of MODBUS Serial line Client and Server end devices.
■ PROTOCOL DESCRIPTION
MODBUS TCP/IP communication architecture
17. 8.2 MODBUS MESSAGING ON TCP/IP IMPLEMENTATION GUIDE V1.0b
❖ MODBUS On TCP/IPApplication Data Unit
• A dedicated header is used on TCP/IP to identify the MODBUS Application Data Unit. It
is called the MBAP header (MODBUS Application Protocol header).
■ PROTOCOL DESCRIPTION
MODBUS request/response over TCP/IP
19. 8.2 MODBUS MESSAGING ON TCP/IP IMPLEMENTATION GUIDE V1.0b
■ MODBUS COMPONENT ARCHITECTURE MODEL
20. 8.2 MODBUS MESSAGING ON TCP/IP IMPLEMENTATION GUIDE V1.0b
■ COMMUNICATION APPLICATION LAYER
❖ MODBUS CLIENT
MODBUS Client unit
21. 8.2 MODBUS MESSAGING ON TCP/IP IMPLEMENTATION GUIDE V1.0b
■ COMMUNICATION APPLICATION LAYER
❖ MODBUS CLIENT DESIGN
A MODBUS client can receive three events:
• A new demand from the user application to send a request, in this case a MODBUS
request has to be encoded and be sent on the network using the TCP management
component service. The lower layer ( TCP management module) can give back an error
due to a TCP connection error, or some other errors.
• A response from the TCP management, in this case the client has to analyze the content
of the response and send a confirmation to the user application
• The expiration of a Time out due to a non-response. A new retry can be sent on the
network or a negative confirmation can be sent to the User Application. Remark :
These retries are initiated by the MODBUS client, some other retries can also be done
by the TCP layer in case of TCP acknowledge lack.
23. 8.2 MODBUS MESSAGING ON TCP/IP IMPLEMENTATION GUIDE V1.0b
■ COMMUNICATION APPLICATION LAYER
❖ EXAMPLE: BUILD A MODBUS REQUEST
MODBUS request ADU encoding for reading the
register # 5 in a remote server :
MODBUS Request ADU encoding :
24. 8.2 MODBUS MESSAGING ON TCP/IP IMPLEMENTATION GUIDE V1.0b
■ COMMUNICATION APPLICATION LAYER
❖ MODBUS SERVER
MODBUS Server unit
25. 8.2 MODBUS MESSAGING ON TCP/IP IMPLEMENTATION GUIDE V1.0b
■ COMMUNICATION APPLICATION LAYER
❖ MODBUS SERVER DESIGN
The MODBUS Server design depends on both :
• The kind of access to the application objects (simple access to attributes or advanced
access to services)
• The kind of interaction between the MODBUS server and the user application
(synchronous or asynchronous).
26. 8.2 MODBUS MESSAGING ON TCP/IP IMPLEMENTATION GUIDE V1.0b
■ COMMUNICATION APPLICATION LAYER
❖ MODBUS SERVER DESIGN
Process MODBUS Indication activity diagram
27. 8.3 MODBUS OVER SERIAL LINE
■ MODBUS MASTER/SLAVE PROTOCOL PRINCIPLE
• The MODBUS Serial Line protocol is a Master-Slaves protocol.
• Only one master (at the same time) is connected to the bus, and one or several (247
maximum number) slaves nodes are also connected to the same serial bus.
• A MODBUS communication is always initiated by the master.
• The slave nodes will never transmit data without receiving a request from the master
node.
• The slave nodes will never communicate with each other.
• The master node initiates only one MODBUS transaction at the same time.
28. 8.3 MODBUS OVER SERIAL LINE
■ MODBUS MASTER/SLAVE PROTOCOL PRINCIPLE
The master node issues a MODBUS request to the slave nodes in two modes:
• In broadcast mode, the master can send a
request to all slaves. No response is
returned to broadcast requests sent by
the master. The broadcast requests are
necessarily writing commands. All
devices must accept the broadcast for
writing function.
• In unicast mode, the master addresses an
individual slave. After receiving and
processing the request, the slave returns
a message (a 'reply') to the master . In
that mode, a MODBUS transaction
consists of 2 messages : a request from
the master, and a reply from the slave.
29. 8.3 MODBUS OVER SERIAL LINE
■ MODBUS ADDRESSING RULES
• The MODBUS addressing space comprises 256 different addresses.
• The Address 0 is reserved as the broadcast address. All slave nodes must recognise
the broadcast address.
• The MODBUS Master node has no specific address, only the slave nodes must have
an address. This address must be unique on a MODBUS serial bus.
30. 8.3 MODBUS OVER SERIAL LINE
■ MODBUS FRAME DESCRIPTION
• On MODBUS Serial Line, the Address field only contains the slave address.
• The function code indicates to the server what kind of action to perform. The function
code can be followed by a data field that contains request and response parameters.
• Error checking field is the result of a "Redundancy Checking" calculation that is
performed on the message contents. Two kinds of calculation methods are used
depending on the transmission mode that is being used (RTU or ASCII).
31. 8.3 MODBUS OVER SERIAL LINE
■ MASTER/SLAVE STATE DIAGRAMS
The MODBUS data link layer comprises two separate sub layers :
• The Master / slave protocol
• The transmission mode ( RTU vs ASCII modes)
32. 8.3 MODBUS OVER SERIAL LINE
■ MASTER/SLAVE STATE DIAGRAMS
❖ MASTER STATE DIAGRAM
33. 8.3 MODBUS OVER SERIAL LINE
■ MASTER/SLAVE STATE DIAGRAMS
❖ SLAVE STATE DIAGRAM
34. 8.3 MODBUS OVER SERIAL LINE
■ MASTER/SLAVE COMMUNICATION TIME DIAGRAM
35. 8.3 MODBUS OVER SERIAL LINE-RTU TRANSMISSION MODE
❖ RTU TRANSMISSION MODE
The format ( 11 bits ) for each byte in RTU(Remote Terminal Unit) mode is :
Coding System: 8–bit binary
Bits per Byte: 1 start bit
8 data bits, least significant bit sent first
1 bit for parity completion
1 stop bit
36. 8.3 MODBUS OVER SERIAL LINE-RTU TRANSMISSION MODE
Frame description:
Cyclical
Redundancy
Checking
❖ MODBUS MESSAGE RTU FRAMING
37. 8.3 MODBUS OVER SERIAL LINE-RTU TRANSMISSION MODE
❖ MODBUS MESSAGE RTU FRAMING
In RTU mode, message frames are separated by a silent interval of at least 3.5
character times.
38. 8.3 MODBUS OVER SERIAL LINE-RTU TRANSMISSION MODE
❖ MODBUS MESSAGE RTU FRAMING
RTU transmission mode state diagram
39. 8.3 MODBUS OVER SERIAL LINE-ASCII TRANSMISSION MODE
❖ ASCII TRANSMISSION MODE
The format ( 10 bits ) for each byte in ASCII mode is :
Coding System: Hexadecimal, ASCII characters 0–9, A–F
One hexadecimal character contains 4-bits of data within each ASCII
character of the message
Bits per Byte: 1 start bit
7 data bits, least significant bit sent first
1 bit for parity completion;
1 stop bit
40. 8.3 MODBUS OVER SERIAL LINE-RTU TRANSMISSION MODE
Frame description:
Longitudinal
Redundancy
Checking
❖ MODBUS MESSAGE ASCII FRAMING
41. 8.3 MODBUS OVER SERIAL LINE-RTU TRANSMISSION MODE
❖ MODBUS MESSAGE ASCII FRAMING
ASCII transmission mode state diagram
42. EXERCISE-HK1-18-19
Viết chương trình PLC S7-1200 và thực hiện giao tiếp máy tính qua cổng TCP/IP
điều khiển và giám sát hoạt động của đèn giao thông, chuẩn truyền Modbus TCP.
a. (L.O.1) (1đ)
- Nêu phần mềm lập trình PLC và giao diện máy tính để thực hiện bài toán.
- Lập bảng mapping thông số và tín hiệu điều khiển giữa PLC và giao diện máy tính.
b. (L.O.2) (1.5đ) Viết chương trình PLC thực hiện liên kết thông số từ PC và thực hiện
điều khiển đèn giao thông.
c. (L.O.4) (1.5đ) Thiết kế giao diện và viết chương trình giao diện cho PC.
- Xác định và nêu hoạt động cơ bản các hàm Modbus liên kết dữ liệu giữa PC PLC
- Viết chương trình cấu hình và kết nối giữa PLC và PC
- Giả sử các hàm Modbus có sẵn, viết chương trình cho nút nhấn “Update_Pars” cho
phép cập nhật nội dung đèn XA, VA, XB, VB xuống PLC; và chương trình cho Timer cập
nhật trạng thái PLC.
43. EXERCISE-HK2-18-19
Lập trình PLC và giao tiếp máy tính qua cổng TCP/IP điều khiển và giám sát hệ
thống lò nhiệt qua chuẩn Modbus TCP theo yêu cầu Hình 1.
a. (L.O.1) (1đ)
- Nêu các hãng, các dòng PLC và phần mềm lập trình PLC tương ứng mà bạn biết.
- Nêu các phần mềm cho phép lập trình kết nối và thực hiện giao diện trên máy tính.
- Xác định bảng mapping thông số Modbus giữa PLC và máy tính từ yêu cầu đề bài,
trong đó Reference là nhiệt độ đặt; Response là nhiệt độ hiện tại của lò; Sigma là độ trễ
của bộ điều khiển ON/OFF.
b. (L.O.2) (2đ) Viết chương trình PLC thực hiện liên kết thông số từ PC. Thực hiện bộ
điều khiển ON/OFF cho hệ lò trên PLC với chu kỳ lấy mẫu 0.5s. Giả sử các ngõ vào AI0
và AI1 đã được chuẩn hóa về tầm áp 0-10V với tầm nhiệt độ 0-200OC.
c. (L.O.4) (2đ) Viết chương trình giao diện cho PC.
- Xác định và nêu hoạt động cơ bản các hàm Modbus liên kết dữ liệu giữa PC và PLC.
- Viết chương trình cấu hình và kết nối giữa PLC và PC
- Giả sử các hàm Modbus có sẵn, viết chương trình điều khiển, giám sát nhiệt độ và cập
nhật thông số xuống PLC.
45. EXERCISE-HK3-18-19
Hình 1: Điều khiển và giám sát hệ lò nhiệt
Viết chương trình PLC S7-1200, Giám sát thông số bằng HMI.
a. (L.O.2) (3đ) Viết chương trình PLC điều khiển ON/OFF cho hệ lò trên
PLC S71200 với chu kỳ lấy mẫu 0.1s. Giả sử các ngõ vào AI0 và AI1 đã
được chuẩn hóa về tầm áp 0-10V với tầm nhiệt độ 0-200OC.
b. (L.O.4) (2đ) Thực hiện giao diện HMI, mô tả kết nối dữ liệu giữa HMI và
PLC