Skip to content

Commit 18e3e78

Browse files
committedJun 11, 2023
feat: Add debug features
1 parent f0f6f60 commit 18e3e78

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed
 

‎src/Modbus.cpp

+18-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ Modbus::Modbus() :
1212
_additional_data (0),
1313
_frame (nullptr),
1414
_len (0),
15-
_reply (0) {
15+
_reply (0),
16+
_debug (nullptr) {
1617
}
1718

1819
//-------------------------------------------------------------------------------
@@ -361,6 +362,22 @@ void Modbus::readRegisters (const byte fcode, const word startreg, const word nu
361362
_reply = MB_REPLY_NORMAL;
362363
}
363364

365+
//-------------------------------------------------------------------------------
366+
// protected
367+
void Modbus::debugMessage (bool reply) {
368+
369+
if (_len && isDebug()) {
370+
char str[3]; // buffer for sprintf
371+
for (uint8_t i = 0; i < _len; i++) {
372+
373+
sprintf (str, "%02X", _frame[i]); // str stores the string representation of the byte, in hex form
374+
_debug->write (!reply ? '[' : '<');
375+
_debug->print (str);
376+
_debug->write (!reply ? ']' : '>');
377+
}
378+
_debug->println ("");
379+
}
380+
}
364381

365382
#ifndef USE_HOLDING_REGISTERS_ONLY
366383
//-------------------------------------------------------------------------------

‎src/Modbus.h

+29-2
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ class Modbus {
7373
void writeSingleRegister (const word reg, const word value);
7474
void writeMultipleRegisters (const byte *frame, const word startreg, const word numoutputs, const byte bytecount);
7575
void exceptionResponse (const byte fcode, const byte excode);
76-
76+
7777
#ifndef USE_HOLDING_REGISTERS_ONLY
78-
void readBits (const byte fcode,const word startreg, const word numregs);
78+
void readBits (const byte fcode, const word startreg, const word numregs);
7979
void writeSingleCoil (const word reg, const word status);
8080
void writeMultipleCoils (const byte *frame, const word startreg, const word numoutputs, const byte bytecount);
8181
#endif
@@ -101,8 +101,10 @@ class Modbus {
101101
byte *_frame;
102102
byte _len;
103103
byte _reply;
104+
Print *_debug;
104105
void receivePDU (byte *frame);
105106
void reportServerId();
107+
void debugMessage (bool reply = false);
106108
#endif
107109

108110
public:
@@ -180,6 +182,31 @@ class Modbus {
180182
return hreg (offset);
181183
}
182184

185+
/**
186+
@brief Enable debug mode
187+
*/
188+
inline void setDebug (Print &print) {
189+
_debug = &print;
190+
_debug->println ("Modbus: debug enabled....");
191+
}
192+
193+
/**
194+
@brief Returns true if debug mode is enabled
195+
*/
196+
inline bool isDebug () {
197+
return _debug != nullptr;
198+
}
199+
200+
/**
201+
@brief Print a debug message, only if debug mode is enabled
202+
@param msg message to print
203+
*/
204+
inline void debug (const char *msg) {
205+
if (_debug) {
206+
_debug->println (msg);
207+
}
208+
}
209+
183210
#ifndef USE_HOLDING_REGISTERS_ONLY
184211

185212
/**

0 commit comments

Comments
 (0)