|
1 | 1 | /*
|
2 |
| - Modbus T1S Client Toggle |
| 2 | + Modbus T1S Client |
3 | 3 |
|
4 | 4 | This sketch demonstrates how to send commands to a Modbus T1S server connected
|
5 | 5 | via T1S Single Pair Ethernet.
|
|
9 | 9 | - Uno WiFi R4
|
10 | 10 | */
|
11 | 11 |
|
12 |
| -#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library |
| 12 | +#include <ArduinoRS485.h> |
13 | 13 | #include <ArduinoModbus.h>
|
14 |
| -#include "arduino_secrets.h" |
15 |
| -/************************************************************************************** |
16 |
| - CONSTANTS |
17 |
| - **************************************************************************************/ |
18 |
| -static uint8_t const T1S_PLCA_NODE_ID = 2; |
19 |
| - |
20 |
| -static IPAddress const ip_addr { |
21 |
| - 192, 168, 42, 100 + T1S_PLCA_NODE_ID |
22 |
| -}; |
23 |
| -static IPAddress const network_mask { |
24 |
| - 255, 255, 255, 0 |
25 |
| -}; |
26 |
| -static IPAddress const gateway { |
27 |
| - 192, 168, 42, 100 |
28 |
| -}; |
29 |
| - |
30 |
| -static T1SPlcaSettings const t1s_plca_settings { |
31 |
| - T1S_PLCA_NODE_ID |
32 |
| -}; |
33 |
| -static T1SMacSettings const t1s_default_mac_settings; |
34 |
| - |
35 |
| -static IPAddress const UDP_SERVER_IP_ADDR = {192, 168, 42, 100 + 0}; |
36 | 14 |
|
37 |
| -/************************************************************************************** |
38 |
| - GLOBAL VARIABLES |
39 |
| - **************************************************************************************/ |
40 |
| -auto const tc6_io = new TC6::TC6_Io |
41 |
| -( SPI |
42 |
| - , CS_PIN |
43 |
| - , RESET_PIN |
44 |
| - , IRQ_PIN); |
45 |
| -auto const tc6_inst = new TC6::TC6_Arduino_10BASE_T1S(tc6_io); |
46 | 15 | Arduino_10BASE_T1S_UDP udp_client;
|
47 |
| - |
| 16 | +static uint8_t const T1S_PLCA_NODE_ID = 2; |
| 17 | +static uint16_t const UDP_SERVER_PORT = 8889; |
| 18 | +static uint16_t const UDP_CLIENT_PORT = 8888; |
| 19 | +#define MODBUS_ID 42 |
48 | 20 |
|
49 | 21 | void setup() {
|
50 |
| - Serial.begin(115200); |
51 |
| - while (!Serial); |
52 |
| - |
53 |
| - Serial.println("Modbus T1S Client Toggle"); |
| 22 | + Serial.begin(115200); |
54 | 23 |
|
55 |
| - /* Initialize digital IO interface for interfacing |
56 |
| - with the LAN8651. |
57 |
| - */ |
58 |
| - pinMode(IRQ_PIN, INPUT_PULLUP); |
59 |
| - attachInterrupt(digitalPinToInterrupt(IRQ_PIN), |
60 |
| - []() { |
61 |
| - tc6_io->onInterrupt(); |
62 |
| - }, |
63 |
| - FALLING); |
64 |
| - |
65 |
| - /* Initialize IO module. */ |
66 |
| - if (!tc6_io->begin()) |
67 |
| - { |
68 |
| - Serial.println("'tc6_io::begin(...)' failed."); |
69 |
| - for (;;) { } |
70 |
| - } |
71 |
| - |
72 |
| - MacAddress const mac_addr = MacAddress::create_from_uid(); |
73 |
| - |
74 |
| - if (!tc6_inst->begin(ip_addr |
75 |
| - , network_mask |
76 |
| - , gateway |
77 |
| - , mac_addr |
78 |
| - , t1s_plca_settings |
79 |
| - , t1s_default_mac_settings)) |
80 |
| - { |
81 |
| - Serial.println("'TC6::begin(...)' failed."); |
82 |
| - for (;;) { } |
83 |
| - } |
84 |
| - |
85 |
| - Serial.print("IP\t"); |
86 |
| - Serial.println(ip_addr); |
87 |
| - Serial.println(mac_addr); |
88 |
| - Serial.println(t1s_plca_settings); |
89 |
| - Serial.println(t1s_default_mac_settings); |
90 |
| - |
91 |
| - if (!udp_client.begin(UDP_CLIENT_PORT)) |
92 |
| - { |
93 |
| - Serial.println("begin(...) failed for UDP client"); |
94 |
| - for (;;) { } |
95 |
| - } |
96 |
| - |
97 |
| - /* A0 -> LOCAL_ENABLE -> DO NOT feed power from board to network. */ |
98 |
| - tc6_inst->digitalWrite(TC6::DIO::A0, false); |
99 |
| - /* A1 -> T1S_DISABLE -> Open the switch connecting network to board by pulling EN LOW. */ |
100 |
| - tc6_inst->digitalWrite(TC6::DIO::A1, false); |
101 |
| - |
102 |
| - ModbusT1SClient.setServerIp(UDP_SERVER_IP_ADDR); |
| 24 | + ModbusT1SClient.setT1SClient(&udp_client); |
| 25 | + ModbusT1SClient.setT1SPort(UDP_CLIENT_PORT); |
103 | 26 | ModbusT1SClient.setServerPort(UDP_SERVER_PORT);
|
104 | 27 | ModbusT1SClient.setModbusId(MODBUS_ID);
|
| 28 | + ModbusT1SClient.setCallback(OnPlcaStatus); |
105 | 29 |
|
106 |
| - Serial.println("UDP_Client"); |
| 30 | + if (!ModbusT1SClient.begin(T1S_PLCA_NODE_ID)) { |
| 31 | + Serial.println("Failed to start Modbus T1S Client!"); |
| 32 | + while (1); |
| 33 | + } |
107 | 34 | }
|
108 | 35 |
|
109 | 36 | void loop() {
|
110 |
| - tc6_inst->service(); |
111 |
| - |
112 |
| - static unsigned long prev_beacon_check = 0; |
113 |
| - static unsigned long prev_udp_packet_sent = 0; |
114 |
| - |
115 |
| - auto const now = millis(); |
116 |
| - |
117 |
| - if ((now - prev_beacon_check) > 1000) |
118 |
| - { |
119 |
| - prev_beacon_check = now; |
120 |
| - if (!tc6_inst->getPlcaStatus(OnPlcaStatus)) { |
121 |
| - Serial.println("getPlcaStatus(...) failed"); |
122 |
| - } |
123 |
| - } |
124 |
| - // for (slave) id 1: write the value of 0x01, to the coil at address 0x00 |
125 |
| - int res = ModbusT1SClient.coilRead(0x00, &udp_client, UDP_READ_COIL_PORT); |
| 37 | + ModbusT1SClient.checkPLCAStatus(); |
126 | 38 |
|
| 39 | + int res = ModbusT1SClient.coilRead(0x00); |
127 | 40 | if (res == -1) {
|
128 | 41 | Serial.println("Failed to read coil! ");
|
129 | 42 | } else {
|
130 | 43 | Serial.print("Coil value: ");
|
131 | 44 | Serial.println(res);
|
132 | 45 | }
|
133 | 46 |
|
134 |
| - res = ModbusT1SClient.coilWrite(0x00, 1, &udp_client, UDP_WRITE_COIL_PORT); |
| 47 | + res = ModbusT1SClient.coilWrite(0, 0x01); |
135 | 48 | if (res == -1) {
|
136 | 49 | Serial.println("Failed to write coil! ");
|
137 | 50 | } else {
|
138 | 51 | Serial.println("write done");
|
139 | 52 | }
|
140 | 53 |
|
141 |
| - res = ModbusT1SClient.inputRegisterRead(0x00, &udp_client, UDP_READ_IR_PORT); |
| 54 | + res = ModbusT1SClient.inputRegisterRead(0x00); |
142 | 55 | if (res == -1) {
|
143 | 56 | Serial.println("Failed to read Input Register! ");
|
144 | 57 | } else {
|
145 | 58 | Serial.print("Input Register value: ");
|
146 | 59 | Serial.println(res);
|
147 | 60 | }
|
148 |
| - |
149 | 61 | }
|
150 | 62 |
|
151 | 63 | static void OnPlcaStatus(bool success, bool plcaStatus)
|
|
0 commit comments