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/07.opta/opta-family/opta/tutorials/19.getting-started-with-modbus-tcp/content.md
+31-9Lines changed: 31 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,6 @@ difficulty: intermediate
11
11
tags:
12
12
- Getting-started
13
13
- ModbusTCP
14
-
- RS-485
15
14
software:
16
15
- ide-v1
17
16
- ide-v2
@@ -96,23 +95,25 @@ Install the latest versions of the following libraries required for Modbus TCP c
96
95
97
96
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.
98
97
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
+
99
100
### Connecting the Opta™ Over Ethernet LAN
100
101
101
102
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:
102
103
103
104

104
105
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.
106
107
107
108
### Code Overview
108
109
109
110
The following example aims to configure and use the Modbus TCP communication protocol over the Ethernet interface between two Opta™ devices.
110
111
111
112
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.
112
113
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 #1output will be a visual indicator for the Opta™ server.
114
115
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.
116
117
117
118
#### Modbus TCP Client
118
119
@@ -190,11 +191,32 @@ void loop() {
190
191
}
191
192
```
192
193
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.
194
216
195
217
#### Modbus TCP Server
196
218
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:
198
220
199
221
```arduino
200
222
#include <SPI.h>
@@ -282,19 +304,19 @@ void updateLED() {
282
304
}
283
305
```
284
306
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.
286
308
287
309
***You can find more information about Opta™ device's LEDs [here](https://docs.arduino.cc/tutorials/opta/user-manual/#leds).***
288
310
289
311
### Testing the Modbus TCP Client and Server
290
312
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 #1will be toggled based on the coil value from the Opta™ Client:
292
314
293
315

294
316
295
317
## Conclusion
296
318
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.
298
320
299
321
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.
0 commit comments