@@ -174,7 +174,6 @@ void ModbusT1SClientClass::setRxTimeout(unsigned long timeout)
174
174
*
175
175
* @param address The address of the coil to read.
176
176
* @param client A pointer to the Arduino_10BASE_T1S_UDP client used for communication.
177
- * @param port The port number to use for the communication.
178
177
* @return int The status of the coil (1 for ON, 0 for OFF) or -1 if an error occurs.
179
178
*/
180
179
int ModbusT1SClientClass::coilRead (int address, Arduino_10BASE_T1S_UDP * client)
@@ -191,7 +190,6 @@ int ModbusT1SClientClass::coilRead(int address, Arduino_10BASE_T1S_UDP * client)
191
190
* @param id The ID of the Modbus server.
192
191
* @param address The address of the coil to read.
193
192
* @param client A pointer to the Arduino_10BASE_T1S_UDP client used for communication.
194
- * @param port The port number to use for the communication.
195
193
* @return int The status of the coil (1 for ON, 0 for OFF) or -1 if an error occurs.
196
194
*/
197
195
int ModbusT1SClientClass::coilRead (int id, int address, Arduino_10BASE_T1S_UDP * client)
@@ -208,7 +206,6 @@ int ModbusT1SClientClass::coilRead(int id, int address, Arduino_10BASE_T1S_UDP *
208
206
* @param address The address of the coil to write to.
209
207
* @param value The value to write to the coil (1 for ON, 0 for OFF).
210
208
* @param client A pointer to the Arduino_10BASE_T1S_UDP client used for communication.
211
- * @param port The port number to use for the communication.
212
209
* @return int 1 if the write operation is successful, -1 if an error occurs.
213
210
*/
214
211
int ModbusT1SClientClass::coilWrite (int address, uint16_t value, Arduino_10BASE_T1S_UDP * client)
@@ -226,7 +223,6 @@ int ModbusT1SClientClass::coilWrite(int address, uint16_t value, Arduino_10BASE_
226
223
* @param address The address of the coil to write to.
227
224
* @param value The value to write to the coil (1 for ON, 0 for OFF).
228
225
* @param client A pointer to the Arduino_10BASE_T1S_UDP client used for communication.
229
- * @param port The port number to use for the communication.
230
226
* @return int 1 if the write operation is successful, -1 if an error occurs.
231
227
*/
232
228
int ModbusT1SClientClass::coilWrite (int id, int address, uint16_t value, Arduino_10BASE_T1S_UDP * client)
@@ -242,7 +238,6 @@ int ModbusT1SClientClass::coilWrite(int id, int address, uint16_t value, Arduino
242
238
*
243
239
* @param address The address of the discrete input to read.
244
240
* @param client A pointer to the Arduino_10BASE_T1S_UDP client used for communication.
245
- * @param port The port number to use for the communication.
246
241
* @return int The status of the discrete input (1 for ON, 0 for OFF) or -1 if an error occurs.
247
242
*/
248
243
int ModbusT1SClientClass::discreteInputRead (int address, Arduino_10BASE_T1S_UDP * client)
@@ -259,7 +254,6 @@ int ModbusT1SClientClass::discreteInputRead(int address, Arduino_10BASE_T1S_UDP
259
254
* @param id The ID of the Modbus server.
260
255
* @param address The address of the discrete input to read.
261
256
* @param client A pointer to the Arduino_10BASE_T1S_UDP client used for communication.
262
- * @param port The port number to use for the communication.
263
257
* @return int The status of the discrete input (1 for ON, 0 for OFF) or -1 if an error occurs.
264
258
*/
265
259
int ModbusT1SClientClass::discreteInputRead (int id, int address, Arduino_10BASE_T1S_UDP * client)
@@ -275,7 +269,6 @@ int ModbusT1SClientClass::discreteInputRead(int id, int address, Arduino_10BASE_
275
269
*
276
270
* @param address The address of the input register to read.
277
271
* @param client A pointer to the Arduino_10BASE_T1S_UDP client used for communication.
278
- * @param port The port number to use for the communication.
279
272
* @return long The value of the input register or -1 if an error occurs.
280
273
*/
281
274
long ModbusT1SClientClass::inputRegisterRead (int address, Arduino_10BASE_T1S_UDP * client)
@@ -292,7 +285,6 @@ long ModbusT1SClientClass::inputRegisterRead(int address, Arduino_10BASE_T1S_UDP
292
285
* @param id The ID of the Modbus server.
293
286
* @param address The address of the input register to read.
294
287
* @param client A pointer to the Arduino_10BASE_T1S_UDP client used for communication.
295
- * @param port The port number to use for the communication.
296
288
* @return long The value of the input register or -1 if an error occurs.
297
289
*/
298
290
long ModbusT1SClientClass::inputRegisterRead (int id, int address, Arduino_10BASE_T1S_UDP * client)
@@ -308,7 +300,6 @@ long ModbusT1SClientClass::inputRegisterRead(int id, int address, Arduino_10BASE
308
300
*
309
301
* @param address The address of the holding register to read from.
310
302
* @param client A pointer to an Arduino_10BASE_T1S_UDP client used for communication.
311
- * @param port The port number to use for the communication.
312
303
* @return Returns the value of the holding register on success, or -1 if the client is null.
313
304
*/
314
305
long ModbusT1SClientClass::holdingRegisterRead (int address, Arduino_10BASE_T1S_UDP * client)
@@ -326,7 +317,6 @@ long ModbusT1SClientClass::holdingRegisterRead(int address, Arduino_10BASE_T1S_U
326
317
* @param address The address of the holding register to write to.
327
318
* @param value The value to write to the holding register.
328
319
* @param client A pointer to an Arduino_10BASE_T1S_UDP client used for communication.
329
- * @param port The port number to use for the communication.
330
320
* @return Returns 1 on success, or -1 if the client is null.
331
321
*/
332
322
long ModbusT1SClientClass::holdingRegisterRead (int id, int address, Arduino_10BASE_T1S_UDP * client)
@@ -361,14 +351,144 @@ int ModbusT1SClientClass::holdingRegisterWrite(int address, uint16_t value, Ardu
361
351
* @param address The address of the holding register to write to.
362
352
* @param value The value to write to the holding register.
363
353
* @param client A pointer to an Arduino_10BASE_T1S_UDP client used for communication.
364
- * @param port The port number to use for the communication.
365
354
* @return Returns 1 on success, or -1 if the client is null.
366
355
*/
367
356
int ModbusT1SClientClass::holdingRegisterWrite (int id, int address, uint16_t value, Arduino_10BASE_T1S_UDP * client)
368
357
{
369
358
return send (id, address, value, client, UDP_WRITE_HR_PORT);
370
359
}
371
360
361
+ /* *
362
+ * Checks if the received packet matches the specified port, id, and address.
363
+ *
364
+ * This function compares the received packet's port, id, and address with the provided
365
+ * values to determine if they match.
366
+ *
367
+ * @param port The port number to check against the received packet.
368
+ * @param id The id to check against the received packet.
369
+ * @param address The address to check against the received packet.
370
+ * @return true if the received packet matches the specified port, id, and address; false otherwise.
371
+ */
372
+ bool ModbusT1SClientClass::checkPacket (int port, uint16_t id, uint16_t address)
373
+ {
374
+ int port_rec = udp_rx_buf[0 ] << 8 | udp_rx_buf[1 ];
375
+ uint16_t id_rcv = udp_rx_buf[2 ] << 8 | udp_rx_buf[3 ];
376
+ uint16_t add_rcv = udp_rx_buf[4 ] << 8 | udp_rx_buf[5 ];
377
+ if (port_rec == port && add_rcv == address && id_rcv == id) {
378
+ return true ;
379
+ }
380
+ return false ;
381
+ }
382
+
383
+ /* *
384
+ * Sets the T1S client for the Modbus communication.
385
+ *
386
+ * This function assigns a 10BASE-T1S UDP client to be used for Modbus communication.
387
+ *
388
+ * @param client A reference to an Arduino_10BASE_T1S_UDP object that represents the T1S client.
389
+ */
390
+ void ModbusT1SClientClass::setT1SClient (Arduino_10BASE_T1S_UDP & client)
391
+ {
392
+ _client = &client;
393
+ }
394
+
395
+ /* *
396
+ * Sets the T1S port for the Modbus communication.
397
+ *
398
+ * This function sets the port number to be used for the T1S communication.
399
+ *
400
+ * @param port The port number to use for T1S communication.
401
+ */
402
+ void ModbusT1SClientClass::setT1SPort (int port)
403
+ {
404
+ udp_port = port;
405
+ }
406
+
407
+ /* *
408
+ * Polls the Modbus client for incoming data.
409
+ *
410
+ * This function polls the Modbus client for incoming data.
411
+ */
412
+ void ModbusT1SClientClass::update ()
413
+ {
414
+ tc6_inst->service ();
415
+
416
+ static unsigned long prev_beacon_check = 0 ;
417
+ static unsigned long prev_udp_packet_sent = 0 ;
418
+
419
+ auto const now = millis ();
420
+
421
+ if ((now - prev_beacon_check) > 1000 )
422
+ {
423
+ prev_beacon_check = now;
424
+ tc6_inst->getPlcaStatus (callback);
425
+ }
426
+ }
427
+
428
+ /* *
429
+ * Sets the callback function to be used by the ModbusT1SClient.
430
+ *
431
+ * This function sets the callback function to be used by the ModbusT1SClient.
432
+ *
433
+ * @param cb The callback function to use.
434
+ */
435
+ void ModbusT1SClientClass::setCallback (callback_f cb) {
436
+ if (cb != nullptr ) {
437
+ callback = cb;
438
+ }
439
+ }
440
+
441
+ /* *
442
+ * Default callback function for PLCA status check.
443
+ *
444
+ * This function is the default callback function for PLCA status check.
445
+ *
446
+ * @param success The success status of the PLCA status check.
447
+ * @param plcaStatus The PLCA status.
448
+ */
449
+ static void default_OnPlcaStatus (bool success, bool plcaStatus)
450
+ {
451
+ if (!success)
452
+ {
453
+ return ;
454
+ }
455
+
456
+ if (!plcaStatus) {
457
+ tc6_inst->enablePlca ();
458
+ }
459
+ }
460
+
461
+ /* *
462
+ * Sets the gateway IP address for the Modbus client.
463
+ *
464
+ * This function sets the gateway IP address for the Modbus client.
465
+ *
466
+ * @param gateway The gateway IP address.
467
+ */
468
+ void ModbusT1SClientClass::setGateway (IPAddress gateway) {
469
+ _gateway = gateway;
470
+ }
471
+
472
+ /* *
473
+ * Enables Power Over Ethernet (POE).
474
+ *
475
+ * This function enables Power Over Ethernet (POE) on the T1S client.
476
+ */
477
+ void ModbusT1SClientClass::enablePOE () {
478
+ tc6_inst->digitalWrite (TC6::DIO::A0, true );
479
+ tc6_inst->digitalWrite (TC6::DIO::A1, true );
480
+ }
481
+
482
+ /* *
483
+ * Disables Power Over Ethernet (POE).
484
+ *
485
+ * This function disables Power Over Ethernet (POE) on the T1S client.
486
+ */
487
+ void ModbusT1SClientClass::disablePOE () {
488
+ tc6_inst->digitalWrite (TC6::DIO::A0, false );
489
+ tc6_inst->digitalWrite (TC6::DIO::A1, true );
490
+ }
491
+
372
492
long ModbusT1SClientClass::receive (int id, int address, Arduino_10BASE_T1S_UDP * client, int functionCode) {
373
493
374
494
long res = -1 ;
@@ -417,6 +537,7 @@ int ModbusT1SClientClass::send(int id, int address, uint16_t value, Arduino_10BA
417
537
return 1 ;
418
538
}
419
539
540
+
420
541
void ModbusT1SClientClass::write (uint8_t * buf, int len, Arduino_10BASE_T1S_UDP * client)
421
542
{
422
543
client->beginPacket (_server_ip, _server_port);
@@ -434,71 +555,5 @@ int ModbusT1SClientClass::read(Arduino_10BASE_T1S_UDP * client)
434
555
}
435
556
return 0 ;
436
557
}
437
-
438
- bool ModbusT1SClientClass::checkPacket (int port, uint16_t id, uint16_t address)
439
- {
440
- int port_rec = udp_rx_buf[0 ] << 8 | udp_rx_buf[1 ];
441
- uint16_t id_rcv = udp_rx_buf[2 ] << 8 | udp_rx_buf[3 ];
442
- uint16_t add_rcv = udp_rx_buf[4 ] << 8 | udp_rx_buf[5 ];
443
- if (port_rec == port && add_rcv == address && id_rcv == id) {
444
- return true ;
445
- }
446
- return false ;
447
- }
448
-
449
- void ModbusT1SClientClass::setT1SClient (Arduino_10BASE_T1S_UDP & client)
450
- {
451
- _client = &client;
452
- }
453
-
454
- void ModbusT1SClientClass::setT1SPort (int port)
455
- {
456
- udp_port = port;
457
- }
458
-
459
- void ModbusT1SClientClass::update ()
460
- {
461
- tc6_inst->service ();
462
-
463
- static unsigned long prev_beacon_check = 0 ;
464
- static unsigned long prev_udp_packet_sent = 0 ;
465
-
466
- auto const now = millis ();
467
-
468
- if ((now - prev_beacon_check) > 1000 )
469
- {
470
- prev_beacon_check = now;
471
- // tc6_inst->getPlcaStatus(callback);
472
- }
473
- }
474
-
475
- void ModbusT1SClientClass::setCallback (callback_f cb) {
476
- if (cb != nullptr ) {
477
- callback = cb;
478
- }
479
- }
480
-
481
- static void default_OnPlcaStatus (bool success, bool plcaStatus)
482
- {
483
- if (!success)
484
- {
485
- return ;
486
- }
487
-
488
- if (!plcaStatus) {
489
- tc6_inst->enablePlca ();
490
- }
491
- }
492
-
493
- void ModbusT1SClientClass::enablePOE () {
494
- tc6_inst->digitalWrite (TC6::DIO::A0, true );
495
- tc6_inst->digitalWrite (TC6::DIO::A1, true );
496
- }
497
-
498
- void ModbusT1SClientClass::disablePOE () {
499
- tc6_inst->digitalWrite (TC6::DIO::A0, false );
500
- tc6_inst->digitalWrite (TC6::DIO::A1, true );
501
- }
502
-
503
558
ModbusT1SClientClass ModbusT1SClient;
504
559
#endif
0 commit comments