Support Portal

Modbus RTU - based on RS485 How to read/ write registers

1) Topology and termination 2) Modbus RTU protocol 3) Function Code 4) Examples
Related Products
OLS20-BB1118142 OPTICAL LINE GUIDANCE
OLS20-UB1118340 OPTICAL LINE GUIDANCE
MLSE-0200C2NUA MAGN.LANE SENSOR
OLS20-BB1118342 OPTICAL LINE GUIDANCE
CSS-WBFA54118RZZZZZZZZZZZZ7ZZ1
GLS100G-1MBB1GZ
CSS-WBF114118RZZZZZZZZZZZZ7ZZ1
CSS-WBG4C4118RZZZZZZZZZZZZTZZ1
CSS-WBGAD4118RZZZZZZZZZZZZTZZ1
MLSE-0400C2NUA MAGN.LANE SENSOR
OLS20-UB1118140 OPTICAL LINE GUIDANCE

Table of Contents

Find here some information about sensors which have a Modbus RTU interface.

 

1) Topology and termination

1a) point-to-point connection

In that case the sensor can have an integrated termination and the sensor address doesn't matter

1b) bus operation in line topology

Each sensor must be configured with a different sensor address

only for sensors without integrated termination resistor

 

1c) Example for a point-to-point connection

For a reliable communication we recommend making a ground connection

 

2) Modbus RTU protocol

Modbus RTU Frame
field 1 field 2 field 3 field 4
Sensor Address Function Code Data CRC

 

field 1 Sensor Address 1 - 247
field 2 determines if Read or Write parameter single or multiple register
field 3 data content of read parameter resp. content of Write parameter
field 4 CRC validation check

 

3) Function Code (field 2)

the function code must match to the requested register address, otherwise an exception Response will happen (see yellow part on the right side)

Function Code Register Type comment Exception Response Exception Code
3 Read Holding Registers r/w Register 130 (0x82) 01 = Illegal Function
02 = Illegal Data Address
03 = Illegal Data Value
4 Read Input Registers ro Register 131 (0x83)
6 Write Single Holding Register write one parameter - only 1 Register 134 (0x86)
16 Write Multiple Holding Registers

write a parameter >1 Register

or write a block of parameter

144 (0x90)

 

4) Examples

4a) find here one example how to read process data with our Color Sensor CSS

for reading process data we must use the function code 4, which is for read only input registers.

In our example the measurement value has 6 Byte, to read all needed values we need 3 register:
start from register address 199 until 201

each register has 16 Bit

the address directory can be found in the operating instruction of the particular sensor.

  199 - 201 = Read 6 Byte measurement value
  Field 1 Field 2 Field 3 Field 4
  Sensor Address Function Code Start Address Number of Register
Register = 16Bit
CRC
Request 10 4
Read ro Register
199 3  
client -> server 0x0A 0x04 0x00C7 0x0003 0x008D
Response repeat Address repeat Code Number of Byte Data CRC
server -> client 0x0A 0x04 0x06 0x0325FED9FC8C 0xFEE1
Result in decimal L = luminance 8,05  
a = +red / -green -2,95
b = +yellow / -blue -8,84

 

4b) how to read and change a configuration parameter

for reading parameter we must use code 3, which is for read/write register

for changing the parameter we have 2 options:

  1. code 6, which is for writing one single register
  2. code 16, which is for writing multiple registers (or for writing a block of serial parameters)

here You see one simple example for the color sensor CSS, how to read the status of sender configuration with code 3 and then how to change the configuration with code 6

  148 - Read Sender Configuration
  Field 1 Field 2 Field 3 Field 4
  Sensor Address Function Code Start Address Number of Register
Register = 16Bit
CRC
Request 10 3
Read r/w Register
148 1  
client -> server 0x0A 0x03 0x0094 0x0001 0xC49D
Response repeat Address repeat Code Number of Byte  Data CRC
server -> client 0x0A 0x03 0x02 0x0000 0x1D85
Result   Sender ON  
  148 - Write Sender OFF
  Field 1 Field 2 Field 3 Field 4
  Sensor Address Function Code Start Address Parameter Value CRC
Request 10 6
Write Single Register
148 1  
client -> server 0x0A 0x06 0x0094 0x0001 0x089D
Response repeat Address repeat Code repeat Start Address  repeat Parameter CRC
server -> client 0x0A 0x06 0x0094 0x0001 0x089D
Result   Sender OFF  

 

 

Keywords:
Modbus, RS485, Register, Modbus RTU