-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathModbusT1SClient.ino
77 lines (64 loc) · 1.68 KB
/
ModbusT1SClient.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
Modbus T1S Client
This sketch demonstrates how to send commands to a Modbus T1S server connected
via T1S Single Pair Ethernet.
Circuit:
- T1S shield
- Uno WiFi R4
*/
#include <ArduinoRS485.h>
#include <ArduinoModbus.h>
Arduino_10BASE_T1S_UDP udp_client;
static uint8_t const T1S_PLCA_NODE_ID = 2;
static uint16_t const UDP_SERVER_PORT = 8889;
static uint16_t const UDP_CLIENT_PORT = 8888;
#define MODBUS_ID 42
void setup() {
Serial.begin(115200);
ModbusT1SClient.setT1SClient(udp_client);
ModbusT1SClient.setT1SPort(UDP_CLIENT_PORT);
ModbusT1SClient.setServerPort(UDP_SERVER_PORT);
ModbusT1SClient.setModbusId(MODBUS_ID);
ModbusT1SClient.setCallback(OnPlcaStatus);
if (!ModbusT1SClient.begin(T1S_PLCA_NODE_ID)) {
Serial.println("Failed to start Modbus T1S Client!");
while (1);
}
}
void loop() {
ModbusT1SClient.update();
int res = ModbusT1SClient.coilRead(0x00);
if (res == -1) {
Serial.println("Failed to read coil! ");
} else {
Serial.print("Coil value: ");
Serial.println(res);
}
res = ModbusT1SClient.coilWrite(0, 0x01);
if (res == -1) {
Serial.println("Failed to write coil! ");
} else {
Serial.println("write done");
}
res = ModbusT1SClient.inputRegisterRead(0x00);
if (res == -1) {
Serial.println("Failed to read Input Register! ");
} else {
Serial.print("Input Register value: ");
Serial.println(res);
}
}
static void OnPlcaStatus(bool success, bool plcaStatus)
{
if (!success)
{
Serial.println("PLCA status register read failed");
return;
}
if (plcaStatus) {
Serial.println("PLCA Mode active");
} else {
Serial.println("CSMA/CD fallback");
tc6_inst->enablePlca();
}
}