1
1
/*
2
2
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.
4
4
5
5
This library is free software; you can redistribute it and/or
6
6
modify it under the terms of the GNU Lesser General Public
28
28
#include " ModbusT1SCommon.h"
29
29
#include < SPI.h>
30
30
31
-
32
-
33
31
#define RX_TIMEOUT 1000
34
- static void OnPlcaStatus_client ( bool success, bool plcaStatus);
32
+
35
33
using callback_f = void (*)(bool , bool );
36
34
class ModbusT1SClientClass : public ModbusClient {
37
35
public:
@@ -47,8 +45,6 @@ class ModbusT1SClientClass : public ModbusClient {
47
45
*
48
46
* Return 1 on success, 0 on failure
49
47
*/
50
- int begin (unsigned long baudrate, uint16_t config = SERIAL_8N1);
51
- int begin (RS485Class& rs485, unsigned long baudrate, uint16_t config = SERIAL_8N1);
52
48
int begin (int node_id);
53
49
/* *
54
50
* Sets the IP address of the Modbus server.
@@ -88,7 +84,7 @@ void setModbusId(uint16_t id);
88
84
* @param port The port number of the Modbus server. Default is 0.
89
85
* @return int The status of the coil (1 for ON, 0 for OFF) or -1 if an error occurs.
90
86
*/
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 );
92
88
93
89
/* *
94
90
* 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 =
102
98
* @param port The port number of the Modbus server. Default is 0.
103
99
* @return int The status of the coil (1 for ON, 0 for OFF) or -1 if an error occurs.
104
100
*/
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 );
106
102
107
103
/* *
108
104
* 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
116
112
* @param port The port number of the Modbus server. Default is 0.
117
113
* @return int Returns 1 if the write operation is successful, 0 otherwise.
118
114
*/
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 );
120
116
121
117
/* *
122
118
* 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
131
127
* @param port The port number of the Modbus server. Default is 0.
132
128
* @return int Returns 1 if the write operation is successful, 0 otherwise.
133
129
*/
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 );
135
131
136
132
/* *
137
133
* 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
144
140
* @param port The port number of the Modbus server. Default is 0.
145
141
* @return int The status of the discrete input (1 for ON, 0 for OFF) or -1 if an error occurs.
146
142
*/
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 );
148
144
149
145
/* *
150
146
* 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
158
154
* @param port The port number of the Modbus server. Default is 0.
159
155
* @return int The status of the discrete input (1 for ON, 0 for OFF) or -1 if an error occurs.
160
156
*/
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 );
162
158
163
159
/* *
164
160
* 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
171
167
* @param port The port number of the Modbus server. Default is 0.
172
168
* @return long The value of the input register or -1 if an error occurs.
173
169
*/
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 );
175
171
176
172
/* *
177
173
* 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
185
181
* @param port The port number of the Modbus server. Default is 0.
186
182
* @return long The value of the input register or -1 if an error occurs.
187
183
*/
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 );
189
185
190
186
/* *
191
187
* 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
199
195
* @param port The port number of the Modbus server. Default is 0.
200
196
* @return int Returns 1 if the write operation is successful, 0 otherwise.
201
197
*/
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 );
203
199
204
200
/* *
205
201
* 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
214
210
* @param port The port number of the Modbus server. Default is 0.
215
211
* @return int Returns 1 if the write operation is successful, 0 otherwise.
216
212
*/
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 );
218
214
219
215
/* *
220
216
* 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
227
223
* @param port The port number of the Modbus server. Default is 0.
228
224
* @return long The value of the holding register or -1 if an error occurs.
229
225
*/
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 );
231
227
232
228
/* *
233
229
* 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,
241
237
* @param port The port number of the Modbus server. Default is 0.
242
238
* @return long The value of the holding register or -1 if an error occurs.
243
239
*/
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);
246
242
void setRxTimeout (unsigned long timeout = RX_TIMEOUT);
247
243
void setT1SPort (int port = 8889 );
248
- void checkPLCAStatus ();
244
+ void update ();
249
245
void setCallback (callback_f cb = nullptr );
246
+ void enablePOE ();
247
+ void disablePOE ();
250
248
251
249
252
250
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);
254
253
void write (uint8_t * buf, int len, Arduino_10BASE_T1S_UDP * client);
255
254
int read (Arduino_10BASE_T1S_UDP * client);
256
255
bool checkPacket (int port, uint16_t id, uint16_t address);
257
256
258
257
private:
259
-
258
+ IPAddress _gateway = IPAddress( 0 , 0 , 0 , 0 );
260
259
unsigned long _rx_timeout = RX_TIMEOUT;
260
+ callback_f callback = nullptr ;
261
261
IPAddress _server_ip = IPAddress(0 , 0 , 0 , 0 );
262
- std::vector<uint8_t > udp_rx_buf;
263
262
uint16_t _server_port = 8889 ;
264
263
uint16_t _modbus_id = 0 ;
264
+ uint8_t udp_rx_buf[8 ] = {0 };
265
+
265
266
RS485Class* _rs485 = &RS485;
266
267
Arduino_10BASE_T1S_UDP * _client = nullptr ;
267
268
int _node_id = 1 ;
0 commit comments