Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0d8842d

Browse files
committedOct 31, 2024·
fixed examples in accord with the new library structures
1 parent c5e9eef commit 0d8842d

File tree

8 files changed

+73
-560
lines changed

8 files changed

+73
-560
lines changed
 

‎examples/T1S/ModbusT1SClient/ModbusT1SClient.ino

Lines changed: 18 additions & 106 deletions
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

Lines changed: 0 additions & 9 deletions
This file was deleted.

‎examples/T1S/ModbusT1SClientTemperatureHumiditySensor/ModbusT1SClientTemperatureHumiditySensor.ino

Lines changed: 17 additions & 104 deletions
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

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 12 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Modbus T1S Server LED
2+
Modbus T1S Server
33
44
55
This sketch demonstrates how to receive commands from a Modbus T1S Client connected
@@ -10,176 +10,30 @@
1010
- Uno WiFi R4
1111
*/
1212

13-
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
13+
#include <ArduinoRS485.h>
1414
#include <ArduinoModbus.h>
15-
#include "arduino_secrets.h"
1615

17-
/**************************************************************************************
18-
CONSTANTS
19-
**************************************************************************************/
20-
#define RS485_SERIAL Serial1
21-
#define RS485_TX_PIN 1
22-
#define RS485_RX_PIN 0
23-
#define RS485_DE_PIN 8
24-
#define RS485_RE_PIN 7
25-
#define PRE_DELAY 100
26-
#define POST_DELAY 100
27-
#define PWR_CARRIER_RATIO 0.18f
28-
RS485Class serial485(RS485_SERIAL, RS485_TX_PIN, RS485_DE_PIN, RS485_RE_PIN);
16+
static uint8_t const T1S_PLCA_NODE_ID = 0;
17+
static uint16_t const UDP_SERVER_PORT = 8889;
2918

30-
static unsigned int const MODBUS_BAUDRATE = 9600;
31-
static float const MODBUS_BIT_DURATION = 1.f / MODBUS_BAUDRATE;
32-
static float const MODBUS_PRE_DELAY_BR = MODBUS_BIT_DURATION * 9.6f * 3.5f * 1e6;
33-
static float const MODBUS_POST_DELAY_BR = MODBUS_BIT_DURATION * 9.6f * 3.5f * 1e6;
34-
35-
static int const MODBUS_DEVICE_ID = 1;
36-
static int const MODBUS_DEVICE_TEMPERATURE_REGISTER = 0x0001;
37-
static int const MODBUS_DEVICE_HUMIDITY_REGISTER = 0x0002;
38-
39-
static uint8_t const T1S_PLCA_NODE_ID = 0; /* The UDP server doubles as PLCA coordinator. */
40-
41-
static IPAddress const ip_addr {
42-
192, 168, 42, 100 + T1S_PLCA_NODE_ID
43-
};
44-
static IPAddress const network_mask {
45-
255, 255, 255, 0
46-
};
47-
static IPAddress const gateway {
48-
192, 168, 42, 100
49-
};
50-
51-
static T1SPlcaSettings const t1s_plca_settings {
52-
T1S_PLCA_NODE_ID
53-
};
54-
static T1SMacSettings const t1s_default_mac_settings;
55-
56-
/**************************************************************************************
57-
GLOBAL VARIABLES
58-
**************************************************************************************/
59-
auto const tc6_io = new TC6::TC6_Io
60-
( SPI
61-
, CS_PIN
62-
, RESET_PIN
63-
, IRQ_PIN);
64-
auto const tc6_inst = new TC6::TC6_Arduino_10BASE_T1S(tc6_io);
6519
Arduino_10BASE_T1S_UDP udp_server;
6620

67-
/**************************************************************************************
68-
SETUP/LOOP
69-
**************************************************************************************/
7021
void setup() {
7122
Serial.begin(115200);
7223

73-
Serial.println("Modbus RTU Server LED");
74-
75-
/* Initialize digital IO interface for interfacing
76-
with the LAN8651.
77-
*/
78-
pinMode(IRQ_PIN, INPUT_PULLUP);
79-
attachInterrupt(digitalPinToInterrupt(IRQ_PIN),
80-
[]() {
81-
tc6_io->onInterrupt();
82-
},
83-
FALLING);
84-
85-
/* Initialize IO module. */
86-
if (!tc6_io->begin())
87-
{
88-
Serial.println("'TC6_Io::begin(...)' failed.");
89-
for (;;) { }
90-
}
91-
92-
MacAddress const mac_addr = MacAddress::create_from_uid();
93-
94-
if (!tc6_inst->begin(ip_addr
95-
, network_mask
96-
, gateway
97-
, mac_addr
98-
, t1s_plca_settings
99-
, t1s_default_mac_settings))
100-
{
101-
Serial.println("'TC6::begin(...)' failed.");
102-
for (;;) { }
103-
}
104-
105-
Serial.print("IP\t");
106-
Serial.println(ip_addr);
107-
Serial.println(mac_addr);
108-
Serial.println(t1s_plca_settings);
109-
Serial.println(t1s_default_mac_settings);
110-
111-
if (!udp_server.begin(UDP_SERVER_PORT))
112-
{
113-
Serial.println("begin(...) failed for UDP coil read server ");
114-
for (;;) { }
115-
}
116-
117-
tc6_inst->digitalWrite(TC6::DIO::A0, true);
118-
/* A1 -> T1S_DISABLE -> close the switch connecting network to board. */
119-
tc6_inst->digitalWrite(TC6::DIO::A1, true);
24+
ModbusT1SServer.setT1SServer(&udp_server);
25+
ModbusT1SServer.setT1SPort(UDP_SERVER_PORT);
26+
ModbusT1SServer.setBadrate(9600);
27+
ModbusT1SServer.setCallback(OnPlcaStatus);
12028

121-
serial485.setDelays(MODBUS_PRE_DELAY_BR, MODBUS_POST_DELAY_BR);
122-
if (!ModbusT1SServer.begin(serial485, MODBUS_BAUDRATE, SERIAL_8N1)) {
123-
Serial.println("Failed to start Modbus RTU Client!");
29+
if (!ModbusT1SServer.begin(T1S_PLCA_NODE_ID)) {
30+
Serial.println("Failed to start Modbus T1S Server!");
12431
while (1);
12532
}
126-
127-
ModbusT1SServer.setT1SServer(&udp_server);
128-
Serial.println("UDP_Server");
12933
}
13034

13135
void loop() {
132-
/* Services the hardware and the protocol stack.
133-
Must be called cyclic. The faster the better.
134-
*/
135-
tc6_inst->service();
136-
137-
static unsigned long prev_beacon_check = 0;
138-
139-
auto const now = millis();
140-
if ((now - prev_beacon_check) > 1000)
141-
{
142-
prev_beacon_check = now;
143-
if (!tc6_inst->getPlcaStatus(OnPlcaStatus)) {
144-
Serial.println("getPlcaStatus(...) failed");
145-
}
146-
}
147-
148-
switch (ModbusT1SServer.parsePacket())
149-
{
150-
case UDP_READ_COIL_PORT:
151-
Serial.println("Read coil");
152-
ModbusT1SServer.coilRead();
153-
break;
154-
155-
case UDP_WRITE_COIL_PORT:
156-
Serial.println("Write coil");
157-
ModbusT1SServer.coilWrite();
158-
break;
159-
160-
case UDP_READ_DI_PORT:
161-
Serial.println("Read discrete input");
162-
ModbusT1SServer.discreteInputRead();
163-
break;
164-
165-
case UDP_READ_IR_PORT:
166-
Serial.println("Read input register");
167-
ModbusT1SServer.inputRegisterRead();
168-
break;
169-
170-
case UDP_READ_HR_PORT:
171-
Serial.println("Read holding register");
172-
ModbusT1SServer.holdingRegisterRead();
173-
break;
174-
175-
case UDP_WRITE_HR_PORT:
176-
Serial.println("Write holding register");
177-
ModbusT1SServer.holdingRegisterWrite();
178-
break;
179-
180-
default:
181-
break;
182-
}
36+
ModbusT1SServer.update();
18337
}
18438

18539
static void OnPlcaStatus(bool success, bool plcaStatus)
@@ -196,4 +50,4 @@ static void OnPlcaStatus(bool success, bool plcaStatus)
19650
Serial.println("CSMA/CD fallback");
19751
tc6_inst->enablePlca();
19852
}
199-
}
53+
}

‎examples/T1S/ModbusT1SServer/arduino_secrets.h

Lines changed: 0 additions & 7 deletions
This file was deleted.

‎examples/T1S/ModbusT1SServerTemperatureHumiditySensor/ModbusT1SServerTemperatureHumiditySensor.ino

Lines changed: 26 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -14,181 +14,46 @@
1414
- Z connected to B/Z of the Modbus RTU client
1515
- all the terminations placed on the hardware
1616
*/
17+
/*
18+
Modbus T1S Server Temperature Humidity sensor
1719
18-
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
19-
#include <ArduinoModbus.h>
20-
#include "arduino_secrets.h"
21-
22-
/**************************************************************************************
23-
CONSTANTS
24-
**************************************************************************************/
25-
#define RS485_SERIAL Serial1
26-
#define RS485_TX_PIN 1
27-
#define RS485_RX_PIN 0
28-
#define RS485_DE_PIN 8
29-
#define RS485_RE_PIN 7
30-
#define PRE_DELAY 100
31-
#define POST_DELAY 100
32-
#define PWR_CARRIER_RATIO 0.18f
33-
RS485Class serial485(RS485_SERIAL, RS485_TX_PIN, RS485_DE_PIN, RS485_RE_PIN);
34-
35-
static unsigned int const MODBUS_BAUDRATE = 9600;
36-
static float const MODBUS_BIT_DURATION = 1.f / MODBUS_BAUDRATE;
37-
static float const MODBUS_PRE_DELAY_BR = MODBUS_BIT_DURATION * 9.6f * 3.5f * 1e6;
38-
static float const MODBUS_POST_DELAY_BR = MODBUS_BIT_DURATION * 9.6f * 3.5f * 1e6;
39-
40-
static int const MODBUS_DEVICE_ID = 1;
41-
static int const MODBUS_DEVICE_TEMPERATURE_REGISTER = 0x0001;
42-
static int const MODBUS_DEVICE_HUMIDITY_REGISTER = 0x0002;
20+
This sketch creates a Modbus T1S Server and demonstrates
21+
how to use read temperature and humidity values as sensed
22+
by the RTU Modbus capable MD02 sensor.
4323
44-
static uint8_t const T1S_PLCA_NODE_ID = 0; /* The UDP server doubles as PLCA coordinator. */
24+
Circuit:
25+
- Arduino Uno Wifi R4
26+
- T1S shield
27+
- SPE ethernet connected to the client
28+
- ISO GND connected to GND of the Modbus RTU server
29+
- Y connected to A/Y of the Modbus RTU client
30+
- Z connected to B/Z of the Modbus RTU client
31+
- all the terminations placed on the hardware
32+
*/
4533

46-
static IPAddress const ip_addr {
47-
192, 168, 42, 100 + T1S_PLCA_NODE_ID
48-
};
49-
static IPAddress const network_mask {
50-
255, 255, 255, 0
51-
};
52-
static IPAddress const gateway {
53-
192, 168, 42, 100
54-
};
34+
#include <ArduinoRS485.h>
35+
#include <ArduinoModbus.h>
5536

56-
static T1SPlcaSettings const t1s_plca_settings {
57-
T1S_PLCA_NODE_ID
58-
};
59-
static T1SMacSettings const t1s_default_mac_settings;
37+
static uint8_t const T1S_PLCA_NODE_ID = 0;
38+
static uint16_t const UDP_SERVER_PORT = 8889;
6039

61-
/**************************************************************************************
62-
GLOBAL VARIABLES
63-
**************************************************************************************/
64-
auto const tc6_io = new TC6::TC6_Io
65-
( SPI
66-
, CS_PIN
67-
, RESET_PIN
68-
, IRQ_PIN);
69-
auto const tc6_inst = new TC6::TC6_Arduino_10BASE_T1S(tc6_io);
7040
Arduino_10BASE_T1S_UDP udp_server;
7141

72-
/**************************************************************************************
73-
SETUP/LOOP
74-
**************************************************************************************/
7542
void setup() {
7643
Serial.begin(115200);
7744

78-
Serial.println("Modbus RTU Server LED");
79-
80-
/* Initialize digital IO interface for interfacing
81-
with the LAN8651.
82-
*/
83-
pinMode(IRQ_PIN, INPUT_PULLUP);
84-
attachInterrupt(digitalPinToInterrupt(IRQ_PIN),
85-
[]() {
86-
tc6_io->onInterrupt();
87-
},
88-
FALLING);
89-
90-
/* Initialize IO module. */
91-
if (!tc6_io->begin())
92-
{
93-
Serial.println("'TC6_Io::begin(...)' failed.");
94-
for (;;) { }
95-
}
96-
97-
MacAddress const mac_addr = MacAddress::create_from_uid();
98-
99-
if (!tc6_inst->begin(ip_addr
100-
, network_mask
101-
, gateway
102-
, mac_addr
103-
, t1s_plca_settings
104-
, t1s_default_mac_settings))
105-
{
106-
Serial.println("'TC6::begin(...)' failed.");
107-
for (;;) { }
108-
}
109-
110-
Serial.print("IP\t");
111-
Serial.println(ip_addr);
112-
Serial.println(mac_addr);
113-
Serial.println(t1s_plca_settings);
114-
Serial.println(t1s_default_mac_settings);
115-
116-
if (!udp_server.begin(UDP_SERVER_PORT))
117-
{
118-
Serial.println("begin(...) failed for UDP coil read server ");
119-
for (;;) { }
120-
}
121-
122-
tc6_inst->digitalWrite(TC6::DIO::A0, false);
123-
/* A1 -> T1S_DISABLE -> close the switch connecting network to board. */
124-
tc6_inst->digitalWrite(TC6::DIO::A1, true);
125-
126-
serial485.setDelays(MODBUS_PRE_DELAY_BR, MODBUS_POST_DELAY_BR);
127-
unsigned long br = MODBUS_BAUDRATE;
128-
uint16_t config = SERIAL_8N1;
129-
if (!ModbusT1SServer.begin(serial485, br, config)) {
130-
Serial.println("Failed to start Modbus RTU Client!");
45+
ModbusT1SServer.setT1SServer(&udp_server);
46+
ModbusT1SServer.setT1SPort(UDP_SERVER_PORT);
47+
ModbusT1SServer.setBadrate(9600);
48+
ModbusT1SServer.setCallback(OnPlcaStatus);
49+
if (!ModbusT1SServer.begin(T1S_PLCA_NODE_ID)) {
50+
Serial.println("Failed to start Modbus T1S Server!");
13151
while (1);
13252
}
133-
134-
ModbusT1SServer.setT1SServer(&udp_server);
135-
Serial.println("UDP_Server");
136-
Serial.println(MODBUS_PRE_DELAY_BR);
137-
Serial.println(MODBUS_POST_DELAY_BR);
13853
}
13954

14055
void loop() {
141-
/* Services the hardware and the protocol stack.
142-
Must be called cyclic. The faster the better.
143-
*/
144-
tc6_inst->service();
145-
146-
static unsigned long prev_beacon_check = 0;
147-
148-
auto const now = millis();
149-
if ((now - prev_beacon_check) > 1000)
150-
{
151-
prev_beacon_check = now;
152-
if (!tc6_inst->getPlcaStatus(OnPlcaStatus)) {
153-
Serial.println("getPlcaStatus(...) failed");
154-
}
155-
}
156-
157-
switch (ModbusT1SServer.parsePacket())
158-
{
159-
case UDP_READ_COIL_PORT:
160-
Serial.println("Read coil");
161-
ModbusT1SServer.coilRead();
162-
break;
163-
164-
case UDP_WRITE_COIL_PORT:
165-
Serial.println("Write coil");
166-
ModbusT1SServer.coilWrite();
167-
break;
168-
169-
case UDP_READ_DI_PORT:
170-
Serial.println("Read discrete input");
171-
ModbusT1SServer.discreteInputRead();
172-
break;
173-
174-
case UDP_READ_IR_PORT:
175-
Serial.println("Read input register");
176-
ModbusT1SServer.inputRegisterRead();
177-
break;
178-
179-
case UDP_READ_HR_PORT:
180-
Serial.println("Read holding register");
181-
ModbusT1SServer.holdingRegisterRead();
182-
break;
183-
184-
case UDP_WRITE_HR_PORT:
185-
Serial.println("Write holding register");
186-
ModbusT1SServer.holdingRegisterWrite();
187-
break;
188-
189-
default:
190-
break;
191-
}
56+
ModbusT1SServer.update();
19257
}
19358

19459
static void OnPlcaStatus(bool success, bool plcaStatus)
@@ -205,4 +70,4 @@ static void OnPlcaStatus(bool success, bool plcaStatus)
20570
Serial.println("CSMA/CD fallback");
20671
tc6_inst->enablePlca();
20772
}
208-
}
73+
}

‎examples/T1S/ModbusT1SServerTemperatureHumiditySensor/arduino_secrets.h

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.