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 b98bbea

Browse files
committedNov 15, 2024·
Added Docs
1 parent bfcc265 commit b98bbea

File tree

4 files changed

+577
-299
lines changed

4 files changed

+577
-299
lines changed
 

‎src/ModbusT1SClient.cpp

Lines changed: 133 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ int ModbusT1SClientClass::begin(int node_id)
8282
{
8383
return 0;
8484
}
85-
//RRR ADD set IPs
85+
8686
if(_gateway == IPAddress(0, 0, 0, 0)) {
8787
_gateway = IPAddress(192, 168, 42, 100);
8888
}
@@ -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

‎src/ModbusT1SClient.h

Lines changed: 270 additions & 199 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,25 @@
3131
using callback_f = void (*)(bool, bool);
3232
class ModbusT1SClientClass : public ModbusClient {
3333
public:
34+
/**
35+
* Default constructor for ModbusT1SClientClass.
36+
*
37+
* Initializes the Modbus client with a default timeout of 1000 milliseconds.
38+
*/
3439
ModbusT1SClientClass();
40+
41+
/**
42+
* Constructor for ModbusT1SClientClass with RS485 support.
43+
*
44+
* Initializes the Modbus client with a default timeout of 1000 milliseconds and sets up RS485 communication.
45+
*
46+
* @param rs485 Reference to an RS485Class object for RS485 communication.
47+
*/
3548
ModbusT1SClientClass(RS485Class& rs485);
49+
50+
/**
51+
* Destructor for ModbusT1SClientClass.
52+
*/
3653
virtual ~ModbusT1SClientClass();
3754

3855
/**
@@ -44,205 +61,259 @@ class ModbusT1SClientClass : public ModbusClient {
4461
* Return 1 on success, 0 on failure
4562
*/
4663
int begin(int node_id);
47-
/**
48-
* Sets the IP address of the Modbus server.
49-
*
50-
* This function sets the IP address of the Modbus server that the client will communicate with.
51-
*
52-
* @param server_ip The IP address of the Modbus server. Default is IPAddress(0, 0, 0, 0).
53-
*/
54-
void setServerIp(IPAddress server_ip = IPAddress(0, 0, 0, 0));
55-
56-
/**
57-
* Sets the port number of the Modbus server.
58-
*
59-
* This function sets the port number of the Modbus server that the client will communicate with.
60-
*
61-
* @param server_port The port number of the Modbus server. Default is 8889.
62-
*/
63-
void setServerPort(uint16_t server_port = 8889);
64-
65-
/**
66-
* Sets the Modbus ID of the client.
67-
*
68-
* This function sets the Modbus ID that the client will use for communication.
69-
*
70-
* @param id The Modbus ID.
71-
*/
72-
void setModbusId(uint16_t id);
73-
74-
/**
75-
* Reads the status of a coil from the Modbus server.
76-
*
77-
* This function sends a request to read the status of a coil at the specified address
78-
* from the Modbus server using the provided UDP client and port.
79-
*
80-
* @param address The address of the coil to read.
81-
* @param client A pointer to the Arduino_10BASE_T1S_UDP client used for communication. Default is nullptr.
82-
* @param port The port number of the Modbus server. Default is 0.
83-
* @return int The status of the coil (1 for ON, 0 for OFF) or -1 if an error occurs.
84-
*/
85-
int coilRead(int address, Arduino_10BASE_T1S_UDP * client = nullptr);
86-
87-
/**
88-
* Reads the status of a coil from the Modbus server with a specified ID.
89-
*
90-
* This function sends a request to read the status of a coil at the specified address
91-
* from the Modbus server with the given ID using the provided UDP client and port.
92-
*
93-
* @param id The ID of the Modbus server.
94-
* @param address The address of the coil to read.
95-
* @param client A pointer to the Arduino_10BASE_T1S_UDP client used for communication. Default is nullptr.
96-
* @param port The port number of the Modbus server. Default is 0.
97-
* @return int The status of the coil (1 for ON, 0 for OFF) or -1 if an error occurs.
98-
*/
99-
int coilRead(int id, int address, Arduino_10BASE_T1S_UDP * client = nullptr);
100-
101-
/**
102-
* Writes a value to a coil on the Modbus server.
103-
*
104-
* This function sends a request to write a value to a coil at the specified address
105-
* on the Modbus server using the provided UDP client and port.
106-
*
107-
* @param address The address of the coil to write to.
108-
* @param value The value to write to the coil (1 for ON, 0 for OFF).
109-
* @param client A pointer to the Arduino_10BASE_T1S_UDP client used for communication. Default is nullptr.
110-
* @param port The port number of the Modbus server. Default is 0.
111-
* @return int Returns 1 if the write operation is successful, 0 otherwise.
112-
*/
113-
int coilWrite(int address, uint16_t value, Arduino_10BASE_T1S_UDP * client = nullptr);
114-
115-
/**
116-
* Writes a value to a coil on the Modbus server with a specified ID.
117-
*
118-
* This function sends a request to write a value to a coil at the specified address
119-
* on the Modbus server with the given ID using the provided UDP client and port.
120-
*
121-
* @param id The ID of the Modbus server.
122-
* @param address The address of the coil to write to.
123-
* @param value The value to write to the coil (1 for ON, 0 for OFF).
124-
* @param client A pointer to the Arduino_10BASE_T1S_UDP client used for communication. Default is nullptr.
125-
* @param port The port number of the Modbus server. Default is 0.
126-
* @return int Returns 1 if the write operation is successful, 0 otherwise.
127-
*/
128-
int coilWrite(int id, int address, uint16_t value, Arduino_10BASE_T1S_UDP * client = nullptr);
129-
130-
/**
131-
* Reads the status of a discrete input from the Modbus server.
132-
*
133-
* This function sends a request to read the status of a discrete input at the specified address
134-
* from the Modbus server using the provided UDP client and port.
135-
*
136-
* @param address The address of the discrete input to read.
137-
* @param client A pointer to the Arduino_10BASE_T1S_UDP client used for communication. Default is nullptr.
138-
* @param port The port number of the Modbus server. Default is 0.
139-
* @return int The status of the discrete input (1 for ON, 0 for OFF) or -1 if an error occurs.
140-
*/
141-
int discreteInputRead(int address, Arduino_10BASE_T1S_UDP * client = nullptr);
142-
143-
/**
144-
* Reads the status of a discrete input from the Modbus server with a specified ID.
145-
*
146-
* This function sends a request to read the status of a discrete input at the specified address
147-
* from the Modbus server with the given ID using the provided UDP client and port.
148-
*
149-
* @param id The ID of the Modbus server.
150-
* @param address The address of the discrete input to read.
151-
* @param client A pointer to the Arduino_10BASE_T1S_UDP client used for communication. Default is nullptr.
152-
* @param port The port number of the Modbus server. Default is 0.
153-
* @return int The status of the discrete input (1 for ON, 0 for OFF) or -1 if an error occurs.
154-
*/
155-
int discreteInputRead(int id, int address, Arduino_10BASE_T1S_UDP * client = nullptr);
156-
157-
/**
158-
* Reads the value of an input register from the Modbus server.
159-
*
160-
* This function sends a request to read the value of an input register at the specified address
161-
* from the Modbus server using the provided UDP client and port.
162-
*
163-
* @param address The address of the input register to read.
164-
* @param client A pointer to the Arduino_10BASE_T1S_UDP client used for communication. Default is nullptr.
165-
* @param port The port number of the Modbus server. Default is 0.
166-
* @return long The value of the input register or -1 if an error occurs.
167-
*/
168-
long inputRegisterRead(int address, Arduino_10BASE_T1S_UDP * client = nullptr);
169-
170-
/**
171-
* Reads the value of an input register from the Modbus server with a specified ID.
172-
*
173-
* This function sends a request to read the value of an input register at the specified address
174-
* from the Modbus server with the given ID using the provided UDP client and port.
175-
*
176-
* @param id The ID of the Modbus server.
177-
* @param address The address of the input register to read.
178-
* @param client A pointer to the Arduino_10BASE_T1S_UDP client used for communication. Default is nullptr.
179-
* @param port The port number of the Modbus server. Default is 0.
180-
* @return long The value of the input register or -1 if an error occurs.
181-
*/
182-
long inputRegisterRead(int id, int address, Arduino_10BASE_T1S_UDP * client = nullptr);
183-
184-
/**
185-
* Writes a value to a holding register on the Modbus server.
186-
*
187-
* This function sends a request to write a value to a holding register at the specified address
188-
* on the Modbus server using the provided UDP client and port.
189-
*
190-
* @param address The address of the holding register to write to.
191-
* @param value The value to write to the holding register.
192-
* @param client A pointer to the Arduino_10BASE_T1S_UDP client used for communication. Default is nullptr.
193-
* @param port The port number of the Modbus server. Default is 0.
194-
* @return int Returns 1 if the write operation is successful, 0 otherwise.
195-
*/
196-
int holdingRegisterWrite(int address, uint16_t value, Arduino_10BASE_T1S_UDP * client = nullptr);
197-
198-
/**
199-
* Writes a value to a holding register on the Modbus server with a specified ID.
200-
*
201-
* This function sends a request to write a value to a holding register at the specified address
202-
* on the Modbus server with the given ID using the provided UDP client and port.
203-
*
204-
* @param id The ID of the Modbus server.
205-
* @param address The address of the holding register to write to.
206-
* @param value The value to write to the holding register.
207-
* @param client A pointer to the Arduino_10BASE_T1S_UDP client used for communication. Default is nullptr.
208-
* @param port The port number of the Modbus server. Default is 0.
209-
* @return int Returns 1 if the write operation is successful, 0 otherwise.
210-
*/
211-
int holdingRegisterWrite(int id, int address, uint16_t value, Arduino_10BASE_T1S_UDP * client = nullptr);
212-
213-
/**
214-
* Reads the value of a holding register from the Modbus server.
215-
*
216-
* This function sends a request to read the value of a holding register at the specified address
217-
* from the Modbus server using the provided UDP client and port.
218-
*
219-
* @param address The address of the holding register to read.
220-
* @param client A pointer to the Arduino_10BASE_T1S_UDP client used for communication. Default is nullptr.
221-
* @param port The port number of the Modbus server. Default is 0.
222-
* @return long The value of the holding register or -1 if an error occurs.
223-
*/
224-
long holdingRegisterRead(int address, Arduino_10BASE_T1S_UDP * client = nullptr);
225-
226-
/**
227-
* Reads the value of a holding register from the Modbus server with a specified ID.
228-
*
229-
* This function sends a request to read the value of a holding register at the specified address
230-
* from the Modbus server with the given ID using the provided UDP client and port.
231-
*
232-
* @param id The ID of the Modbus server.
233-
* @param address The address of the holding register to read.
234-
* @param client A pointer to the Arduino_10BASE_T1S_UDP client used for communication. Default is nullptr.
235-
* @param port The port number of the Modbus server. Default is 0.
236-
* @return long The value of the holding register or -1 if an error occurs.
237-
*/
238-
long holdingRegisterRead(int id, int address, Arduino_10BASE_T1S_UDP * client = nullptr);
239-
void setT1SClient(Arduino_10BASE_T1S_UDP & client);
240-
void setRxTimeout(unsigned long timeout = RX_TIMEOUT);
241-
void setT1SPort(int port = 8889);
242-
void update();
243-
void setCallback(callback_f cb = nullptr);
244-
void enablePOE();
245-
void disablePOE();
64+
65+
/**
66+
* Sets the IP address of the Modbus server.
67+
*
68+
* This function sets the IP address of the Modbus server that the client will communicate with.
69+
*
70+
* @param server_ip The IP address of the Modbus server. Default is IPAddress(0, 0, 0, 0).
71+
*/
72+
void setServerIp(IPAddress server_ip = IPAddress(0, 0, 0, 0));
73+
74+
/**
75+
* Sets the port number of the Modbus server.
76+
*
77+
* This function sets the port number of the Modbus server that the client will communicate with.
78+
*
79+
* @param server_port The port number of the Modbus server. Default is 8889.
80+
*/
81+
void setServerPort(uint16_t server_port = 8889);
82+
83+
/**
84+
* Sets the Modbus ID of the client.
85+
*
86+
* This function sets the Modbus ID that the client will use for communication.
87+
*
88+
* @param id The Modbus ID.
89+
*/
90+
void setModbusId(uint16_t id);
91+
92+
/**
93+
* Reads the status of a coil from the Modbus server.
94+
*
95+
* This function sends a request to read the status of a coil at the specified address
96+
* from the Modbus server using the provided UDP client and port.
97+
*
98+
* @param address The address of the coil to read.
99+
* @param client A pointer to the Arduino_10BASE_T1S_UDP client used for communication. Default is nullptr.
100+
* @return int The status of the coil (1 for ON, 0 for OFF) or -1 if an error occurs.
101+
*/
102+
int coilRead(int address, Arduino_10BASE_T1S_UDP * client = nullptr);
103+
104+
/**
105+
* Reads the status of a coil from the Modbus server with a specified ID.
106+
*
107+
* This function sends a request to read the status of a coil at the specified address
108+
* from the Modbus server with the given ID using the provided UDP client and port.
109+
*
110+
* @param id The ID of the Modbus server.
111+
* @param address The address of the coil to read.
112+
* @param client A pointer to the Arduino_10BASE_T1S_UDP client used for communication. Default is nullptr.
113+
* @return int The status of the coil (1 for ON, 0 for OFF) or -1 if an error occurs.
114+
*/
115+
int coilRead(int id, int address, Arduino_10BASE_T1S_UDP * client = nullptr);
116+
117+
/**
118+
* Writes a value to a coil on the Modbus server.
119+
*
120+
* This function sends a request to write a value to a coil at the specified address
121+
* on the Modbus server using the provided UDP client and port.
122+
*
123+
* @param address The address of the coil to write to.
124+
* @param value The value to write to the coil (1 for ON, 0 for OFF).
125+
* @param client A pointer to the Arduino_10BASE_T1S_UDP client used for communication. Default is nullptr.
126+
* @return int Returns 1 if the write operation is successful, 0 otherwise.
127+
*/
128+
int coilWrite(int address, uint16_t value, Arduino_10BASE_T1S_UDP * client = nullptr);
129+
130+
/**
131+
* Writes a value to a coil on the Modbus server with a specified ID.
132+
*
133+
* This function sends a request to write a value to a coil at the specified address
134+
* on the Modbus server with the given ID using the provided UDP client and port.
135+
*
136+
* @param id The ID of the Modbus server.
137+
* @param address The address of the coil to write to.
138+
* @param value The value to write to the coil (1 for ON, 0 for OFF).
139+
* @param client A pointer to the Arduino_10BASE_T1S_UDP client used for communication. Default is nullptr.
140+
* @return int Returns 1 if the write operation is successful, 0 otherwise.
141+
*/
142+
int coilWrite(int id, int address, uint16_t value, Arduino_10BASE_T1S_UDP * client = nullptr);
143+
144+
/**
145+
* Reads the status of a discrete input from the Modbus server.
146+
*
147+
* This function sends a request to read the status of a discrete input at the specified address
148+
* from the Modbus server using the provided UDP client and port.
149+
*
150+
* @param address The address of the discrete input to read.
151+
* @param client A pointer to the Arduino_10BASE_T1S_UDP client used for communication. Default is nullptr.
152+
* @return int The status of the discrete input (1 for ON, 0 for OFF) or -1 if an error occurs.
153+
*/
154+
int discreteInputRead(int address, Arduino_10BASE_T1S_UDP * client = nullptr);
155+
156+
/**
157+
* Reads the status of a discrete input from the Modbus server with a specified ID.
158+
*
159+
* This function sends a request to read the status of a discrete input at the specified address
160+
* from the Modbus server with the given ID using the provided UDP client and port.
161+
*
162+
* @param id The ID of the Modbus server.
163+
* @param address The address of the discrete input to read.
164+
* @param client A pointer to the Arduino_10BASE_T1S_UDP client used for communication. Default is nullptr.
165+
* @return int The status of the discrete input (1 for ON, 0 for OFF) or -1 if an error occurs.
166+
*/
167+
int discreteInputRead(int id, int address, Arduino_10BASE_T1S_UDP * client = nullptr);
168+
169+
/**
170+
* Reads the value of an input register from the Modbus server.
171+
*
172+
* This function sends a request to read the value of an input register at the specified address
173+
* from the Modbus server using the provided UDP client and port.
174+
*
175+
* @param address The address of the input register to read.
176+
* @param client A pointer to the Arduino_10BASE_T1S_UDP client used for communication. Default is nullptr.
177+
* @return long The value of the input register or -1 if an error occurs.
178+
*/
179+
long inputRegisterRead(int address, Arduino_10BASE_T1S_UDP * client = nullptr);
180+
181+
/**
182+
* Reads the value of an input register from the Modbus server with a specified ID.
183+
*
184+
* This function sends a request to read the value of an input register at the specified address
185+
* from the Modbus server with the given ID using the provided UDP client and port.
186+
*
187+
* @param id The ID of the Modbus server.
188+
* @param address The address of the input register to read.
189+
* @param client A pointer to the Arduino_10BASE_T1S_UDP client used for communication. Default is nullptr.
190+
* @return long The value of the input register or -1 if an error occurs.
191+
*/
192+
long inputRegisterRead(int id, int address, Arduino_10BASE_T1S_UDP * client = nullptr);
193+
194+
/**
195+
* Writes a value to a holding register on the Modbus server.
196+
*
197+
* This function sends a request to write a value to a holding register at the specified address
198+
* on the Modbus server using the provided UDP client and port.
199+
*
200+
* @param address The address of the holding register to write to.
201+
* @param value The value to write to the holding register.
202+
* @param client A pointer to the Arduino_10BASE_T1S_UDP client used for communication. Default is nullptr.
203+
* @return int Returns 1 if the write operation is successful, 0 otherwise.
204+
*/
205+
int holdingRegisterWrite(int address, uint16_t value, Arduino_10BASE_T1S_UDP * client = nullptr);
206+
207+
/**
208+
* Writes a value to a holding register on the Modbus server with a specified ID.
209+
*
210+
* This function sends a request to write a value to a holding register at the specified address
211+
* on the Modbus server with the given ID using the provided UDP client and port.
212+
*
213+
* @param id The ID of the Modbus server.
214+
* @param address The address of the holding register to write to.
215+
* @param value The value to write to the holding register.
216+
* @param client A pointer to the Arduino_10BASE_T1S_UDP client used for communication. Default is nullptr.
217+
* @return int Returns 1 if the write operation is successful, 0 otherwise.
218+
*/
219+
int holdingRegisterWrite(int id, int address, uint16_t value, Arduino_10BASE_T1S_UDP * client = nullptr);
220+
221+
/**
222+
* Reads the value of a holding register from the Modbus server.
223+
*
224+
* This function sends a request to read the value of a holding register at the specified address
225+
* from the Modbus server using the provided UDP client and port.
226+
*
227+
* @param address The address of the holding register to read.
228+
* @param client A pointer to the Arduino_10BASE_T1S_UDP client used for communication. Default is nullptr.
229+
* @return long The value of the holding register or -1 if an error occurs.
230+
*/
231+
long holdingRegisterRead(int address, Arduino_10BASE_T1S_UDP * client = nullptr);
232+
233+
/**
234+
* Reads the value of a holding register from the Modbus server with a specified ID.
235+
*
236+
* This function sends a request to read the value of a holding register at the specified address
237+
* from the Modbus server with the given ID using the provided UDP client and port.
238+
*
239+
* @param id The ID of the Modbus server.
240+
* @param address The address of the holding register to read.
241+
* @param client A pointer to the Arduino_10BASE_T1S_UDP client used for communication. Default is nullptr.
242+
* @return long The value of the holding register or -1 if an error occurs.
243+
*/
244+
long holdingRegisterRead(int id, int address, Arduino_10BASE_T1S_UDP * client = nullptr);
245+
246+
/**
247+
* Sets the T1S client for the Modbus communication.
248+
*
249+
* This function assigns a 10BASE-T1S UDP client to be used for Modbus communication.
250+
*
251+
* @param client A reference to an Arduino_10BASE_T1S_UDP object that represents the T1S client.
252+
*/
253+
void setT1SClient(Arduino_10BASE_T1S_UDP & client);
254+
255+
/**
256+
* Sets the gateway IP address for the Modbus client.
257+
*
258+
* This function sets the gateway IP address for the Modbus client.
259+
*
260+
* @param gateway The gateway IP address.
261+
*/
262+
263+
void setGateway(IPAddress gateway = IPAddress(0, 0, 0, 0));
264+
265+
/**
266+
* Sets the timeout for receiving a response from the Modbus server.
267+
*
268+
* This function sets the timeout for receiving a response from the Modbus server.
269+
*
270+
* @param timeout The timeout value in milliseconds.
271+
*/
272+
void setRxTimeout(unsigned long timeout = RX_TIMEOUT);
273+
274+
/**
275+
* Sets the port to use for communication.
276+
*
277+
* This function sets the port to use for communication.
278+
*
279+
* @param port The port to use.
280+
*/
281+
void setT1SPort(int port = 8889);
282+
283+
/**
284+
* Polls the Modbus client for incoming data.
285+
*
286+
* This function polls the Modbus client for incoming data.
287+
*
288+
* @return int The number of bytes read from the client.
289+
*/
290+
void update();
291+
292+
/**
293+
* Sets the callback function to be used by the ModbusT1SClient.
294+
*
295+
* This function allows you to specify a callback function that will be called
296+
* when certain events occur in the ModbusT1SClient. If no callback function is
297+
* provided, the default value of nullptr will be used, meaning no callback
298+
* will be executed.
299+
*
300+
* @param cb The callback function to be set. The default value is nullptr.
301+
*/
302+
void setCallback(callback_f cb = nullptr);
303+
304+
/**
305+
* Enables Power Over Ethernet (POE) on the Modbus client.
306+
*
307+
* This function enables Power Over Ethernet (POE) on the Modbus client and sets the device as the power source.
308+
*/
309+
void enablePOE();
310+
311+
/**
312+
* Disables Power Over Ethernet (POE) on the Modbus client.
313+
*
314+
* This function disables Power Over Ethernet (POE) on the Modbus client and set the USB as power source.
315+
*/
316+
void disablePOE();
246317

247318

248319
private:

‎src/ModbusT1SServer.cpp

Lines changed: 102 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ extern "C" {
2727

2828
#include "ModbusT1SServer.h"
2929
#ifndef __AVR__
30+
3031
/**
31-
* @class ModbusT1SServerClass
32-
* Class for Modbus T1S Server communication.
32+
* Default constructor for ModbusT1SServerClass.
3333
*
34-
* This class provides functionalities to communicate with a Modbus T1S server.
34+
* Initializes the Modbus server with RS485 communication.
3535
*/
3636
ModbusT1SServerClass::ModbusT1SServerClass():
3737
callback(default_OnPlcaStatus)
@@ -58,6 +58,16 @@ ModbusT1SServerClass::~ModbusT1SServerClass()
5858
{
5959
}
6060

61+
/**
62+
* Start the Modbus T1S server with the specified parameters and RS485 communication.
63+
*
64+
* @param node_id id of the server
65+
* @param baudrate Baud rate to use
66+
* @param config serial config. to use defaults to SERIAL_8N1
67+
* @param rs485 RS485 object to use
68+
*
69+
* Return 1 on success, 0 on failure
70+
*/
6171
int ModbusT1SServerClass::begin(int node_id, unsigned long baudrate, uint16_t config, RS485Class& rs485)
6272
{
6373
_node_id = node_id;
@@ -116,11 +126,18 @@ int ModbusT1SServerClass::begin(int node_id, unsigned long baudrate, uint16_t co
116126
return 0;
117127
}
118128

119-
ModbusRTUClient.setTimeout(2*1000); //RRR set timeout
129+
ModbusRTUClient.setTimeout(2*1000);
120130

121131
return 1;
122132
}
123133

134+
/**
135+
* Set the Modbus RTU client timeout.
136+
*
137+
* This function sets the Modbus RTU client timeout.
138+
*
139+
* @param timeout The timeout value in milliseconds.
140+
*/
124141
void ModbusT1SServerClass::setTimeout(unsigned long timeout) {
125142
ModbusRTUClient.setTimeout(timeout);
126143
}
@@ -156,10 +173,9 @@ int ModbusT1SServerClass::coilRead(int address)
156173
* on the Modbus server using the provided UDP client.
157174
*
158175
* @param address The address of the coil to write to.
159-
* @param value The value to write to the coil (1 for ON, 0 for OFF).
160176
* @return int 1 if the write operation is successful, -1 if an error occurs.
161177
*/
162-
int ModbusT1SServerClass::coilWrite(int address, uint8_t value)
178+
int ModbusT1SServerClass::coilWrite(int address)
163179
{
164180
return write(address, COILS);
165181
}
@@ -197,7 +213,6 @@ long ModbusT1SServerClass::inputRegisterRead(int address)
197213
* on the Modbus server using the provided UDP client.
198214
*
199215
* @param address The address of the holding register to write to.
200-
* @param value The value to write to the holding register.
201216
* @return int 1 if the write operation is successful, -1 if an error occurs.
202217
*/
203218
long ModbusT1SServerClass::holdingRegisterRead(int address) {
@@ -211,7 +226,6 @@ long ModbusT1SServerClass::holdingRegisterRead(int address) {
211226
* on the Modbus server using the provided UDP client.
212227
*
213228
* @param address The address of the holding register to write to.
214-
* @param value The value to write to the holding register.
215229
* @return int 1 if the write operation is successful, -1 if an error occurs.
216230
*/
217231
int ModbusT1SServerClass::holdingRegisterWrite(int address) {
@@ -243,6 +257,13 @@ int ModbusT1SServerClass::parsePacket() {
243257
return res;
244258
}
245259

260+
/**
261+
* Checks the PLCA status and Services the hardware and the protocol stack.
262+
*
263+
* This function checks the PLCA status and services the hardware and the
264+
* protocol stack.
265+
*
266+
*/
246267
void ModbusT1SServerClass::checkStatus()
247268
{
248269
tc6_inst->service();
@@ -259,10 +280,13 @@ void ModbusT1SServerClass::checkStatus()
259280
}
260281
}
261282

283+
/**
284+
* Manage the incoming T1S packets and make the appropriate Modbus request.
285+
*
286+
* This function manages the incoming T1S packets and makes the appropriate Modbus request.
287+
*/
262288
void ModbusT1SServerClass::update() {
263-
/* Services the hardware and the protocol stack.
264-
Must be called cyclic. The faster the better.
265-
*/
289+
266290
checkStatus();
267291
switch (parsePacket())
268292
{
@@ -295,6 +319,16 @@ void ModbusT1SServerClass::update() {
295319
}
296320
}
297321

322+
/**
323+
* Reads a value from the T1S client, sends it to the Modbus server and sends the response back to the client.
324+
*
325+
* This function reads a value from the T1S client, sends it to the Modbus server and sends the response back to the client.
326+
*
327+
* @param address The address of the value to read.
328+
* @param functionCode The function code to use for the Modbus request.
329+
*
330+
* @return long The value read from the Modbus server.
331+
*/
298332
long ModbusT1SServerClass::read(int address, int functionCode) {
299333
long res = -1;
300334
if(_server == nullptr) {
@@ -330,6 +364,16 @@ long ModbusT1SServerClass::read(int address, int functionCode) {
330364
return res;
331365
}
332366

367+
/**
368+
* reads a value from the T1S client and sends it to the Modbus server.
369+
*
370+
* This function reads a value from the T1S client and sends it to the Modbus server.
371+
*
372+
* @param address The address of the value to read.
373+
* @param functionCode The function code to use for the Modbus request.
374+
*
375+
* @return long The value read from the Modbus server.
376+
*/
333377
long ModbusT1SServerClass::write(int address, int functionCode) {
334378
long res = -1;
335379
if(_server == nullptr) {
@@ -359,7 +403,6 @@ long ModbusT1SServerClass::write(int address, int functionCode) {
359403
return res;
360404
}
361405

362-
363406
/**
364407
* Sets the Arduino_10BASE_T1S_UDP server for communication.
365408
*
@@ -371,20 +414,49 @@ void ModbusT1SServerClass::setT1SServer(Arduino_10BASE_T1S_UDP & server) {
371414
_server = &server;
372415
}
373416

374-
417+
/**
418+
* Sets the port to use for communication.
419+
*
420+
* This function sets the port to use for communication.
421+
*
422+
* @param port The port to use.
423+
*/
375424
void ModbusT1SServerClass::setT1SPort(int port) {
376425
udp_port = port;
377426
}
427+
428+
/**
429+
* Sets the baud rate to use for communication.
430+
*
431+
* This function sets the baud rate to use for communication.
432+
*
433+
* @param baudrate The baud rate to use.
434+
*/
378435
void ModbusT1SServerClass::setBaudrate(int baudrate) {
379436
_baudrate = baudrate;
380437
}
381438

439+
/**
440+
* Sets the callback function to use for PLCA status check.
441+
*
442+
* This function sets the callback function to use for PLCA status check.
443+
*
444+
* @param cb The callback function to use.
445+
*/
382446
void ModbusT1SServerClass::setCallback(callback_f cb) {
383447
if(cb != nullptr) {
384448
callback = cb;
385449
}
386450
}
387451

452+
/**
453+
* Default callback function for PLCA status check.
454+
*
455+
* This function is the default callback function for PLCA status check.
456+
*
457+
* @param success The success status of the PLCA status check.
458+
* @param plcaStatus The PLCA status.
459+
*/
388460
static void default_OnPlcaStatus(bool success, bool plcaStatus)
389461
{
390462
if (!success)
@@ -397,16 +469,33 @@ static void default_OnPlcaStatus(bool success, bool plcaStatus)
397469
}
398470
}
399471

472+
/**
473+
* Enables Power Over Ethernet (POE).
474+
*
475+
* This function enables Power Over Ethernet (POE) setting the device as power source.
476+
*/
400477
void ModbusT1SServerClass::enablePOE() {
401478
tc6_inst->digitalWrite(TC6::DIO::A0, true);
402479
tc6_inst->digitalWrite(TC6::DIO::A1, true);
403480
}
404481

482+
/**
483+
* Disables Power Over Ethernet (POE).
484+
*
485+
* This function disables Power Over Ethernet (POE) and set the USB as power source.
486+
*/
405487
void ModbusT1SServerClass::disablePOE() {
406488
tc6_inst->digitalWrite(TC6::DIO::A0, false);
407489
tc6_inst->digitalWrite(TC6::DIO::A1, true);
408490
}
409491

492+
/**
493+
* Sets the gateway IP address.
494+
*
495+
* This function sets the gateway IP address.
496+
*
497+
* @param ip The gateway IP address.
498+
*/
410499
void ModbusT1SServerClass::setGatwayIP(IPAddress ip) {
411500
_gateway = ip;
412501
}

‎src/ModbusT1SServer.h

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,16 @@ class ModbusT1SServerClass : public ModbusServer {
4747
/**
4848
* Start the Modbus T1S server with the specified parameters
4949
*
50-
* @param id (slave) id of the server
50+
* @param node_id id of the server
5151
* @param baudrate Baud rate to use
5252
* @param config serial config. to use defaults to SERIAL_8N1
53+
* @param rs485 RS485 object to use
5354
*
5455
* Return 1 on success, 0 on failure
5556
*/
5657

5758
int begin(int node_id, unsigned long baudrate, uint16_t config = SERIAL_8N1, RS485Class& rs485 = RS485);
59+
5860
/**
5961
* Reads a coil from the Modbus server.
6062
*
@@ -73,10 +75,9 @@ class ModbusT1SServerClass : public ModbusServer {
7375
* on the Modbus server using the provided UDP client.
7476
*
7577
* @param address The address of the coil to write to.
76-
* @param value The value to write to the coil (1 for ON, 0 for OFF).
7778
* @return int Returns 1 if the write operation is successful, 0 otherwise.
7879
*/
79-
int coilWrite(int address = -1, uint8_t value = 255);
80+
int coilWrite(int address = -1);
8081

8182
/**
8283
* Reads the status of a discrete input from the Modbus server.
@@ -107,7 +108,6 @@ class ModbusT1SServerClass : public ModbusServer {
107108
* on the Modbus server using the provided UDP client.
108109
*
109110
* @param address The address of the holding register to write to.
110-
* @param value The value to write to the holding register.
111111
* @return int 1 if the write operation is successful, -1 if an error occurs.
112112
*/
113113
long holdingRegisterRead(int address = -1);
@@ -119,7 +119,6 @@ class ModbusT1SServerClass : public ModbusServer {
119119
* on the Modbus server using the provided UDP client.
120120
*
121121
* @param address The address of the holding register to write to.
122-
* @param value The value to write to the holding register.
123122
* @return int 1 if the write operation is successful, -1 if an error occurs.
124123
*/
125124
int holdingRegisterWrite(int address = -1);
@@ -140,26 +139,90 @@ class ModbusT1SServerClass : public ModbusServer {
140139
*/
141140
void setT1SServer(Arduino_10BASE_T1S_UDP & server);
142141

142+
/**
143+
* Set the port to use for communication
144+
*
145+
* @param port The port to use
146+
*/
143147
void setT1SPort(int port = 8889);
144148

149+
/**
150+
* Update the Modbus server.
151+
*
152+
* This function updates the Modbus server.
153+
*/
145154
void update();
146155

156+
/**
157+
* Set the baudrate to use for communication
158+
*
159+
* @param baudrate The baudrate to use
160+
*/
147161
void setBaudrate(int baudrate);
148162

163+
/**
164+
* Sets the gateway IP address.
165+
*
166+
* This function sets the gateway IP address.
167+
*
168+
* @param ip The gateway IP address.
169+
*/
170+
void setGatwayIP(IPAddress ip);
149171

150172
/**
151-
* Poll interface for requests
173+
* Poll the Modbus server.
174+
*
175+
* This function polls the Modbus server.
176+
*
177+
* @return int The status of the poll.
152178
*/
153-
void setGatwayIP(IPAddress ip);
154179
virtual int poll();
180+
181+
/**
182+
* Checks the PLCA status and Services the hardware and the protocol stack.
183+
*
184+
* This function checks the PLCA status and services the hardware and the
185+
* protocol stack.
186+
*
187+
*/
155188
void checkStatus();
189+
190+
/**
191+
* Set the callback function to use for PLCA status check.
192+
*
193+
* This function sets the callback function to use for PLCA status check.
194+
*
195+
* @param cb The callback function to use.
196+
*/
156197
void setCallback(callback_f cb = nullptr);
157-
long read(int address, int functionCode);
158-
long write(int address, int functionCode);
198+
199+
/**
200+
* Enables Power Over Ethernet (POE).
201+
*
202+
* This function enables Power Over Ethernet (POE) setting the device as power source.
203+
*/
159204
void enablePOE();
205+
206+
/**
207+
* Disables Power Over Ethernet (POE).
208+
*
209+
* This function disables Power Over Ethernet (POE) and set the USB as power source.
210+
*/
160211
void disablePOE();
212+
213+
/**
214+
* Set the Modbus RTU client timeout.
215+
*
216+
* This function sets the Modbus RTU client timeout.
217+
*
218+
* @param timeout The timeout value in milliseconds.
219+
*/
161220
void setTimeout(unsigned long timeout);
162221

222+
private:
223+
long read(int address, int functionCode);
224+
long write(int address, int functionCode);
225+
163226
private:
164227
IPAddress _gateway = IPAddress(0, 0, 0, 0);
165228
uint8_t udp_rx_buf[8] = {0};

0 commit comments

Comments
 (0)
Please sign in to comment.