Skip to content

Commit 568b7ab

Browse files
committed
added examples T1S
1 parent 841af79 commit 568b7ab

File tree

8 files changed

+805
-0
lines changed

8 files changed

+805
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/*
2+
Modbus T1S Client Toggle
3+
4+
This sketch demonstrates how to send commands to a Modbus T1S server connected
5+
via T1S Single Pair Ethernet.
6+
7+
Circuit:
8+
- T1S shield
9+
- Uno WiFi R4
10+
*/
11+
12+
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
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+
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+
Arduino_10BASE_T1S_UDP udp_client;
47+
48+
49+
void setup() {
50+
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);
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);
103+
ModbusT1SClient.setServerPort(UDP_SERVER_PORT);
104+
ModbusT1SClient.setModbusId(MODBUS_ID);
105+
106+
Serial.println("UDP_Client");
107+
}
108+
109+
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(UDP_READ_COIL_PORT, &udp_client);
126+
127+
if (res == -1) {
128+
Serial.println("Failed to read coil! ");
129+
} else {
130+
Serial.print("Coil value: ");
131+
Serial.println(res);
132+
}
133+
134+
res = ModbusT1SClient.coilWrite(UDP_WRITE_COIL_PORT, 1, &udp_client);
135+
if (res == -1) {
136+
Serial.println("Failed to write coil! ");
137+
} else {
138+
Serial.println("write done");
139+
}
140+
141+
res = ModbusT1SClient.inputRegisterRead(UDP_READ_IR_PORT, &udp_client);
142+
if (res == -1) {
143+
Serial.println("Failed to read Input Register! ");
144+
} else {
145+
Serial.print("Input Register value: ");
146+
Serial.println(res);
147+
}
148+
149+
}
150+
151+
static void OnPlcaStatus(bool success, bool plcaStatus)
152+
{
153+
if (!success)
154+
{
155+
Serial.println("PLCA status register read failed");
156+
return;
157+
}
158+
159+
if (plcaStatus) {
160+
Serial.println("PLCA Mode active");
161+
} else {
162+
Serial.println("CSMA/CD fallback");
163+
tc6_inst->enablePlca();
164+
}
165+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
static uint16_t const UDP_CLIENT_PORT = 8888;
2+
static uint16_t const UDP_SERVER_PORT = 8889;
3+
#define UDP_READ_COIL_PORT 1
4+
#define UDP_WRITE_COIL_PORT 2
5+
#define UDP_READ_DI_PORT 3
6+
#define UDP_READ_IR_PORT 4
7+
#define UDP_READ_HR_PORT 5
8+
#define UDP_WRITE_HR_PORT 6
9+
#define MODBUS_ID 42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
/*
2+
Modbus T1S Client Temperature Humidity sensor
3+
4+
This sketch creates a Modbus T1S Client and demonstrates
5+
how to use the ModbusT1S API to communicate.
6+
- Arduino Uno Wifi R4
7+
- T1S shield
8+
- SPE ethernet connected to the client
9+
- all the terminations placed on the hardware
10+
*/
11+
12+
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
13+
#include <ArduinoModbus.h>
14+
#include "arduino_secrets.h"
15+
16+
/**************************************************************************************
17+
CONSTANTS
18+
**************************************************************************************/
19+
#define RS485_SERIAL Serial1
20+
#define RS485_TX_PIN 1
21+
#define RS485_RX_PIN 0
22+
#define RS485_DE_PIN 8
23+
#define RS485_RE_PIN 7
24+
#define PRE_DELAY 100
25+
#define POST_DELAY 100
26+
#define PWR_CARRIER_RATIO 0.18f
27+
RS485Class serial485(RS485_SERIAL, RS485_TX_PIN, RS485_DE_PIN, RS485_RE_PIN);
28+
29+
static unsigned int const MODBUS_BAUDRATE = 9600;
30+
static float const MODBUS_BIT_DURATION = 1.f / MODBUS_BAUDRATE;
31+
static float const MODBUS_PRE_DELAY_BR = MODBUS_BIT_DURATION * 9.6f * 3.5f * 1e6;
32+
static float const MODBUS_POST_DELAY_BR = MODBUS_BIT_DURATION * 9.6f * 3.5f * 1e6;
33+
34+
static int const MODBUS_DEVICE_ID = 1;
35+
static int const MODBUS_DEVICE_TEMPERATURE_REGISTER = 0x0001;
36+
static int const MODBUS_DEVICE_HUMIDITY_REGISTER = 0x0002;
37+
38+
static uint8_t const T1S_PLCA_NODE_ID = 0; /* The UDP server doubles as PLCA coordinator. */
39+
40+
static IPAddress const ip_addr {
41+
192, 168, 42, 100 + T1S_PLCA_NODE_ID
42+
};
43+
static IPAddress const network_mask {
44+
255, 255, 255, 0
45+
};
46+
static IPAddress const gateway {
47+
192, 168, 42, 100
48+
};
49+
50+
static T1SPlcaSettings const t1s_plca_settings {
51+
T1S_PLCA_NODE_ID
52+
};
53+
static T1SMacSettings const t1s_default_mac_settings;
54+
55+
/**************************************************************************************
56+
GLOBAL VARIABLES
57+
**************************************************************************************/
58+
auto const tc6_io = new TC6::TC6_Io
59+
( SPI
60+
, CS_PIN
61+
, RESET_PIN
62+
, IRQ_PIN);
63+
auto const tc6_inst = new TC6::TC6_Arduino_10BASE_T1S(tc6_io);
64+
Arduino_10BASE_T1S_UDP udp_server;
65+
66+
/**************************************************************************************
67+
SETUP/LOOP
68+
**************************************************************************************/
69+
void setup() {
70+
Serial.begin(115200);
71+
72+
Serial.println("Modbus RTU Server LED");
73+
74+
/* Initialize digital IO interface for interfacing
75+
with the LAN8651.
76+
*/
77+
pinMode(IRQ_PIN, INPUT_PULLUP);
78+
attachInterrupt(digitalPinToInterrupt(IRQ_PIN),
79+
[]() {
80+
tc6_io->onInterrupt();
81+
},
82+
FALLING);
83+
84+
/* Initialize IO module. */
85+
if (!tc6_io->begin())
86+
{
87+
Serial.println("'TC6_Io::begin(...)' failed.");
88+
for (;;) { }
89+
}
90+
91+
MacAddress const mac_addr = MacAddress::create_from_uid();
92+
93+
if (!tc6_inst->begin(ip_addr
94+
, network_mask
95+
, gateway
96+
, mac_addr
97+
, t1s_plca_settings
98+
, t1s_default_mac_settings))
99+
{
100+
Serial.println("'TC6::begin(...)' failed.");
101+
for (;;) { }
102+
}
103+
104+
Serial.print("IP\t");
105+
Serial.println(ip_addr);
106+
Serial.println(mac_addr);
107+
Serial.println(t1s_plca_settings);
108+
Serial.println(t1s_default_mac_settings);
109+
110+
if (!udp_server.begin(UDP_SERVER_PORT))
111+
{
112+
Serial.println("begin(...) failed for UDP coil read server ");
113+
for (;;) { }
114+
}
115+
116+
tc6_inst->digitalWrite(TC6::DIO::A0, false);
117+
/* A1 -> T1S_DISABLE -> close the switch connecting network to board. */
118+
tc6_inst->digitalWrite(TC6::DIO::A1, true);
119+
120+
serial485.setDelays(MODBUS_PRE_DELAY_BR, MODBUS_POST_DELAY_BR);
121+
unsigned long br = MODBUS_BAUDRATE;
122+
uint16_t config = SERIAL_8N1;
123+
if (!ModbusT1SServer.begin(serial485, br, config)) {
124+
Serial.println("Failed to start Modbus RTU Client!");
125+
while (1);
126+
}
127+
128+
ModbusT1SServer.setT1SServer(&udp_server);
129+
Serial.println("UDP_Server");
130+
Serial.println(MODBUS_PRE_DELAY_BR);
131+
Serial.println(MODBUS_POST_DELAY_BR);
132+
}
133+
134+
void loop() {
135+
/* Services the hardware and the protocol stack.
136+
Must be called cyclic. The faster the better.
137+
*/
138+
tc6_inst->service();
139+
140+
static unsigned long prev_beacon_check = 0;
141+
142+
auto const now = millis();
143+
if ((now - prev_beacon_check) > 1000)
144+
{
145+
prev_beacon_check = now;
146+
if (!tc6_inst->getPlcaStatus(OnPlcaStatus)) {
147+
Serial.println("getPlcaStatus(...) failed");
148+
}
149+
}
150+
151+
switch (ModbusT1SServer.parsePacket())
152+
{
153+
case UDP_READ_COIL_PORT:
154+
Serial.println("Read coil");
155+
ModbusT1SServer.coilRead();
156+
break;
157+
158+
case UDP_WRITE_COIL_PORT:
159+
Serial.println("Write coil");
160+
ModbusT1SServer.coilWrite();
161+
break;
162+
163+
case UDP_READ_DI_PORT:
164+
Serial.println("Read discrete input");
165+
ModbusT1SServer.discreteInputRead();
166+
break;
167+
168+
case UDP_READ_IR_PORT:
169+
Serial.println("Read input register");
170+
ModbusT1SServer.inputRegisterRead();
171+
break;
172+
173+
case UDP_READ_HR_PORT:
174+
Serial.println("Read holding register");
175+
ModbusT1SServer.holdingRegisterRead();
176+
break;
177+
178+
case UDP_WRITE_HR_PORT:
179+
Serial.println("Write holding register");
180+
ModbusT1SServer.holdingRegisterWrite();
181+
break;
182+
183+
default:
184+
break;
185+
}
186+
}
187+
188+
static void OnPlcaStatus(bool success, bool plcaStatus)
189+
{
190+
if (!success)
191+
{
192+
Serial.println("PLCA status register read failed");
193+
return;
194+
}
195+
196+
if (plcaStatus) {
197+
Serial.println("PLCA Mode active");
198+
} else {
199+
Serial.println("CSMA/CD fallback");
200+
tc6_inst->enablePlca();
201+
}
202+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
static uint16_t const UDP_CLIENT_PORT = 8888;
2+
static uint16_t const UDP_SERVER_PORT = 8889;
3+
#define UDP_READ_COIL_PORT 1
4+
#define UDP_WRITE_COIL_PORT 2
5+
#define UDP_READ_DI_PORT 3
6+
#define UDP_READ_IR_PORT 4
7+
#define UDP_READ_HR_PORT 5
8+
#define UDP_WRITE_HR_PORT 6

0 commit comments

Comments
 (0)