Skip to content

Commit b198071

Browse files
committed
Opta articles update (Additional adjustments)
1 parent b9683ea commit b198071

File tree

4 files changed

+47
-70
lines changed

4 files changed

+47
-70
lines changed

content/hardware/05.pro-solutions/solutions-and-kits/opta/tutorials/getting-started-with-modbus-rtu/content.md

Lines changed: 40 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ hardware:
2323

2424
## Overview
2525

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.
2727

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.
2929

3030
## Goals
3131

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
3333
- Learn how to use the Modbus RTU communication protocol between two Opta™ devices
3434

3535
### Required Hardware and Software
@@ -39,9 +39,9 @@ The **Opta™**, with its industrial hardware and software capabilities, and the
3939
- Opta™ PLC with RS-485 support (x2)
4040
- 12VDC/1A DIN rail power supply (x1)
4141
- USB-C® cable (x1)
42-
- Wires similar to the following specs (x3):
43-
- - STP/UTP 24-18AWG (Unterminated)/22-16AWG (Terminated)
44-
- - 100-130Ω rated for RS-485 connection
42+
- 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
4545

4646
### Software Requirements
4747

@@ -50,34 +50,33 @@ The **Opta™**, with its industrial hardware and software capabilities, and the
5050

5151
## Modbus Protocol
5252

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).
5454

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).
5656

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).
5858

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™.
6060

6161
## Instructions
6262

6363
### Setting Up the Arduino IDE
6464

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.
6766

6867
### Connecting the Opta™ Over RS-485
6968

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.
7170

7271
![Connecting two Opta™ devices via RS-485](assets/opta-modbus-connection.svg)
7372

7473
### Code Overview
7574

7675
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.
7776

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.
7978

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.
8180

8281
#### Modbus RTU Client
8382

@@ -109,7 +108,7 @@ void setup() {
109108
110109
RS485.setDelays(preDelayBR, postDelayBR);
111110
112-
// start the Modbus RTU client
111+
// Start the Modbus RTU client
113112
if (!ModbusRTUClient.begin(baudrate, SERIAL_8E1)) {
114113
Serial.println("Failed to start Modbus RTU Client!");
115114
while (1);
@@ -119,7 +118,7 @@ void setup() {
119118

120119
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).
121120

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.
123122

124123
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.
125124

@@ -161,7 +160,6 @@ The complete code for the Client is shown below:
161160
constexpr auto baudrate { 19200 };
162161
163162
// Calculate preDelay and postDelay in microseconds as per Modbus RTU Specification
164-
//
165163
// MODBUS over serial line specification and implementation guide V1.02
166164
// Paragraph 2.5.1.1 MODBUS Message RTU Framing
167165
// https://modbus.org/docs/Modbus_over_serial_line_V1_02.pdf
@@ -180,7 +178,7 @@ void setup() {
180178
181179
RS485.setDelays(preDelayBR, postDelayBR);
182180
183-
// start the Modbus RTU client
181+
// Start the Modbus RTU client
184182
if (!ModbusRTUClient.begin(baudrate, SERIAL_8E1)) {
185183
Serial.println("Failed to start Modbus RTU Client!");
186184
while (1);
@@ -210,13 +208,12 @@ void loop() {
210208
Writes Coil values to the server under specified address.
211209
*/
212210
void writeCoilValues() {
213-
// set the coils to 1 when counter is odd
211+
// Set the coils to 1 when counter is odd
214212
byte coilValue = ((counter % 2) == 0) ? 0x00 : 0x01;
215213
216214
Serial.print("Writing Coil values ... ");
217215
218-
// write 10 Coil values to (server) id 42, address 0x00
219-
216+
// Srite 10 Coil values to (server) id 42, address 0x00
220217
ModbusRTUClient.beginTransmission(42, COILS, 0x00, 10);
221218
for (int i = 0; i < 10; i++) {
222219
ModbusRTUClient.write(coilValue);
@@ -238,8 +235,7 @@ void writeCoilValues() {
238235
void readCoilValues() {
239236
Serial.print("Reading Coil values ... ");
240237
241-
// read 10 Coil values from (server) id 42, address 0x00
242-
238+
// Read 10 Coil values from (server) id 42, address 0x00
243239
if (!ModbusRTUClient.requestFrom(42, COILS, 0x00, 10)) {
244240
Serial.print("failed! ");
245241
Serial.println(ModbusRTUClient.lastError());
@@ -263,8 +259,7 @@ void readCoilValues() {
263259
void readDiscreteInputValues() {
264260
Serial.print("Reading Discrete Input values ... ");
265261
266-
// read 10 Discrete Input values from (server) id 42, address 0x00
267-
262+
// Read 10 Discrete Input values from (server) id 42, address 0x00
268263
if (!ModbusRTUClient.requestFrom(42, DISCRETE_INPUTS, 0x00, 10)) {
269264
Serial.print("failed! ");
270265
Serial.println(ModbusRTUClient.lastError());
@@ -286,12 +281,10 @@ void readDiscreteInputValues() {
286281
Writes Holding Register values to the server under specified address.
287282
*/
288283
void writeHoldingRegisterValues() {
289-
// set the Holding Register values to counter
290-
284+
//Set the Holding Register values to counter
291285
Serial.print("Writing Holding Registers values ... ");
292286
293-
// write 10 coil values to (server) id 42, address 0x00
294-
287+
// Write 10 coil values to (server) id 42, address 0x00
295288
ModbusRTUClient.beginTransmission(42, HOLDING_REGISTERS, 0x00, 10);
296289
for (int i = 0; i < 10; i++) {
297290
ModbusRTUClient.write(counter);
@@ -313,8 +306,7 @@ void writeHoldingRegisterValues() {
313306
void readHoldingRegisterValues() {
314307
Serial.print("Reading Holding Register values ... ");
315308
316-
// read 10 Input Register values from (server) id 42, address 0x00
317-
309+
// Read 10 Input Register values from (server) id 42, address 0x00
318310
if (!ModbusRTUClient.requestFrom(42, HOLDING_REGISTERS, 0x00, 10)) {
319311
Serial.print("failed! ");
320312
Serial.println(ModbusRTUClient.lastError());
@@ -338,8 +330,7 @@ void readHoldingRegisterValues() {
338330
void readInputRegisterValues() {
339331
Serial.print("Reading input register values ... ");
340332
341-
// read 10 discrete input values from (server) id 42,
342-
333+
// Read 10 discrete input values from (server) id 42,
343334
if (!ModbusRTUClient.requestFrom(42, INPUT_REGISTERS, 0x00, 10)) {
344335
Serial.print("failed! ");
345336
Serial.println(ModbusRTUClient.lastError());
@@ -369,7 +360,6 @@ In the Opta™ Server, the main task will be to poll for Modbus RTU requests and
369360
constexpr auto baudrate { 19200 };
370361
371362
// Calculate preDelay and postDelay in microseconds as per Modbus RTU Specification
372-
//
373363
// MODBUS over serial line specification and implementation guide V1.02
374364
// Paragraph 2.5.1.1 MODBUS Message RTU Framing
375365
// https://modbus.org/docs/Modbus_over_serial_line_V1_02.pdf
@@ -391,28 +381,28 @@ void setup() {
391381
392382
RS485.setDelays(preDelayBR, postDelayBR);
393383
394-
// start the Modbus RTU client
384+
// Start the Modbus RTU client
395385
if (!ModbusRTUServer.begin(42, baudrate, SERIAL_8E1)) {
396386
Serial.println("Failed to start Modbus RTU Server!");
397387
398388
while (1);
399389
}
400390
401-
// configure coils at address 0x00
391+
// Configure coils at address 0x00
402392
ModbusRTUServer.configureCoils(0x00, numCoils);
403393
404-
// configure discrete inputs at address 0x00
394+
// Configure discrete inputs at address 0x00
405395
ModbusRTUServer.configureDiscreteInputs(0x00, numDiscreteInputs);
406396
407-
// configure holding registers at address 0x00
397+
// Configure holding registers at address 0x00
408398
ModbusRTUServer.configureHoldingRegisters(0x00, numHoldingRegisters);
409399
410-
// configure input registers at address 0x00
400+
// Configure input registers at address 0x00
411401
ModbusRTUServer.configureInputRegisters(0x00, numInputRegisters);
412402
}
413403
```
414404

415-
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:
416406

417407
```arduino
418408
ModbusRTUServer.poll();
@@ -435,7 +425,6 @@ This is the line that polls for Modbus RTU requests. The complete code for the S
435425
constexpr auto baudrate { 19200 };
436426
437427
// Calculate preDelay and postDelay in microseconds as per Modbus RTU Specification
438-
//
439428
// MODBUS over serial line specification and implementation guide V1.02
440429
// Paragraph 2.5.1.1 MODBUS Message RTU Framing
441430
// https://modbus.org/docs/Modbus_over_serial_line_V1_02.pdf
@@ -457,37 +446,37 @@ void setup() {
457446
458447
RS485.setDelays(preDelayBR, postDelayBR);
459448
460-
// start the Modbus RTU client
449+
// Start the Modbus RTU client
461450
if (!ModbusRTUServer.begin(42, baudrate, SERIAL_8E1)) {
462451
Serial.println("Failed to start Modbus RTU Client!");
463452
while (1);
464453
}
465454
466-
// configure coils at address 0x00
455+
// Configure coils at address 0x00
467456
ModbusRTUServer.configureCoils(0x00, numCoils);
468457
469-
// configure discrete inputs at address 0x00
458+
// Configure discrete inputs at address 0x00
470459
ModbusRTUServer.configureDiscreteInputs(0x00, numDiscreteInputs);
471460
472-
// configure holding registers at address 0x00
461+
// Configure holding registers at address 0x00
473462
ModbusRTUServer.configureHoldingRegisters(0x00, numHoldingRegisters);
474463
475-
// configure input registers at address 0x00
464+
// Configure input registers at address 0x00
476465
ModbusRTUServer.configureInputRegisters(0x00, numInputRegisters);
477466
}
478467
479468
void loop() {
480-
// poll for Modbus RTU requests
469+
// Poll for Modbus RTU requests
481470
ModbusRTUServer.poll();
482471
483-
// map the coil values to the discrete input values
472+
// Map the coil values to the discrete input values
484473
for (int i = 0; i < numCoils; i++) {
485474
int coilValue = ModbusRTUServer.coilRead(i);
486475
487476
ModbusRTUServer.discreteInputWrite(i, coilValue);
488477
}
489478
490-
// map the holding register values to the input register values
479+
// Map the holding register values to the input register values
491480
for (int i = 0; i < numHoldingRegisters; i++) {
492481
long holdingRegisterValue = ModbusRTUServer.holdingRegisterRead(i);
493482
@@ -498,7 +487,7 @@ void loop() {
498487

499488
### Testing the Modbus RTU Client and Server
500489

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:
502491

503492
![Modbus RTU Client and Server communication status](assets/opta-modbus-client.svg)
504493

0 commit comments

Comments
 (0)