Skip to content

Commit 0b698ce

Browse files
committed
fixed examples in accord with the new library structures
1 parent c5e9eef commit 0b698ce

File tree

8 files changed

+58
-561
lines changed

8 files changed

+58
-561
lines changed

examples/T1S/ModbusT1SClient/ModbusT1SClient.ino

+18-106
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Modbus T1S Client Toggle
2+
Modbus T1S Client
33
44
This sketch demonstrates how to send commands to a Modbus T1S server connected
55
via T1S Single Pair Ethernet.
@@ -9,143 +9,55 @@
99
- Uno WiFi R4
1010
*/
1111

12-
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
12+
#include <ArduinoRS485.h>
1313
#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};
3614

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);
4615
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
4820

4921
void setup() {
50-
Serial.begin(115200);
51-
while (!Serial);
52-
53-
Serial.println("Modbus T1S Client Toggle");
22+
Serial.begin(115200);
5423

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);
10326
ModbusT1SClient.setServerPort(UDP_SERVER_PORT);
10427
ModbusT1SClient.setModbusId(MODBUS_ID);
28+
ModbusT1SClient.setCallback(OnPlcaStatus);
10529

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+
}
10734
}
10835

10936
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();
12638

39+
int res = ModbusT1SClient.coilRead(0x00);
12740
if (res == -1) {
12841
Serial.println("Failed to read coil! ");
12942
} else {
13043
Serial.print("Coil value: ");
13144
Serial.println(res);
13245
}
13346

134-
res = ModbusT1SClient.coilWrite(0x00, 1, &udp_client, UDP_WRITE_COIL_PORT);
47+
res = ModbusT1SClient.coilWrite(0, 0x01);
13548
if (res == -1) {
13649
Serial.println("Failed to write coil! ");
13750
} else {
13851
Serial.println("write done");
13952
}
14053

141-
res = ModbusT1SClient.inputRegisterRead(0x00, &udp_client, UDP_READ_IR_PORT);
54+
res = ModbusT1SClient.inputRegisterRead(0x00);
14255
if (res == -1) {
14356
Serial.println("Failed to read Input Register! ");
14457
} else {
14558
Serial.print("Input Register value: ");
14659
Serial.println(res);
14760
}
148-
14961
}
15062

15163
static void OnPlcaStatus(bool success, bool plcaStatus)

examples/T1S/ModbusT1SClient/arduino_secrets.h

-9
This file was deleted.

examples/T1S/ModbusT1SClientTemperatureHumiditySensor/ModbusT1SClientTemperatureHumiditySensor.ino

+17-104
Original file line numberDiff line numberDiff line change
@@ -9,133 +9,46 @@
99
- all the terminations placed on the hardware
1010
*/
1111

12-
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
12+
#include <ArduinoRS485.h>
1313
#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-
};
2914

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};
15+
static uint8_t const T1S_PLCA_NODE_ID = 2;
16+
static uint16_t const UDP_SERVER_PORT = 8889;
17+
static uint16_t const UDP_CLIENT_PORT = 8888;
3618

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);
4619
Arduino_10BASE_T1S_UDP udp_client;
4720

48-
4921
void setup() {
5022
Serial.begin(115200);
51-
while (!Serial);
52-
53-
Serial.println("Modbus T1S Client Toggle");
54-
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);
9023

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, true);
101-
102-
ModbusT1SClient.setServerIp(UDP_SERVER_IP_ADDR);
24+
ModbusT1SClient.setT1SClient(&udp_client);
25+
ModbusT1SClient.setT1SPort(UDP_CLIENT_PORT);
10326
ModbusT1SClient.setServerPort(UDP_SERVER_PORT);
27+
ModbusT1SClient.setCallback(OnPlcaStatus);
10428

105-
106-
Serial.println("UDP_Client");
29+
if (!ModbusT1SClient.begin(T1S_PLCA_NODE_ID)) {
30+
Serial.println("Failed to start Modbus T1S Client!");
31+
while (1);
32+
}
10733
}
10834

10935
unsigned long start = 0;
11036
void loop() {
111-
tc6_inst->service();
112-
113-
static unsigned long prev_beacon_check = 0;
114-
static unsigned long prev_udp_packet_sent = 0;
115-
116-
auto const now = millis();
117-
118-
if ((now - prev_beacon_check) > 1000)
119-
{
120-
prev_beacon_check = now;
121-
if (!tc6_inst->getPlcaStatus(OnPlcaStatus)) {
122-
Serial.println("getPlcaStatus(...) failed");
123-
}
124-
}
37+
ModbusT1SClient.checkPLCAStatus();
12538

12639
if ((millis() - start) > 1000)
12740
{
128-
int res = ModbusT1SClient.inputRegisterRead(1, 0x01, &udp_client, UDP_READ_IR_PORT);
41+
int res = ModbusT1SClient.inputRegisterRead(1, 0x01);
12942
if (res == -1) {
13043
Serial.println("Failed to read temperature! ");
13144
} else {
132-
int16_t const temperature_raw =res;
45+
int16_t const temperature_raw = res;
13346
float const temperature_deg = temperature_raw / 10.f;
13447
Serial.print("Temperature: ");
13548
Serial.println(temperature_deg);
13649
}
137-
138-
res = ModbusT1SClient.inputRegisterRead(1, 0x02, &udp_client, UDP_READ_IR_PORT);
50+
51+
res = ModbusT1SClient.inputRegisterRead(1, 0x02);
13952
if (res == -1) {
14053
Serial.println("Failed to read humidity! ");
14154
} else {
@@ -162,4 +75,4 @@ static void OnPlcaStatus(bool success, bool plcaStatus)
16275
Serial.println("CSMA/CD fallback");
16376
tc6_inst->enablePlca();
16477
}
165-
}
78+
}

examples/T1S/ModbusT1SClientTemperatureHumiditySensor/arduino_secrets.h

-8
This file was deleted.

0 commit comments

Comments
 (0)