-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathModbusRTUClientParameters.ino
63 lines (49 loc) · 1.55 KB
/
ModbusRTUClientParameters.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
Modbus RTU Client Parameters
This sketch is the same as ModbusRTUClientToggle and shows how to set
a few RS485 parameters.
Circuit:
- Arduino Opta
- GND connected to GND of the Modbus RTU server
- A(-) connected to A of the Modbus RTU server
- B(+) connected to B of the Modbus RTU server
*/
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
#include <ArduinoModbus.h>
constexpr auto baudrate { 19200 };
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("Modbus RTU Client Toggle w/ Parameters");
// start the Modbus RTU client in 8E1 mode
if (!ModbusRTUClient.begin(baudrate, SERIAL_8E1)) {
Serial.println("Failed to start Modbus RTU Client!");
while (1);
}
}
void loop() {
// for (slave) id 1: write the value of 0x01, to the coil at address 0x00
if (!ModbusRTUClient.coilWrite(1, 0x00, 0x01)) {
Serial.print("Failed to write coil! ");
Serial.println(ModbusRTUClient.lastError());
}
// wait for 0.5 second
delay(500);
// for (slave) id 1: write the value of 0x00, to the coil at address 0x00
if (!ModbusRTUClient.coilWrite(1, 0x00, 0x00)) {
Serial.print("Failed to write coil! ");
Serial.println(ModbusRTUClient.lastError());
}
// wait for 0.5 second
delay(500);
auto coil = ModbusRTUClient.coilRead(0x02, 0x00);
if (coil < 0) {
Serial.print("Failed to read coil: ");
Serial.println(ModbusRTUClient.lastError());
} else {
Serial.print("Coil: ");
Serial.println(coil);
}
// wait for 0.5 second
delay(500);
}