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 9dcb66c

Browse files
committedNov 14, 2024
Modified the examples to show the new API
1 parent 0b698ce commit 9dcb66c

File tree

7 files changed

+146
-441
lines changed

7 files changed

+146
-441
lines changed
 

‎examples/RTU/ModbusRTUClientMD02TemperatureHumiditySensor/ModbusRTUClientMD02TemperatureHumiditySensor.ino

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void loop()
5656
if (ModbusRTUClient.available())
5757
{
5858
int16_t const temperature_raw = ModbusRTUClient.read();
59-
float const temperature_deg = temperature_raw / 100.f;
59+
float const temperature_deg = temperature_raw / 10.f;
6060
Serial.println(temperature_deg);
6161
}
6262

@@ -68,7 +68,7 @@ void loop()
6868
if (ModbusRTUClient.available())
6969
{
7070
int16_t const humidity_raw = ModbusRTUClient.read();
71-
float const humidity_per_cent = humidity_raw / 100.f;
71+
float const humidity_per_cent = humidity_raw / 10.f;
7272
Serial.println(humidity_per_cent);
7373
}
7474

‎examples/T1S/ModbusT1SClient/ModbusT1SClient.ino

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static uint16_t const UDP_CLIENT_PORT = 8888;
2121
void setup() {
2222
Serial.begin(115200);
2323

24-
ModbusT1SClient.setT1SClient(&udp_client);
24+
ModbusT1SClient.setT1SClient(udp_client);
2525
ModbusT1SClient.setT1SPort(UDP_CLIENT_PORT);
2626
ModbusT1SClient.setServerPort(UDP_SERVER_PORT);
2727
ModbusT1SClient.setModbusId(MODBUS_ID);
@@ -34,7 +34,7 @@ void setup() {
3434
}
3535

3636
void loop() {
37-
ModbusT1SClient.checkPLCAStatus();
37+
ModbusT1SClient.update();
3838

3939
int res = ModbusT1SClient.coilRead(0x00);
4040
if (res == -1) {

‎examples/T1S/ModbusT1SClientTemperatureHumiditySensor/ModbusT1SClientTemperatureHumiditySensor.ino

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Arduino_10BASE_T1S_UDP udp_client;
2121
void setup() {
2222
Serial.begin(115200);
2323

24-
ModbusT1SClient.setT1SClient(&udp_client);
24+
ModbusT1SClient.setT1SClient(udp_client);
2525
ModbusT1SClient.setT1SPort(UDP_CLIENT_PORT);
2626
ModbusT1SClient.setServerPort(UDP_SERVER_PORT);
2727
ModbusT1SClient.setCallback(OnPlcaStatus);
@@ -30,11 +30,12 @@ void setup() {
3030
Serial.println("Failed to start Modbus T1S Client!");
3131
while (1);
3232
}
33+
ModbusT1SClient.disablePOE();
3334
}
3435

3536
unsigned long start = 0;
3637
void loop() {
37-
ModbusT1SClient.checkPLCAStatus();
38+
ModbusT1SClient.update();
3839

3940
if ((millis() - start) > 1000)
4041
{

‎examples/T1S/ModbusT1SServer/ModbusT1SServer.ino

+5-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <ArduinoRS485.h>
1414
#include <ArduinoModbus.h>
1515

16+
RS485Class serial485(RS485_SERIAL, RS485_TX_PIN, RS485_DE_PIN, RS485_RE_PIN);
17+
1618
static uint8_t const T1S_PLCA_NODE_ID = 0;
1719
static uint16_t const UDP_SERVER_PORT = 8889;
1820

@@ -21,15 +23,15 @@ Arduino_10BASE_T1S_UDP udp_server;
2123
void setup() {
2224
Serial.begin(115200);
2325

24-
ModbusT1SServer.setT1SServer(&udp_server);
26+
ModbusT1SServer.setT1SServer(udp_server);
2527
ModbusT1SServer.setT1SPort(UDP_SERVER_PORT);
26-
ModbusT1SServer.setBadrate(9600);
2728
ModbusT1SServer.setCallback(OnPlcaStatus);
2829

29-
if (!ModbusT1SServer.begin(T1S_PLCA_NODE_ID)) {
30+
if (!ModbusT1SServer.begin(T1S_PLCA_NODE_ID, 9600, SERIAL_8N1, serial485)) {
3031
Serial.println("Failed to start Modbus T1S Server!");
3132
while (1);
3233
}
34+
ModbusT1SServer.disablePOE();
3335
}
3436

3537
void loop() {

‎examples/T1S/ModbusT1SServerTemperatureHumiditySensor/ModbusT1SServerTemperatureHumiditySensor.ino

-57
This file was deleted.

‎src/ModbusT1SClient.cpp

+110-352
Large diffs are not rendered by default.

‎src/ModbusT1SClient.h

+24-23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
This file is part of the ArduinoModbus library.
3-
Copyright (c) 2018 Arduino SA. All rights reserved.
3+
Copyright (c) 2024 Arduino SA. All rights reserved.
44
55
This library is free software; you can redistribute it and/or
66
modify it under the terms of the GNU Lesser General Public
@@ -28,10 +28,8 @@
2828
#include "ModbusT1SCommon.h"
2929
#include <SPI.h>
3030

31-
32-
3331
#define RX_TIMEOUT 1000
34-
static void OnPlcaStatus_client(bool success, bool plcaStatus);
32+
3533
using callback_f = void (*)(bool, bool);
3634
class ModbusT1SClientClass : public ModbusClient {
3735
public:
@@ -47,8 +45,6 @@ class ModbusT1SClientClass : public ModbusClient {
4745
*
4846
* Return 1 on success, 0 on failure
4947
*/
50-
int begin(unsigned long baudrate, uint16_t config = SERIAL_8N1);
51-
int begin(RS485Class& rs485, unsigned long baudrate, uint16_t config = SERIAL_8N1);
5248
int begin(int node_id);
5349
/**
5450
* Sets the IP address of the Modbus server.
@@ -88,7 +84,7 @@ void setModbusId(uint16_t id);
8884
* @param port The port number of the Modbus server. Default is 0.
8985
* @return int The status of the coil (1 for ON, 0 for OFF) or -1 if an error occurs.
9086
*/
91-
int coilRead(int address, Arduino_10BASE_T1S_UDP * client = nullptr, int port = -1);
87+
int coilRead(int address, Arduino_10BASE_T1S_UDP * client = nullptr);
9288

9389
/**
9490
* Reads the status of a coil from the Modbus server with a specified ID.
@@ -102,7 +98,7 @@ int coilRead(int address, Arduino_10BASE_T1S_UDP * client = nullptr, int port =
10298
* @param port The port number of the Modbus server. Default is 0.
10399
* @return int The status of the coil (1 for ON, 0 for OFF) or -1 if an error occurs.
104100
*/
105-
int coilRead(int id, int address, Arduino_10BASE_T1S_UDP * client = nullptr, int port = -1);
101+
int coilRead(int id, int address, Arduino_10BASE_T1S_UDP * client = nullptr);
106102

107103
/**
108104
* Writes a value to a coil on the Modbus server.
@@ -116,7 +112,7 @@ int coilRead(int id, int address, Arduino_10BASE_T1S_UDP * client = nullptr, int
116112
* @param port The port number of the Modbus server. Default is 0.
117113
* @return int Returns 1 if the write operation is successful, 0 otherwise.
118114
*/
119-
int coilWrite(int address, uint8_t value, Arduino_10BASE_T1S_UDP * client = nullptr, int port = -1);
115+
int coilWrite(int address, uint16_t value, Arduino_10BASE_T1S_UDP * client = nullptr);
120116

121117
/**
122118
* Writes a value to a coil on the Modbus server with a specified ID.
@@ -131,7 +127,7 @@ int coilWrite(int address, uint8_t value, Arduino_10BASE_T1S_UDP * client = null
131127
* @param port The port number of the Modbus server. Default is 0.
132128
* @return int Returns 1 if the write operation is successful, 0 otherwise.
133129
*/
134-
int coilWrite(int id, int address, uint8_t value, Arduino_10BASE_T1S_UDP * client = nullptr, int port = -1);
130+
int coilWrite(int id, int address, uint16_t value, Arduino_10BASE_T1S_UDP * client = nullptr);
135131

136132
/**
137133
* Reads the status of a discrete input from the Modbus server.
@@ -144,7 +140,7 @@ int coilWrite(int id, int address, uint8_t value, Arduino_10BASE_T1S_UDP * clien
144140
* @param port The port number of the Modbus server. Default is 0.
145141
* @return int The status of the discrete input (1 for ON, 0 for OFF) or -1 if an error occurs.
146142
*/
147-
int discreteInputRead(int address, Arduino_10BASE_T1S_UDP * client = nullptr, int port = -1);
143+
int discreteInputRead(int address, Arduino_10BASE_T1S_UDP * client = nullptr);
148144

149145
/**
150146
* Reads the status of a discrete input from the Modbus server with a specified ID.
@@ -158,7 +154,7 @@ int discreteInputRead(int address, Arduino_10BASE_T1S_UDP * client = nullptr, in
158154
* @param port The port number of the Modbus server. Default is 0.
159155
* @return int The status of the discrete input (1 for ON, 0 for OFF) or -1 if an error occurs.
160156
*/
161-
int discreteInputRead(int id, int address, Arduino_10BASE_T1S_UDP * client = nullptr, int port = -1);
157+
int discreteInputRead(int id, int address, Arduino_10BASE_T1S_UDP * client = nullptr);
162158

163159
/**
164160
* Reads the value of an input register from the Modbus server.
@@ -171,7 +167,7 @@ int discreteInputRead(int id, int address, Arduino_10BASE_T1S_UDP * client = nul
171167
* @param port The port number of the Modbus server. Default is 0.
172168
* @return long The value of the input register or -1 if an error occurs.
173169
*/
174-
long inputRegisterRead(int address, Arduino_10BASE_T1S_UDP * client = nullptr, int port = -1);
170+
long inputRegisterRead(int address, Arduino_10BASE_T1S_UDP * client = nullptr);
175171

176172
/**
177173
* Reads the value of an input register from the Modbus server with a specified ID.
@@ -185,7 +181,7 @@ long inputRegisterRead(int address, Arduino_10BASE_T1S_UDP * client = nullptr, i
185181
* @param port The port number of the Modbus server. Default is 0.
186182
* @return long The value of the input register or -1 if an error occurs.
187183
*/
188-
long inputRegisterRead(int id, int address, Arduino_10BASE_T1S_UDP * client = nullptr, int port = -1);
184+
long inputRegisterRead(int id, int address, Arduino_10BASE_T1S_UDP * client = nullptr);
189185

190186
/**
191187
* Writes a value to a holding register on the Modbus server.
@@ -199,7 +195,7 @@ long inputRegisterRead(int id, int address, Arduino_10BASE_T1S_UDP * client = nu
199195
* @param port The port number of the Modbus server. Default is 0.
200196
* @return int Returns 1 if the write operation is successful, 0 otherwise.
201197
*/
202-
int holdingRegisterWrite(int address, uint16_t value, Arduino_10BASE_T1S_UDP * client = nullptr, int port = -1);
198+
int holdingRegisterWrite(int address, uint16_t value, Arduino_10BASE_T1S_UDP * client = nullptr);
203199

204200
/**
205201
* Writes a value to a holding register on the Modbus server with a specified ID.
@@ -214,7 +210,7 @@ int holdingRegisterWrite(int address, uint16_t value, Arduino_10BASE_T1S_UDP * c
214210
* @param port The port number of the Modbus server. Default is 0.
215211
* @return int Returns 1 if the write operation is successful, 0 otherwise.
216212
*/
217-
int holdingRegisterWrite(int id, int address, uint16_t value, Arduino_10BASE_T1S_UDP * client = nullptr, int port = -1);
213+
int holdingRegisterWrite(int id, int address, uint16_t value, Arduino_10BASE_T1S_UDP * client = nullptr);
218214

219215
/**
220216
* Reads the value of a holding register from the Modbus server.
@@ -227,7 +223,7 @@ int holdingRegisterWrite(int id, int address, uint16_t value, Arduino_10BASE_T1S
227223
* @param port The port number of the Modbus server. Default is 0.
228224
* @return long The value of the holding register or -1 if an error occurs.
229225
*/
230-
long holdingRegisterRead(int address, Arduino_10BASE_T1S_UDP * client = nullptr, int port = -1);
226+
long holdingRegisterRead(int address, Arduino_10BASE_T1S_UDP * client = nullptr);
231227

232228
/**
233229
* Reads the value of a holding register from the Modbus server with a specified ID.
@@ -241,27 +237,32 @@ long holdingRegisterRead(int address, Arduino_10BASE_T1S_UDP * client = nullptr,
241237
* @param port The port number of the Modbus server. Default is 0.
242238
* @return long The value of the holding register or -1 if an error occurs.
243239
*/
244-
long holdingRegisterRead(int id, int address, Arduino_10BASE_T1S_UDP * client = nullptr, int port = -1);
245-
void setT1SClient(Arduino_10BASE_T1S_UDP * client = nullptr);
240+
long holdingRegisterRead(int id, int address, Arduino_10BASE_T1S_UDP * client = nullptr);
241+
void setT1SClient(Arduino_10BASE_T1S_UDP & client);
246242
void setRxTimeout(unsigned long timeout = RX_TIMEOUT);
247243
void setT1SPort(int port = 8889);
248-
void checkPLCAStatus();
244+
void update();
249245
void setCallback(callback_f cb = nullptr);
246+
void enablePOE();
247+
void disablePOE();
250248

251249

252250
private:
253-
callback_f callback = nullptr;
251+
long receive(int id, int address, Arduino_10BASE_T1S_UDP * client, int functionCode);
252+
int send(int id, int address, uint16_t value, Arduino_10BASE_T1S_UDP * client, int functionCode);
254253
void write(uint8_t * buf, int len, Arduino_10BASE_T1S_UDP * client);
255254
int read(Arduino_10BASE_T1S_UDP * client);
256255
bool checkPacket(int port, uint16_t id, uint16_t address);
257256

258257
private:
259-
258+
IPAddress _gateway = IPAddress(0, 0, 0, 0);
260259
unsigned long _rx_timeout = RX_TIMEOUT;
260+
callback_f callback = nullptr;
261261
IPAddress _server_ip = IPAddress(0, 0, 0, 0);
262-
std::vector<uint8_t> udp_rx_buf;
263262
uint16_t _server_port = 8889;
264263
uint16_t _modbus_id = 0;
264+
uint8_t udp_rx_buf[8] = {0};
265+
265266
RS485Class* _rs485 = &RS485;
266267
Arduino_10BASE_T1S_UDP * _client = nullptr;
267268
int _node_id = 1;

0 commit comments

Comments
 (0)
Please sign in to comment.