Skip to content

Commit 356f23c

Browse files
committed
Tutorial content update
1 parent b23e29a commit 356f23c

File tree

1 file changed

+31
-9
lines changed
  • content/hardware/07.opta/opta-family/opta/tutorials/19.getting-started-with-modbus-tcp

1 file changed

+31
-9
lines changed

content/hardware/07.opta/opta-family/opta/tutorials/19.getting-started-with-modbus-tcp/content.md

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ difficulty: intermediate
1111
tags:
1212
- Getting-started
1313
- ModbusTCP
14-
- RS-485
1514
software:
1615
- ide-v1
1716
- ide-v2
@@ -96,23 +95,25 @@ Install the latest versions of the following libraries required for Modbus TCP c
9695

9796
You can easily install them through the Library Manager in the Arduino IDE. The Library manager can be accessed using the **"ctrl + shift+ i"** shortcut, by going to **Tools > Manage Libraries...**, or by navigating the left panel of the Arduino IDE and selecting the third option from the top.
9897

98+
These two libraries are required for properly working Modbus TCP communication, as the [**ArduinoModbus**](https://github.com/arduino-libraries/ArduinoModbus) library depends on the [**ArduinoRS485**](https://github.com/arduino-libraries/ArduinoRS485) library.
99+
99100
### Connecting the Opta™ Over Ethernet LAN
100101

101102
Set up the connection by attaching the Ethernet LAN (RJ-45) cable to both devices using the `ETH RJ45` port. The following image provides a connection diagram for both devices:
102103

103104
![Connecting two Opta™ devices via Ethernet cable with RJ45 connector](assets/opta-modbus-connection.png)
104105

105-
The setup incorporates an Ethernet switch that monitors both Opta™ devices using the PLC IDE. This configuration not only links both Opta™ devices using the PLC IDE but also lets you employ a profile to observe information exchanges in real-time. We recommend using the setup with the Ethernet switch for this tutorial to ensure optimal communication between devices.
106+
The setup in the diagram includes an optional Ethernet switch that connects and monitors both Opta™ devices using the Arduino IDE. This configuration with the Ethernet switch can be used for this tutorial, and it can be expanded to include other devices that support Modbus TCP.
106107

107108
### Code Overview
108109

109110
The following example aims to configure and use the Modbus TCP communication protocol over the Ethernet interface between two Opta™ devices.
110111

111112
Modbus is a well-known client-server protocol for its reliability. The Modbus client is responsible for sending requests, and the Modbus server provides the requested information when available. Multiple Modbus servers can be present, but only one Modbus client can be active.
112113

113-
In this example, an Opta™ client handles writing and reading coil values. At the same time, an Opta™ server polls for Modbus TCP requests and returns the appropriate values, with LED output as a visual indicator.
114+
In this example, one Opta™ device will be assigned as a client to handle writing coil values. A secondary Opta™ device will perform as a server, polling for Modbus TCP requests and returning the appropriate values. The status LED #1 output will be a visual indicator for the Opta™ server.
114115

115-
You can access the complete example code below.
116+
You can access the complete example code below. Compile and upload each example to the appointed Opta™ as you follow along with the following sections.
116117

117118
#### Modbus TCP Client
118119

@@ -190,11 +191,32 @@ void loop() {
190191
}
191192
```
192193

193-
This example allows the Opta™ client to communicate with the Opta™ server over Modbus TCP. The client attempts to connect to the server and toggles a coil value at address 0x00 every second.
194+
This example allows the Opta™ client to communicate with the Opta™ server over Modbus TCP. The client attempts to connect to the server and toggles a coil value at address 0x00 every second.
195+
196+
The IP configurations for the Opta™ client and server are defined in the following block:
197+
198+
```arduino
199+
// Define the IP address for Opta
200+
IPAddress ip(10, 0, 0, 157);
201+
202+
// Define the IP Address of the Modbus TCP server (Opta device)
203+
IPAddress server(10, 0, 0, 227);
204+
```
205+
206+
The Ethernet connection initialization process occurs within the following method inside the `setup()` function:
207+
208+
```arduino
209+
// Field arguments - Ethernet.begin(<mac>, <IP>);
210+
Ethernet.begin(NULL, ip);
211+
```
212+
213+
If both field arguments of the `Ethernet.begin(<mac>, <IP>);` are left empty, the **DHCP** configuration will be used. A **Static** IP address is used when the method is defined with a specific IP address, as in the example, which would be user assigned.
214+
215+
Defining `NULL` for the MAC address field will automatically assign a MAC address for the corresponding device.
194216

195217
#### Modbus TCP Server
196218

197-
In the Opta™ server, the main task will be to poll for Modbus TCP requests and return configured values when requested. It requires following the same initial configuration as the Opta™ client. The main difference between the client and the server devices lies in the `setup()` function:
219+
In the Opta™ server, the main task is to poll for Modbus TCP requests and control status LED #1 based on the readings. It requires the same initial configuration as the Opta™ client. The main difference between the client and server devices lies in the `setup()` function:
198220

199221
```arduino
200222
#include <SPI.h>
@@ -282,19 +304,19 @@ void updateLED() {
282304
}
283305
```
284306

285-
This example sets up the Opta™ server to listen for incoming Modbus TCP connections and handle requests. It controls the LED_D0 based on the coil value received from the client.
307+
This example sets up the Opta™ server to listen for incoming Modbus TCP connections and handle requests. It controls the **status LED #1 (`LED_D0`)** based on the coil value received from the client.
286308

287309
***You can find more information about Opta™ device's LEDs [here](https://docs.arduino.cc/tutorials/opta/user-manual/#leds).***
288310

289311
### Testing the Modbus TCP Client and Server
290312

291-
Once the Modbus TCP Client and Server code for each Opta™ device has been uploaded, the user LED will be toggles along with the coil value on the Serial Monitor of the Opta™ Client after each read-and-write task:
313+
Once the Modbus TCP Client and Server code for each Opta™ device has been uploaded, the status LED #1 will be toggled based on the coil value from the Opta™ Client:
292314

293315
![Modbus TCP Client and Server communication status](assets/opta-modbus-client.svg)
294316

295317
## Conclusion
296318

297-
This tutorial demonstrates how to use the Arduino ecosystem's `ArduinoRS485` and `ArduinoModbus` libraries and the Arduino IDE to implement the Modbus TCP protocol between two Opta™ devices. These elements are essential for allowing connections with Modbus TCP-compliant devices.
319+
This tutorial shows how to use the Arduino ecosystem's `ArduinoRS485` and `ArduinoModbus` libraries and the Arduino IDE to implement the Modbus TCP protocol between two Opta™ devices. These elements are essential for allowing connections with Modbus TCP-compliant devices.
298320

299321
With these examples, you can easily understand how to establish Modbus TCP communication between a Server and a Client using Opta™. This setup provides a scalable architecture for connecting additional Modbus Server devices, such as another Opta™ or a Modbus TCP-compatible module.
300322

0 commit comments

Comments
 (0)