Skip to content

Commit e74a192

Browse files
committed
Added Docs
1 parent bfcc265 commit e74a192

File tree

4 files changed

+575
-297
lines changed

4 files changed

+575
-297
lines changed

src/ModbusT1SClient.cpp

+132-77
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ void ModbusT1SClientClass::setRxTimeout(unsigned long timeout)
174174
*
175175
* @param address The address of the coil to read.
176176
* @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.
178177
* @return int The status of the coil (1 for ON, 0 for OFF) or -1 if an error occurs.
179178
*/
180179
int ModbusT1SClientClass::coilRead(int address, Arduino_10BASE_T1S_UDP * client)
@@ -191,7 +190,6 @@ int ModbusT1SClientClass::coilRead(int address, Arduino_10BASE_T1S_UDP * client)
191190
* @param id The ID of the Modbus server.
192191
* @param address The address of the coil to read.
193192
* @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.
195193
* @return int The status of the coil (1 for ON, 0 for OFF) or -1 if an error occurs.
196194
*/
197195
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 *
208206
* @param address The address of the coil to write to.
209207
* @param value The value to write to the coil (1 for ON, 0 for OFF).
210208
* @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.
212209
* @return int 1 if the write operation is successful, -1 if an error occurs.
213210
*/
214211
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_
226223
* @param address The address of the coil to write to.
227224
* @param value The value to write to the coil (1 for ON, 0 for OFF).
228225
* @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.
230226
* @return int 1 if the write operation is successful, -1 if an error occurs.
231227
*/
232228
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
242238
*
243239
* @param address The address of the discrete input to read.
244240
* @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.
246241
* @return int The status of the discrete input (1 for ON, 0 for OFF) or -1 if an error occurs.
247242
*/
248243
int ModbusT1SClientClass::discreteInputRead(int address, Arduino_10BASE_T1S_UDP * client)
@@ -259,7 +254,6 @@ int ModbusT1SClientClass::discreteInputRead(int address, Arduino_10BASE_T1S_UDP
259254
* @param id The ID of the Modbus server.
260255
* @param address The address of the discrete input to read.
261256
* @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.
263257
* @return int The status of the discrete input (1 for ON, 0 for OFF) or -1 if an error occurs.
264258
*/
265259
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_
275269
*
276270
* @param address The address of the input register to read.
277271
* @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.
279272
* @return long The value of the input register or -1 if an error occurs.
280273
*/
281274
long ModbusT1SClientClass::inputRegisterRead(int address, Arduino_10BASE_T1S_UDP * client)
@@ -292,7 +285,6 @@ long ModbusT1SClientClass::inputRegisterRead(int address, Arduino_10BASE_T1S_UDP
292285
* @param id The ID of the Modbus server.
293286
* @param address The address of the input register to read.
294287
* @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.
296288
* @return long The value of the input register or -1 if an error occurs.
297289
*/
298290
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
308300
*
309301
* @param address The address of the holding register to read from.
310302
* @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.
312303
* @return Returns the value of the holding register on success, or -1 if the client is null.
313304
*/
314305
long ModbusT1SClientClass::holdingRegisterRead(int address, Arduino_10BASE_T1S_UDP * client)
@@ -326,7 +317,6 @@ long ModbusT1SClientClass::holdingRegisterRead(int address, Arduino_10BASE_T1S_U
326317
* @param address The address of the holding register to write to.
327318
* @param value The value to write to the holding register.
328319
* @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.
330320
* @return Returns 1 on success, or -1 if the client is null.
331321
*/
332322
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
361351
* @param address The address of the holding register to write to.
362352
* @param value The value to write to the holding register.
363353
* @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.
365354
* @return Returns 1 on success, or -1 if the client is null.
366355
*/
367356
int ModbusT1SClientClass::holdingRegisterWrite(int id, int address, uint16_t value, Arduino_10BASE_T1S_UDP * client)
368357
{
369358
return send(id, address, value, client, UDP_WRITE_HR_PORT);
370359
}
371360

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+
372492
long ModbusT1SClientClass::receive(int id, int address, Arduino_10BASE_T1S_UDP * client, int functionCode) {
373493

374494
long res = -1;
@@ -417,6 +537,7 @@ int ModbusT1SClientClass::send(int id, int address, uint16_t value, Arduino_10BA
417537
return 1;
418538
}
419539

540+
420541
void ModbusT1SClientClass::write(uint8_t * buf, int len, Arduino_10BASE_T1S_UDP * client)
421542
{
422543
client->beginPacket(_server_ip, _server_port);
@@ -434,71 +555,5 @@ int ModbusT1SClientClass::read(Arduino_10BASE_T1S_UDP * client)
434555
}
435556
return 0;
436557
}
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-
503558
ModbusT1SClientClass ModbusT1SClient;
504559
#endif

0 commit comments

Comments
 (0)