Modbus Communication
Modbus Communication
2
Introduction
● Various versions
● Modbus RTU (Serial), Modbus ASCII, Modbus Ethernet TCP/IP, Modbus
Plus (Proprietary of =S=)
3
Layers Used in ISO Model
4
Physical Layer
Required Multi-point and
RS485 2 wire point-to-point
To integrate into an
RS485 4 wire Optional
existing installation
without modification
Transmission speeds:
9600 bps and 19200 bps required with 19.2 Kbps by default
Other speeds: 1200, 2400, 4800, 38400, 56 Kbps, 115 Kbps optional
5
Modbus RS485 4 wire installation
Master
Topology: bus type
T
R 5V
650 ohms
Slave pair
650 ohms
120 ohms 5V 120 ohms
1 nF 1 nF
Master pair
PG
R R
T T
Max. number of devices:
Slave 1 Slave n 32 including master without a repeater
Max. length:
Main segment: 1,000 m at 19,200 bits/s
Drops: 40 m in total (20 m for one tap link)
6
Modbus RS485 2 wire installation
Master
T
R
5V
650 ohms
Balanced pair
650 ohms 120 ohms
120 ohms
1 nF
Common 1 nF
PG
R R
T T
Max. length:
Main segment: 1,000 m at 19,200 bits/s
Topology: bus type Drops: 40 m in total (20 m for one tap link)
7
Data Transmission
● Transmission method:
● Client / Server
8
Unicast Addressing Mode
Address = 1 to 247
Request
Funct.
5 Code
Request data CRC
Response
Funct.
5 Code
Response data CRC
9
Broadcast Addressing Mode
Address = 0
Request
Funct.
0 Code
Request data CRC
10
Transmission Modes
11
Modbus Serial Frame
Modbus PDU
PDU = Protocol Data Unit
Function
Address Data CRC or LRC
Code
12
Address Field
● 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.
13
Function Field
● 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 a logic 1.
14
Data Field
● Request
● The data field contains additional information which the slave must use to
take the action defined by the function code. This can include items like
register addresses, quantity of items to be handled, 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 used to determine the next action to be taken.
15
Checksum Field
Address Function Data Checksum
● Request
● The checksum is calculated by the master and sends to the slave.
● Response
● The checksum is re-calculated by the slave and compared to the value sent
by the master. If a difference is detected, the slave will not construct a
response to the master.
16
Function Codes
Code Function
http://www.modbus.org
17
Example of Read Request
Request
1 byte 1 byte 2 bytes 2 bytes 2 bytes
Slave Function First word Number of
CRC16
Address code = 3 address words to read
Response
1 byte 1 byte 2 bytes 2 bytes 2 bytes 2 bytes
Slave Function Number of Value of the Value of the
last word CRC16
Address code = 3 bytes read first word
18
Registers & Index
● Registers
● Memory variable located in a device
● Word, Bit, Byte, etc…
● Can be reached thanks to its Index
19
Programming Modbus with Unity Pro
20
1 Platform, 3 Target
● 3 PLC Targets
● M340, Premium, Quantum
● Same Philosophy
21
BMXNOM0200
22
BMXNOM0200 Configuration
23
Request Programming
● Dedicated Function Blocks for M340
● ADDM
●Address Conversion
● READ_VAR
●Execute a read request
● WRITE_VAR
●Execute a write request
● DATA_EXCH
●Execute a user defined request
● OUT_IN_MBUS
●Change from Slave to Master mode
24
ADDM FB
● IN:
● String variable: ‘r.m.c.e.MBS’
● Out
● Converted table of words, to be used in the others FB
● ‘r.m.c.e.MBS’
● r = the rack number where the Modbus module is located,
● m = the slot number of the Modbus module,
● c = the channel used
● e = the equipment address (slave address) of the target
● “MBS” = used to indicates to use Modbus Serial (optional)
25
READ_VAR FB
● IN:
● ADR: to be linked to the output of the ADDM block.
● OBJ: defining object to read (in the case of Modbus register: ‘%MW’)
● NUM: starting register to read
● NB: number of consecutive register to read
● OUT
● RECP: reception zone of the block, delivering the value read (table of
words)
● IN/OUT
● GEST: table of 4 words to manage the communication block (errors,
timeout, length, etc..)
26
WRITE_VAR FB
● IN:
● ADR: to be linked to the output of the ADDM block.
● OBJ: defining object to read (in the case of Modbus register: ‘%MW’)
● NUM: starting register to read
● EMIS: source table to write from the PLC
● IN/OUT
● GEST: table of 4 words to manage the communication block (errors,
timeout, length, etc..)
27
GEST Variable
● Structured Variable
● Table of 4 words
GEST[3] Timeout
GEST[4] Length
28
Exercise
29
Optimizing Communication
30
Optimizing Communication (Example)
31
Optimizing Communication (Cont.)
● Device Timeout
● Mean to know if the Master is lost
● After a defined time without receiving request, Slave goes to fallback mode
Allo, anybody
still there?
● Fallback Mode
● Mode activated when communication is lost
● Behaviour to define (start a motor, stop a process, switch off outputs..)
No
answers…
great, let’s go
party!
32
Exercise
33
Modbus Serial RTU
34