Skip to content

Commit cce9eca

Browse files
committed
added examples T1S
1 parent b036c50 commit cce9eca

File tree

4 files changed

+281
-0
lines changed

4 files changed

+281
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
Modbus T1S Client
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>
13+
#include <ArduinoModbus.h>
14+
15+
Arduino_10BASE_T1S_UDP udp_client;
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
20+
21+
void setup() {
22+
Serial.begin(115200);
23+
24+
ModbusT1SClient.setT1SClient(&udp_client);
25+
ModbusT1SClient.setT1SPort(UDP_CLIENT_PORT);
26+
ModbusT1SClient.setServerPort(UDP_SERVER_PORT);
27+
ModbusT1SClient.setModbusId(MODBUS_ID);
28+
ModbusT1SClient.setCallback(OnPlcaStatus);
29+
30+
if (!ModbusT1SClient.begin(T1S_PLCA_NODE_ID)) {
31+
Serial.println("Failed to start Modbus T1S Client!");
32+
while (1);
33+
}
34+
}
35+
36+
void loop() {
37+
ModbusT1SClient.checkPLCAStatus();
38+
39+
int res = ModbusT1SClient.coilRead(0x00);
40+
if (res == -1) {
41+
Serial.println("Failed to read coil! ");
42+
} else {
43+
Serial.print("Coil value: ");
44+
Serial.println(res);
45+
}
46+
47+
res = ModbusT1SClient.coilWrite(0, 0x01);
48+
if (res == -1) {
49+
Serial.println("Failed to write coil! ");
50+
} else {
51+
Serial.println("write done");
52+
}
53+
54+
res = ModbusT1SClient.inputRegisterRead(0x00);
55+
if (res == -1) {
56+
Serial.println("Failed to read Input Register! ");
57+
} else {
58+
Serial.print("Input Register value: ");
59+
Serial.println(res);
60+
}
61+
}
62+
63+
static void OnPlcaStatus(bool success, bool plcaStatus)
64+
{
65+
if (!success)
66+
{
67+
Serial.println("PLCA status register read failed");
68+
return;
69+
}
70+
71+
if (plcaStatus) {
72+
Serial.println("PLCA Mode active");
73+
} else {
74+
Serial.println("CSMA/CD fallback");
75+
tc6_inst->enablePlca();
76+
}
77+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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>
13+
#include <ArduinoModbus.h>
14+
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;
18+
19+
Arduino_10BASE_T1S_UDP udp_client;
20+
21+
void setup() {
22+
Serial.begin(115200);
23+
24+
ModbusT1SClient.setT1SClient(&udp_client);
25+
ModbusT1SClient.setT1SPort(UDP_CLIENT_PORT);
26+
ModbusT1SClient.setServerPort(UDP_SERVER_PORT);
27+
ModbusT1SClient.setCallback(OnPlcaStatus);
28+
29+
if (!ModbusT1SClient.begin(T1S_PLCA_NODE_ID)) {
30+
Serial.println("Failed to start Modbus T1S Client!");
31+
while (1);
32+
}
33+
}
34+
35+
unsigned long start = 0;
36+
void loop() {
37+
ModbusT1SClient.checkPLCAStatus();
38+
39+
if ((millis() - start) > 1000)
40+
{
41+
int res = ModbusT1SClient.inputRegisterRead(1, 0x01);
42+
if (res == -1) {
43+
Serial.println("Failed to read temperature! ");
44+
} else {
45+
int16_t const temperature_raw = res;
46+
float const temperature_deg = temperature_raw / 10.f;
47+
Serial.print("Temperature: ");
48+
Serial.println(temperature_deg);
49+
}
50+
51+
res = ModbusT1SClient.inputRegisterRead(1, 0x02);
52+
if (res == -1) {
53+
Serial.println("Failed to read humidity! ");
54+
} else {
55+
int16_t const humidity_raw = res;
56+
float const humidity_per_cent = humidity_raw / 10.f;
57+
Serial.print("Humidity: ");
58+
Serial.println(humidity_per_cent);
59+
}
60+
start = millis();
61+
}
62+
}
63+
64+
static void OnPlcaStatus(bool success, bool plcaStatus)
65+
{
66+
if (!success)
67+
{
68+
Serial.println("PLCA status register read failed");
69+
return;
70+
}
71+
72+
if (plcaStatus) {
73+
Serial.println("PLCA Mode active");
74+
} else {
75+
Serial.println("CSMA/CD fallback");
76+
tc6_inst->enablePlca();
77+
}
78+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Modbus T1S Server
3+
4+
5+
This sketch demonstrates how to receive commands from a Modbus T1S Client connected
6+
via T1S Single Pair Ethernet.
7+
8+
Circuit:
9+
- T1S shield
10+
- Uno WiFi R4
11+
*/
12+
13+
#include <ArduinoRS485.h>
14+
#include <ArduinoModbus.h>
15+
16+
static uint8_t const T1S_PLCA_NODE_ID = 0;
17+
static uint16_t const UDP_SERVER_PORT = 8889;
18+
19+
Arduino_10BASE_T1S_UDP udp_server;
20+
21+
void setup() {
22+
Serial.begin(115200);
23+
24+
ModbusT1SServer.setT1SServer(&udp_server);
25+
ModbusT1SServer.setT1SPort(UDP_SERVER_PORT);
26+
ModbusT1SServer.setBadrate(9600);
27+
ModbusT1SServer.setCallback(OnPlcaStatus);
28+
29+
if (!ModbusT1SServer.begin(T1S_PLCA_NODE_ID)) {
30+
Serial.println("Failed to start Modbus T1S Server!");
31+
while (1);
32+
}
33+
}
34+
35+
void loop() {
36+
ModbusT1SServer.update();
37+
}
38+
39+
static void OnPlcaStatus(bool success, bool plcaStatus)
40+
{
41+
if (!success)
42+
{
43+
Serial.println("PLCA status register read failed");
44+
return;
45+
}
46+
47+
if (plcaStatus) {
48+
Serial.println("PLCA Mode active");
49+
} else {
50+
Serial.println("CSMA/CD fallback");
51+
tc6_inst->enablePlca();
52+
}
53+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
Modbus T1S Server Temperature Humidity sensor
3+
4+
This sketch creates a Modbus T1S Server and demonstrates
5+
how to use read temperature and humidity values as sensed
6+
by the RTU Modbus capable MD02 sensor.
7+
8+
Circuit:
9+
- Arduino Uno Wifi R4
10+
- T1S shield
11+
- SPE ethernet connected to the client
12+
- ISO GND connected to GND of the Modbus RTU server
13+
- Y connected to A/Y of the Modbus RTU client
14+
- Z connected to B/Z of the Modbus RTU client
15+
- all the terminations placed on the hardware
16+
*/
17+
/*
18+
Modbus T1S Server Temperature Humidity sensor
19+
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.
23+
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+
*/
33+
34+
#include <ArduinoRS485.h>
35+
#include <ArduinoModbus.h>
36+
37+
static uint8_t const T1S_PLCA_NODE_ID = 0;
38+
static uint16_t const UDP_SERVER_PORT = 8889;
39+
40+
Arduino_10BASE_T1S_UDP udp_server;
41+
42+
void setup() {
43+
Serial.begin(115200);
44+
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!");
51+
while (1);
52+
}
53+
}
54+
55+
void loop() {
56+
ModbusT1SServer.update();
57+
}
58+
59+
static void OnPlcaStatus(bool success, bool plcaStatus)
60+
{
61+
if (!success)
62+
{
63+
Serial.println("PLCA status register read failed");
64+
return;
65+
}
66+
67+
if (plcaStatus) {
68+
Serial.println("PLCA Mode active");
69+
} else {
70+
Serial.println("CSMA/CD fallback");
71+
tc6_inst->enablePlca();
72+
}
73+
}

0 commit comments

Comments
 (0)