R01an5380ej0102 rl78 Modbus
R01an5380ej0102 rl78 Modbus
R01an5380ej0102 rl78 Modbus
RL78/G14
Modbus ASCII/RTU
Introduction
Modbus is a communication protocol developed by Modicon Inc. (AEG Schneider Automation
International SAS) for PLCs. It is used for the purpose of data transfer, not only for PLCs but also between
electronic devices. It is widely used in the field of factory automation and plant automation because the
specifications are open to the public, free to use, and relatively easy to implement. This makes it the most
common serial communication protocol for connecting industrial equipment.
For example, in a factory, Modbus is used to connect devices through various wired or wireless
communication gateways. The master device used by Modbus can be a gateway, HMI, SCADA (Supervisor
Control and Data Acquisition), PLC, or similar device. The slave devices can include I/O control devices,
temperature and humidity measuring machines, meters, frequency transformers, motor units, etc.
There are two Modbus transmission modes, both based on serial communication: Modbus ASCII
(American Standard Code for Information Interchange) and Modbus RTU (Remote Terminal Unit).
Modbus ASCII transmits data as ASCII strings by converting each byte of data into a two-character
ASCII code. Termination characters are appended to delimit the data, which increases the amount of data
and increases the transmission time compared to Modbus RTU. On the other hand, the transmitted data is
easier to parse. Modbus RTU, in contrast, transmits the data in binary format without conversion. To delimit
the data in Modbus RTU, a non-communication interval of at least 3.5 characters is required. The amount of
data and the transmission time are reduced compared to Modbus ASCII, but parsing the transmitted data
requires using a timer.
Modbus communication uses a single-master/multi-slave method in which the slave responds to
requests from the master. There is 1 master and 1 to 247 slaves in a Modbus network. Each slave must
have a unique address (1 to 247) in the network. Since it is not possible to communicate with different data
formats on the same network, either Modbus ASCII or Modbus RTU must be used exclusively on a single
network.
Request
Modbus
Master
Response
At the physical layer, Modbus ASCII/RTU often uses the RS-485 standard. Connecting the RL78
microcontroller with the Renesas RS-485 transceiver via UART makes it easy to implement communication
by slave devices over Modbus ASCII/RTU. The RL78 microcontroller is particularly ideal for simple functions
such as low-power operation, I/O control, and temperature and humidity measurement. Early-stage
development can be done using our sample programs.
Summary
This Application Note describes a sample program that combines an RL78 microcontroller with a
Renesas RS-485 transceiver to enable master/slave functionality over Modbus ASCII/RTU.
1. Specifications ............................................................................................................................ 5
ISL32704EVAL1Z
PmodUSBUART
ISL32704EVAL1Z
RL78/G14 Fast Prototyping Board
USB cable
(debugger)
PmodUSBUART
Host PC
ISL32704EVAL1Z
ISL32704EVAL1Z
RL78/G14 Fast Prototyping Board
USB cable
(debugger)
PmodUSBUART
Host PC
Modbus device
RS-485 cable
(Modbus communication)
Figure 4-3 Example Configuration for the RL78 – User Modbus Device Environment
4.2 Table of Pins Connecting the RL78/G14 Fast Prototyping Board and the
ISL32704EVAL1Z
Table 4-1 shows the Pins Connecting the RL78/G14 Fast Prototyping Board and the ISL32704EVAL1Z..
Table 4-1 Pins Connecting the RL78/G14 Fast Prototyping Board and the ISL32704EVAL1Z
RL78/G14 Fast Prototyping Board ISL32704EEVAL1Z
Connector Pin Number Pin Name Connector Pin Number Pin Name
Pmod 1 TM
1 P74 J2 2, 3 RE#, DE
2 P51/TxD0 4 D
3 P50/RxD0 1 R
5 GND J1 2 GND1
6 TARGET_VCC 1 VDD1
Figure 4-4 The photo of connection between RL78/G14 Fast Prototyping Board and ISL32704EVAL1Z
an-r01an5380jj0100_rl78_modbus/
├ CodeGenerator/
│ ├ iodefine.h
│ ├ r_cg_cgc.c
│ ├ r_cg_cgc.h
│ ├ r_cg_cgc_user.c
│ ├ r_cg_macrodriver.h
│ ├ r_cg_port.c
│ ├ r_cg_port.h
│ ├ r_cg_port_user.c
│ ├ r_cg_userdefine.h
│ ├ r_main.c
│ └ r_systeminit.c
│
└Modbus/
├ r_modbus.c
├ r_modbus.h
├ r_modbus_serial.c
├ r_modbus_serial.h
├ r_modbus_timer.c
└ r_modbus_timer.h
Main
Is Modbus receive
flag ON?
No
Yes
Is slave address
addressed to me?
No
Yes
Yes
Is checksum
valid?
No
Yes
Restart TAU00
No No
Yes Yes
End
TAU00 interrupt
End
Figure 5-4 Interval Between Characters Error Interrupt Handling (Slave Mode (ASCII))
In this sample code, a single timer (TAU00) is used to make two determinations: the interval between
characters and the interval between frames. After the timer interrupt occurs for the interval between
characters, TAU00 is changed to 1158 [us] (2026 − 868) and used to determine the interval between frames.
Finally, the Modbus communication frame logs and error logs are output to UART2.
Main
Is Modbus
receive flag ON?
No
Yes
Is slave address
addressed to me? No
Yes
Yes
Is checksum
valid?
No
Yes
Restart TAU00
Yes
End
TAU00 interrupt
Restart TAU00
End
Figure 5-7 Interval Between Characters Interrupt Handling (Slave Mode (RTU))
TAU00 interrupt
End
Main
Start TAU01
Is Modbus
receive flag ON?
No
Yes
Is function code
valid?
No
Yes
Is checksum
valid?
No
Yes
TAU01 interrupt
End
Figure 5-10 Read Coil Send Interrupt Handling (Master Mode (ASCII))
Restart TAU00
End
TAU00 interrupt
End
Figure 5-12 Interval Between Characters Error Handling (Master Mode (ASCII))
In this sample code, a single timer (TAU00) is used to make two determinations: the interval between
characters and the interval between frames. After the timer interrupt occurs for the interval between
characters, TAU00 is changed to 1158 [us] (2026 − 868) and used to determine the interval between frames.
The Read Coils transmission interval (1 second) uses TAU01.
Finally, the Modbus communication frame logs and error logs are output to UART2.
Main
Start TAU01
Is Modbus
receive flag ON?
No
Yes
Is function code
valid?
No
Yes
Is checksum
valid?
No
Yes
TAU01 interrupt
End
Figure 5-14 Read Coil Send Interrupt Handling (Master Mode (RTU))
Restart TAU00
Yes
End
TAU00 interrupt
Restart TAU00
End
Figure 5-16 Interval Between Characters Interrupt Handling (Master Mode (RTU))
TAU00 interrupt
End
Note: In this example, the RL78 communicates in slave mode (RTU) and Master Mode (RTU), but you
can change the communication method by changing the MODBUS_MODE setting. For the settings see 5.7.1
Modbus Operation Configuration Constants.
All trademarks and registered trademarks are the property of their respective owners.
Revision History
Description
Rev. Date Page Summary
1.00 Jun. 25, 2020 — First Edition
1.01 Jul. 08, 2020 10, 43 Added the photo of connection between the boards
1.02 Apr. 05, 2021 13 Correction of error in Discrete Output and Discrete Input
Trademarks
Renesas and the Renesas logo are trademarks of Renesas Electronics
Corporation. All trademarks and registered trademarks are the property
of their respective owners.