You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/hardware/05.pro-solutions/solutions-and-kits/opta/tutorials/getting-started-with-modbus-rtu/content.md
+40-51Lines changed: 40 additions & 51 deletions
Original file line number
Diff line number
Diff line change
@@ -23,13 +23,13 @@ hardware:
23
23
24
24
## Overview
25
25
26
-
Modbus is an open serial protocol derived from the client/server architecture initially developed and published by Modicon (now Schneider Electric) to be used with programmable logic controllers (PLCs). Since 1979, Modbus has become a standard communications protocol in industrial electronic devices.
26
+
The **Opta™**, with its industrial hardware and software capabilities, and the Arduino ecosystem tools such as the Arduino IDE and its libraries, provide several types of Modbus communication protocol with effortless implementation thanks to its robust design.
27
27
28
-
The **Opta™**, with its industrial hardware and software capabilities, and the Arduino ecosystem tools such as the Arduino IDE and its libraries, has been designed with the Modbus communication protocol in mind, being really easy to create a new Modbus communication line using Opta™. In this tutorial, we will learn how to implement Modbus RTU communications protocol over RS-485 between two Opta™ devices.
28
+
The Modbus RTU protocol is one of the protocols available within Opta™. In this tutorial, we will learn how to implement Modbus RTU communications protocol over RS-485 between two Opta™ devices.
29
29
30
30
## Goals
31
31
32
-
- Learn how to create a RS-485 interface connection between two Opta™ devices
32
+
- Learn how to establish RS-485 interface connection between two Opta™ devices
33
33
- Learn how to use the Modbus RTU communication protocol between two Opta™ devices
34
34
35
35
### Required Hardware and Software
@@ -39,9 +39,9 @@ The **Opta™**, with its industrial hardware and software capabilities, and the
-Wire with either specification for RS-485 connection (x3):
43
+
- STP/UTP 24-18AWG (Unterminated) 100-130Ω rated
44
+
-STP/UTP 22-16AWG (Terminated) 100-130Ω rated
45
45
46
46
### Software Requirements
47
47
@@ -50,34 +50,33 @@ The **Opta™**, with its industrial hardware and software capabilities, and the
50
50
51
51
## Modbus Protocol
52
52
53
-
Modbus is an open and royalty-free serial communication protocol widely used in industrial electronic devices, especially in Building Management Systems (BMS) and Industrial Automation Systems (IAS). It was published in 1979 (more than 40 years ago) and has become a _de facto_ standard communication protocol among industrial electronic devices.
53
+
Modbus is an open and royalty-free serial communication protocol derived from the client/server architecture. It is widely used in industrial electronic devices, especially in Building Management Systems (BMS) and Industrial Automation Systems (IAS).
54
54
55
-
Modbus communication protocol is often used to connect a supervisory device with a Remote Terminal Unit (RTU) in Supervisory Control and Data Acquisition (SCADA) Systems.
55
+
It was published by Modicon (now Schneider Electric) in 1979 and has become a _de facto_ standard communication protocol among industrial electronic devices to be used with programmable logic controllers (PLCs).
56
56
57
-
Reliability in communications between electronic devices is ensured with Modbus by using messages with a simple 16-bit structure with a Cyclic-Redundant Checksum (CRC).
57
+
Modbus communication protocol is often used to connect a supervisory device with a Remote Terminal Unit (RTU) in Supervisory Control and Data Acquisition (SCADA) Systems. Reliability in communications between electronic devices is ensured with Modbus by using messages with a simple 16-bit structure with a Cyclic-Redundant Checksum (CRC).
58
58
59
-
***If you want more insights on the Modbus communication protocol, take a look at [Modbus article](https://docs.arduino.cc/learn/communication/modbus) complying as well with Opta™.***
59
+
If you want more insights on the Modbus communication protocol, take a look at [Modbus article](https://docs.arduino.cc/learn/communication/modbus) complying as well with Opta™.
60
60
61
61
## Instructions
62
62
63
63
### Setting Up the Arduino IDE
64
64
65
-
If you haven't already, head over [here](https://www.arduino.cc/en/software) and install the most recent version of the Arduino IDE along with the necessary device drivers for your computer. For additional details on Opta™, check our [getting started tutorial](/tutorials/opta/getting-started). Make sure you install the latest version of the [`ArduinoModbus`](https://www.arduino.cc/reference/en/libraries/arduinomodbus/) and the [ArduinoRS485](https://www.arduino.cc/reference/en/libraries/arduinors485/) libraries, they will be used to implement the Modbus RTU communication protocol.
66
-
65
+
If you haven't already, head over [here](https://www.arduino.cc/en/software) and install the most recent version of the Arduino IDE along with the necessary device drivers for your computer. For additional details on Opta™, check out [getting started tutorial](/tutorials/opta/getting-started). Make sure you install the latest version of the [`ArduinoModbus`](https://www.arduino.cc/reference/en/libraries/arduinomodbus/) and the [ArduinoRS485](https://www.arduino.cc/reference/en/libraries/arduinors485/) libraries, as they will be used to implement the Modbus RTU communication protocol.
67
66
68
67
### Connecting the Opta™ Over RS-485
69
68
70
-
To use the Modbus RTU communication protocol, first you need to create an RS-485 connection. Refer to the following diagram for connecting two Opta™ devices via RS-485 interface.
69
+
It requires setting up an RS-485 connection to enable the Modbus RTU communication protocol. Refer to the following diagram for connecting two Opta™ devices via the RS-485 interface.
71
70
72
71

73
72
74
73
### Code Overview
75
74
76
75
The goal of the following example is to configure and use the Modbus RTU communication protocol over the RS-485 interface between two Opta™ devices.
77
76
78
-
In the Client-Server protocol known as Modbus, the requesting device is known as the Modbus Client, and the device that will provide the requested information is known as the Modbus Server. While several Modbus Servers are allowed, only one Modbus Client is authorized. In this example, an Opta™ Client is in charge of writing and reading `Coil`, `Holding`, `Discrete Input`, and `Input` register values, while an Opta™ Server will poll for Modbus RTU requests and return the appropriate values.
77
+
The Modbus is a renowned Client-Server protocol for its reliability. The Modbus Client is responsible as a requesting device, and the Modbus Server provides requested information when available. Several Modbus Servers are allowed, but only one Modbus Client can be present. In this example, an Opta™ Client handles writing and reading `Coil`, `Holding`, `Discrete Input`, and `Input` register values, while an Opta™ Server will poll for Modbus RTU requests and return the appropriate values.
79
78
80
-
The crucial components of the code used in this tutorial are discussed in detail in the next sections to make the example easier to understand.
79
+
The crucial components of the code used in this tutorial are discussed in detail in the following sections to make the example easier to understand.
81
80
82
81
#### Modbus RTU Client
83
82
@@ -109,7 +108,7 @@ void setup() {
109
108
110
109
RS485.setDelays(preDelayBR, postDelayBR);
111
110
112
-
// start the Modbus RTU client
111
+
// Start the Modbus RTU client
113
112
if (!ModbusRTUClient.begin(baudrate, SERIAL_8E1)) {
114
113
Serial.println("Failed to start Modbus RTU Client!");
115
114
while (1);
@@ -119,7 +118,7 @@ void setup() {
119
118
120
119
The `preDelay` and `postDelay` parameters are configured for a proper operation per Modbus RTU specification. The method `RS485.setDelays(preDelayBR, postDelayBR)` is then called to correctly set and use Modbus RTU over RS-485 interface on Opta™. In this example, such parameters are applied based on the message RTU framing specifications explained in depth in this [guide](https://modbus.org/docs/Modbus_over_serial_line_V1_02.pdf).
121
120
122
-
The typical baud rates are usually `9600` and `19200`; in the current example, we are using a baud rate of `19200`, but it can be changed depending on the system requirements. For the serial port parameter, `SERIAL_8E1` is defined for setting 8 data bits, even parity, and one stop bit.
121
+
The typical baud rates are usually `9600` and `19200`; in the current example, we are using a baud rate of `19200`, but it can be changed depending on the system requirements. For the serial port parameter, `SERIAL_8E1` is used to set 8 data bits, even parity, and one stop bit.
123
122
124
123
The Modbus Server can be a module or a sensor with registers that can be accessed using specified addresses to obtain the monitored information or measurements. Inside the loop function of the sketch for the Client device, there are several tasks in charge of reading and writing specific values to access these types of data. Such data are `Coil`, `Holding`, `Discrete Input`, and `Input` register values.
125
124
@@ -161,7 +160,6 @@ The complete code for the Client is shown below:
161
160
constexpr auto baudrate { 19200 };
162
161
163
162
// Calculate preDelay and postDelay in microseconds as per Modbus RTU Specification
164
-
//
165
163
// MODBUS over serial line specification and implementation guide V1.02
In the `setup()` function of the sketch dedicated to the Modbus server, the Server address is assigned as an identifier that will be recognized by the Client. Also, the initial values of the `Coils`, `Discrete Input`, `Holding`, and `Input` registers are configured. Those are the data that the Client will locate and retrieve. The following line is necessary in the Server `loop()` function:
405
+
In the `setup()` function of the sketch dedicated to the Modbus server, the Server address is assigned with an identifier that will be recognized by the Client. Also, the initial values of the `Coils`, `Discrete Input`, `Holding`, and `Input` registers are configured. These are the data that the Client will locate and retrieve. The following line is necessary in the Server `loop()` function:
416
406
417
407
```arduino
418
408
ModbusRTUServer.poll();
@@ -435,7 +425,6 @@ This is the line that polls for Modbus RTU requests. The complete code for the S
435
425
constexpr auto baudrate { 19200 };
436
426
437
427
// Calculate preDelay and postDelay in microseconds as per Modbus RTU Specification
438
-
//
439
428
// MODBUS over serial line specification and implementation guide V1.02
// map the coil values to the discrete input values
472
+
// Map the coil values to the discrete input values
484
473
for (int i = 0; i < numCoils; i++) {
485
474
int coilValue = ModbusRTUServer.coilRead(i);
486
475
487
476
ModbusRTUServer.discreteInputWrite(i, coilValue);
488
477
}
489
478
490
-
// map the holding register values to the input register values
479
+
// Map the holding register values to the input register values
491
480
for (int i = 0; i < numHoldingRegisters; i++) {
492
481
long holdingRegisterValue = ModbusRTUServer.holdingRegisterRead(i);
493
482
@@ -498,7 +487,7 @@ void loop() {
498
487
499
488
### Testing the Modbus RTU Client and Server
500
489
501
-
Once the Modbus RTU Client and Server code for each Opta™ device has been uploaded, a `Success!` message will be displayed on the Opta's Client's Serial Monitor after each read-and-write task:
490
+
Once the Modbus RTU Client and Server code for each Opta™ device has been uploaded, a `Success!` message will be displayed on the Serial Monitor of Opta™ Client after each read-and-write task:
502
491
503
492

0 commit comments