Skip to content

Commit 69f3d07

Browse files
committed
Replace magic number with constants
- MODBUS_MAX_WORDS/BITS - NULLREG - Alternative API read/write functions implemented
1 parent 2f14138 commit 69f3d07

File tree

6 files changed

+146
-71
lines changed

6 files changed

+146
-71
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ For more information about Modbus see:
4747
## Last Changes
4848

4949
```diff
50+
// 4.1.1
51+
+ ModbusTCP: Fix potential memory leak
52+
+ API: cbEnable/cbDisable functionality extended
53+
+ ESP-IDF: CMakeList.txt added
54+
+ Examples: TCP-to-RTU fixed
5055
// 4.1.0
5156
+ API: Raw Modbus frame processing functionality
5257
+ ModbusRTU: Precise inter-frame interval control

src/Modbus.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ static inline uint16_t __swap_16(uint16_t num) { return (num >> 8) | (num << 8);
2222
#define ISTS(n) (TAddress){TAddress::ISTS, n}
2323
#define IREG(n) (TAddress){TAddress::IREG, n}
2424
#define HREG(n) (TAddress){TAddress::HREG, n}
25+
#define NULLREG (TAddress){TAddress::NONE, 0xFFFF}
2526
#define BIT_VAL(v) (v?0xFF00:0x0000)
2627
#define BIT_BOOL(v) (v==0xFF00)
2728
#define COIL_VAL(v) (v?0xFF00:0x0000)
@@ -40,7 +41,7 @@ typedef uint16_t (*cbModbus)(TRegister* reg, uint16_t val); // Callback function
4041
#endif
4142

4243
struct TAddress {
43-
enum RegType {COIL, ISTS, IREG, HREG};
44+
enum RegType {COIL, ISTS, IREG, HREG, NONE = 0xFF};
4445
RegType type;
4546
uint16_t address;
4647
bool operator==(const TAddress &obj) const { // TAddress == TAddress

src/ModbusAPI.h

Lines changed: 135 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
template <class T>
1111
class ModbusAPI : public T {
1212
public:
13-
/* // New API
13+
// Alternative API
14+
template <typename TYPEID>
15+
uint16_t read(TYPEID id, TAddress reg, uint16_t* value, uint16_t numregs = 1, cbTransaction cb = nullptr, uint8_t unit = MODBUSIP_UNIT);
16+
template <typename TYPEID>
17+
uint16_t read(TYPEID id, TAddress reg, bool* value, uint16_t numregs = 1, cbTransaction cb = nullptr, uint8_t unit = MODBUSIP_UNIT);
1418
template <typename TYPEID>
1519
uint16_t write(TYPEID id, TAddress reg, uint16_t value, cbTransaction cb = nullptr, uint8_t unit = MODBUSIP_UNIT);
1620
template <typename TYPEID>
@@ -19,64 +23,60 @@ class ModbusAPI : public T {
1923
uint16_t write(TYPEID id, TAddress reg, uint16_t* value, uint16_t numregs = 1, cbTransaction cb = nullptr, uint8_t unit = MODBUSIP_UNIT);
2024
template <typename TYPEID>
2125
uint16_t write(TYPEID id, TAddress reg, bool* value, uint16_t numregs = 1, cbTransaction cb = nullptr, uint8_t unit = MODBUSIP_UNIT);
22-
template <typename TYPEID>
23-
uint16_t read(TYPEID id, TAddress reg, uint16_t* value, uint16_t numregs = 1, cbTransaction cb = nullptr, uint8_t unit = MODBUSIP_UNIT);
24-
template <typename TYPEID>
25-
uint16_t read(TYPEID id, TAddress reg, bool* value, uint16_t numregs = 1, cbTransaction cb = nullptr, uint8_t unit = MODBUSIP_UNIT);
26-
26+
/*
2727
template <typename TYPEID>
2828
uint16_t push(TYPEID id, TAddress to, TAddress from, uint16_t numregs = 1, cbTransaction cb = nullptr, uint8_t unit = MODBUSIP_UNIT);
2929
template <typename TYPEID>
3030
uint16_t pull(TYPEID id, TAddress from, TAddress to, uint16_t numregs = 1, cbTransaction cb = nullptr, uint8_t unit = MODBUSIP_UNIT);
3131
*/
32-
// Legacy API
32+
// Classic API
3333
bool Hregs(uint16_t offset, uint16_t* value, uint16_t numregs = 1) {return this->Reg(HREG(offset), value);}
34-
bool Coils(uint16_t offset, bool* value, uint16_t numregs = 1) {return this->Reg(COIL(offset), value);}
35-
bool Istss(uint16_t offset, bool* value, uint16_t numregs = 1) {return this->Reg(ISTS(offset), value);}
36-
bool Iregs(uint16_t offset, uint16_t* value, uint16_t numregs = 1) {return this->Reg(IREG(offset), value);}
34+
bool Coils(uint16_t offset, bool* value, uint16_t numregs = 1) {return this->Reg(COIL(offset), value);}
35+
bool Istss(uint16_t offset, bool* value, uint16_t numregs = 1) {return this->Reg(ISTS(offset), value);}
36+
bool Iregs(uint16_t offset, uint16_t* value, uint16_t numregs = 1) {return this->Reg(IREG(offset), value);}
3737

3838
//bool addHreg(uint16_t offset, uint16_t* value, uint16_t numregs = 1) {return this->addReg(HREG(offset), value);}
39-
//bool addCoil(uint16_t offset, bool* value, uint16_t numregs = 1) {return this->addReg(COIL(offset), value);}
40-
//bool addIsts(uint16_t offset, bool* value, uint16_t numregs = 1) {return this->addReg(ISTS(offset), value);}
41-
//bool addIreg(uint16_t offset, uint16_t* value, uint16_t numregs = 1) {return this->addReg(IREG(offset), value);}
39+
//bool addCoil(uint16_t offset, bool* value, uint16_t numregs = 1) {return this->addReg(COIL(offset), value);}
40+
//bool addIsts(uint16_t offset, bool* value, uint16_t numregs = 1) {return this->addReg(ISTS(offset), value);}
41+
//bool addIreg(uint16_t offset, uint16_t* value, uint16_t numregs = 1) {return this->addReg(IREG(offset), value);}
4242

4343
bool addHreg(uint16_t offset, uint16_t value = 0, uint16_t numregs = 1);
44-
bool addCoil(uint16_t offset, bool value = false, uint16_t numregs = 1);
45-
bool addIsts(uint16_t offset, bool value = false, uint16_t numregs = 1);
46-
bool addIreg(uint16_t offset, uint16_t value = 0, uint16_t numregs = 1);
44+
bool addCoil(uint16_t offset, bool value = false, uint16_t numregs = 1);
45+
bool addIsts(uint16_t offset, bool value = false, uint16_t numregs = 1);
46+
bool addIreg(uint16_t offset, uint16_t value = 0, uint16_t numregs = 1);
4747

48-
bool Hreg(uint16_t offset, uint16_t value);
49-
bool Coil(uint16_t offset, bool value);
50-
bool Ists(uint16_t offset, bool value);
51-
bool Ireg(uint16_t offset, uint16_t value);
48+
bool Hreg(uint16_t offset, uint16_t value);
49+
bool Coil(uint16_t offset, bool value);
50+
bool Ists(uint16_t offset, bool value);
51+
bool Ireg(uint16_t offset, uint16_t value);
5252

53-
bool Coil(uint16_t offset);
54-
bool Ists(uint16_t offset);
55-
uint16_t Ireg(uint16_t offset);
56-
uint16_t Hreg(uint16_t offset);
53+
bool Coil(uint16_t offset);
54+
bool Ists(uint16_t offset);
55+
uint16_t Ireg(uint16_t offset);
56+
uint16_t Hreg(uint16_t offset);
5757

58-
bool removeCoil(uint16_t offset, uint16_t numregs = 1);
59-
bool removeIsts(uint16_t offset, uint16_t numregs = 1);
60-
bool removeIreg(uint16_t offset, uint16_t numregs = 1);
61-
bool removeHreg(uint16_t offset, uint16_t numregs = 1);
58+
bool removeCoil(uint16_t offset, uint16_t numregs = 1);
59+
bool removeIsts(uint16_t offset, uint16_t numregs = 1);
60+
bool removeIreg(uint16_t offset, uint16_t numregs = 1);
61+
bool removeHreg(uint16_t offset, uint16_t numregs = 1);
6262

63-
bool onGetCoil(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
64-
bool onSetCoil(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
65-
bool onGetHreg(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
66-
bool onSetHreg(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
67-
bool onGetIsts(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
68-
bool onSetIsts(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
69-
bool onGetIreg(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
70-
bool onSetIreg(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
63+
bool onGetCoil(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
64+
bool onSetCoil(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
65+
bool onGetHreg(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
66+
bool onSetHreg(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
67+
bool onGetIsts(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
68+
bool onSetIsts(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
69+
bool onGetIreg(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
70+
bool onSetIreg(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
7171

72-
bool removeOnGetCoil(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
73-
bool removeOnSetCoil(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
74-
bool removeOnGetHreg(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
75-
bool removeOnSetHreg(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
76-
bool removeOnGetIsts(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
77-
bool removeOnSetIsts(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
78-
bool removeOnGetIreg(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
79-
bool removeOnSetIreg(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
72+
bool removeOnGetCoil(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
73+
bool removeOnSetCoil(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
74+
bool removeOnGetHreg(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
75+
bool removeOnSetHreg(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
76+
bool removeOnGetIsts(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
77+
bool removeOnSetIsts(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
78+
bool removeOnGetIreg(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
79+
bool removeOnSetIreg(uint16_t offset, cbModbus cb = nullptr, uint16_t numregs = 1);
8080

8181
template <typename TYPEID>
8282
uint16_t writeCoil(TYPEID id, uint16_t offset, bool value, cbTransaction cb = nullptr, uint8_t unit = MODBUSIP_UNIT);
@@ -117,7 +117,7 @@ class ModbusAPI : public T {
117117
template <typename TYPEID>
118118
uint16_t pushIregToHreg(TYPEID id, uint16_t to, uint16_t from, uint16_t numregs = 1, cbTransaction cb = nullptr, uint8_t unit = MODBUSIP_UNIT);
119119

120-
template <typename TYPEID>
120+
template <typename TYPEID>
121121
uint16_t readFileRec(TYPEID slaveId, uint16_t fileNum, uint16_t startRec, uint16_t len, uint8_t* data, cbTransaction cb = nullptr, uint8_t unit = MODBUSIP_UNIT);
122122
template <typename TYPEID>
123123
uint16_t writeFileRec(TYPEID slaveId, uint16_t fileNum, uint16_t startRec, uint16_t len, uint8_t* data, cbTransaction cb = nullptr, uint8_t unit = MODBUSIP_UNIT);
@@ -159,8 +159,8 @@ uint16_t ModbusAPI<T>::FNAME(TYPEID ip, uint16_t offset, VALTYPE* value, uint16_
159159
this->VALUE(REG(offset), offset, numregs, Modbus::FUNC, value); \
160160
return this->send(ip, REG(offset), cb, unit); \
161161
}
162-
IMPLEMENT_WRITEREGS(writeCoil, COIL, FC_WRITE_COILS, writeSlaveBits, 0x07D0, bool)
163-
IMPLEMENT_WRITEREGS(writeHreg, HREG, FC_WRITE_REGS, writeSlaveWords, 0x007D, uint16_t)
162+
IMPLEMENT_WRITEREGS(writeCoil, COIL, FC_WRITE_COILS, writeSlaveBits, MODBUS_MAX_BITS, bool)
163+
IMPLEMENT_WRITEREGS(writeHreg, HREG, FC_WRITE_REGS, writeSlaveWords, MODBUS_MAX_WORDS, uint16_t)
164164

165165
#define IMPLEMENT_READREGS(FNAME, REG, FUNC, MAXNUM, VALTYPE) \
166166
template <class T> \
@@ -170,10 +170,10 @@ uint16_t ModbusAPI<T>::FNAME(TYPEID ip, uint16_t offset, VALTYPE* value, uint16_
170170
this->readSlave(offset, numregs, Modbus::FUNC); \
171171
return this->send(ip, REG(offset), cb, unit, (uint8_t*)value); \
172172
}
173-
IMPLEMENT_READREGS(readCoil, COIL, FC_READ_COILS, 0x07D0, bool)
174-
IMPLEMENT_READREGS(readHreg, HREG, FC_READ_REGS, 0x007D, uint16_t)
175-
IMPLEMENT_READREGS(readIsts, ISTS, FC_READ_INPUT_STAT, 0x07D0, bool)
176-
IMPLEMENT_READREGS(readIreg, IREG, FC_READ_INPUT_REGS, 0x007D, uint16_t)
173+
IMPLEMENT_READREGS(readCoil, COIL, FC_READ_COILS, MODBUS_MAX_BITS, bool)
174+
IMPLEMENT_READREGS(readHreg, HREG, FC_READ_REGS, MODBUS_MAX_WORDS, uint16_t)
175+
IMPLEMENT_READREGS(readIsts, ISTS, FC_READ_INPUT_STAT, MODBUS_MAX_BITS, bool)
176+
IMPLEMENT_READREGS(readIreg, IREG, FC_READ_INPUT_REGS, MODBUS_MAX_WORDS, uint16_t)
177177

178178
#if defined(MODBUS_ADD_REG)
179179
#define ADDREG(R) this->addReg(R(to), (uint16_t)0, numregs);
@@ -189,12 +189,12 @@ uint16_t ModbusAPI<T>::FNAME(TYPEID ip, uint16_t from, uint16_t to, uint16_t num
189189
this->readSlave(from, numregs, Modbus::FUNC); \
190190
return this->send(ip, REG(to), cb, unit); \
191191
}
192-
IMPLEMENT_PULL(pullCoil, COIL, FC_READ_COILS, 0x07D0)
193-
IMPLEMENT_PULL(pullIsts, ISTS, FC_READ_INPUT_STAT, 0x07D0)
194-
IMPLEMENT_PULL(pullHreg, HREG, FC_READ_REGS, 0x007D)
195-
IMPLEMENT_PULL(pullIreg, IREG, FC_READ_INPUT_REGS, 0x007D)
196-
IMPLEMENT_PULL(pullHregToIreg, IREG, FC_READ_REGS, 0x007D)
197-
IMPLEMENT_PULL(pullCoilToIsts, ISTS, FC_READ_COILS, 0x07D0)
192+
IMPLEMENT_PULL(pullCoil, COIL, FC_READ_COILS, MODBUS_MAX_BITS)
193+
IMPLEMENT_PULL(pullIsts, ISTS, FC_READ_INPUT_STAT, MODBUS_MAX_BITS)
194+
IMPLEMENT_PULL(pullHreg, HREG, FC_READ_REGS, MODBUS_MAX_WORDS)
195+
IMPLEMENT_PULL(pullIreg, IREG, FC_READ_INPUT_REGS, MODBUS_MAX_WORDS)
196+
IMPLEMENT_PULL(pullHregToIreg, IREG, FC_READ_REGS, MODBUS_MAX_WORDS)
197+
IMPLEMENT_PULL(pullCoilToIsts, ISTS, FC_READ_COILS, MODBUS_MAX_BITS)
198198

199199
#define IMPLEMENT_PUSH(FNAME, REG, FUNC, MAXNUM, FINT) \
200200
template <class T> \
@@ -205,10 +205,77 @@ uint16_t ModbusAPI<T>::FNAME(TYPEID ip, uint16_t to, uint16_t from, uint16_t num
205205
this->FINT(REG(from), to, numregs, Modbus::FUNC); \
206206
return this->send(ip, REG(from), cb, unit); \
207207
}
208-
IMPLEMENT_PUSH(pushCoil, COIL, FC_WRITE_COILS, 0x7D0, writeSlaveBits)
209-
IMPLEMENT_PUSH(pushHreg, HREG, FC_WRITE_REGS, 0x007D, writeSlaveWords)
210-
IMPLEMENT_PUSH(pushIregToHreg, IREG, FC_WRITE_REGS, 0x007D, writeSlaveWords)
211-
IMPLEMENT_PUSH(pushIstsToCoil, ISTS, FC_WRITE_COILS, 0x07D0, writeSlaveBits)
208+
IMPLEMENT_PUSH(pushCoil, COIL, FC_WRITE_COILS, MODBUS_MAX_BITS, writeSlaveBits)
209+
IMPLEMENT_PUSH(pushHreg, HREG, FC_WRITE_REGS, MODBUS_MAX_WORDS, writeSlaveWords)
210+
IMPLEMENT_PUSH(pushIregToHreg, IREG, FC_WRITE_REGS, MODBUS_MAX_WORDS, writeSlaveWords)
211+
IMPLEMENT_PUSH(pushIstsToCoil, ISTS, FC_WRITE_COILS, MODBUS_MAX_BITS, writeSlaveBits)
212+
213+
template <class T>
214+
template <typename TYPEID>
215+
uint16_t ModbusAPI<T>::read(TYPEID id, TAddress reg, uint16_t* value, uint16_t numregs, cbTransaction cb, uint8_t unit) {
216+
switch (reg.type) {
217+
case TAddress::HREG:
218+
return readHreg(id, reg.address, value, numregs, cb, unit);
219+
case TAddress::IREG:
220+
return readIreg(id, reg.address, value, numregs, cb, unit);
221+
default:
222+
return 0;
223+
}
224+
}
225+
template <class T>
226+
template <typename TYPEID>
227+
uint16_t ModbusAPI<T>::read(TYPEID id, TAddress reg, bool* value, uint16_t numregs, cbTransaction cb, uint8_t unit) {
228+
switch (reg.type) {
229+
case TAddress::COIL:
230+
return readCoil(id, reg.address, value, numregs, cb, unit);
231+
case TAddress::ISTS:
232+
return readIsts(id, reg.address, value, numregs, cb, unit);
233+
default:
234+
return 0;
235+
}
236+
}
237+
template <class T>
238+
template <typename TYPEID>
239+
uint16_t ModbusAPI<T>::write(TYPEID id, TAddress reg, uint16_t value, cbTransaction cb, uint8_t unit) {
240+
switch (reg.type) {
241+
case TAddress::COIL:
242+
return writeCoil(id, reg.address, value, cb, unit);
243+
case TAddress::HREG:
244+
return writeHreg(id, reg.address, value, cb, unit);
245+
default:
246+
return 0;
247+
}
248+
}
249+
template <class T>
250+
template <typename TYPEID>
251+
uint16_t ModbusAPI<T>::write(TYPEID id, TAddress reg, bool value, cbTransaction cb, uint8_t unit) {
252+
switch (reg.type) {
253+
case TAddress::COIL:
254+
return writeCoil(id, reg.address, value, cb, unit);
255+
default:
256+
return 0;
257+
}
258+
}
259+
uint16_t ModbusAPI<T>::write(TYPEID id, TAddress reg, uint16_t* value, uint16_t numregs, cbTransaction cb, uint8_t unit) {
260+
switch (reg.type) {
261+
case TAddress::COIL:
262+
return writeCoil(id, reg.address, value, numregs, cb, unit);
263+
case TAddress::HREG:
264+
return writeHreg(id, reg.address, value, numregs, cb, unit);
265+
default:
266+
return 0;
267+
}
268+
}
269+
template <class T>
270+
template <typename TYPEID>
271+
uint16_t ModbusAPI<T>::write(TYPEID id, TAddress reg, bool* value, uint16_t numregs, cbTransaction cb, uint8_t unit) {
272+
switch (reg.type) {
273+
case TAddress::COIL:
274+
return writeCoil(id, reg.address, value, cb, numregs, unit);
275+
default:
276+
return 0;
277+
}
278+
}
212279

213280
template <class T> \
214281
bool ModbusAPI<T>::addHreg(uint16_t offset, uint16_t value, uint16_t numregs) {
@@ -341,16 +408,16 @@ bool ModbusAPI<T>::removeOnSetIreg(uint16_t offset, cbModbus cb, uint16_t numreg
341408
template <class T> \
342409
template <typename TYPEID> \
343410
uint16_t ModbusAPI<T>::readFileRec(TYPEID slaveId, uint16_t fileNum, uint16_t startRec, uint16_t len, uint8_t* data, cbTransaction cb, uint8_t unit) {
344-
if (startRec > 0x270F) return 0;
411+
if (startRec > MODBUS_MAX_FILES) return 0;
345412
if (!this->readSlaveFile(&fileNum, &startRec, &len, 1, Modbus::FC_READ_FILE_REC)) return 0;
346-
return this->send(slaveId, HREG(0), cb, unit, data); // HREG(0) - just dummy value
413+
return this->send(slaveId, NULLREG, cb, unit, data);
347414
};
348415
template <class T> \
349416
template <typename TYPEID> \
350417
uint16_t ModbusAPI<T>::writeFileRec(TYPEID slaveId, uint16_t fileNum, uint16_t startRec, uint16_t len, uint8_t* data, cbTransaction cb, uint8_t unit) {
351-
if (startRec > 0x270F) return 0;
418+
if (startRec > MODBUS_MAX_FILES) return 0;
352419
if (!this->writeSlaveFile(&fileNum, &startRec, &len, 1, Modbus::FC_WRITE_FILE_REC, data)) return 0;
353-
return this->send(slaveId, HREG(0), cb, unit); // HREG(0) - just dummy value
420+
return this->send(slaveId, NULLREG, cb, unit);
354421
};
355422
template <class T> \
356423
template <typename TYPEID> \
@@ -414,7 +481,7 @@ uint16_t ModbusAPI<T>::rawRequest(TYPEID ip, \
414481
return 0;
415482
this->_len = len;
416483
memcpy(this->_frame, data, len);
417-
return this->send(ip, HREG(0), cb, unit);
484+
return this->send(ip, NULLREG, cb, unit);
418485
};
419486

420487
template <class T>
@@ -427,12 +494,12 @@ uint16_t ModbusAPI<T>::rawResponce(TYPEID ip, \
427494
return 0;
428495
this->_len = len;
429496
memcpy(this->_frame, data, len);
430-
return this->send(ip, HREG(0), nullptr, unit, nullptr, false);
497+
return this->send(ip, NULLREG, nullptr, unit, nullptr, false);
431498
};
432499

433500
template <class T>
434501
template <typename TYPEID>
435502
uint16_t ModbusAPI<T>::errorResponce(TYPEID ip, Modbus::FunctionCode fn, Modbus::ResultCode excode, uint8_t unit) {
436503
this->exceptionResponse(fn, excode);
437-
return this->send(ip, HREG(0), nullptr, unit, nullptr, false);
504+
return this->send(ip, NULLREG, nullptr, unit, nullptr, false);
438505
}

src/ModbusRTU.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class ModbusRTUTemplate : public Modbus {
2828
uint8_t* _data = nullptr;
2929
uint8_t* _sentFrame = nullptr;
3030
TAddress _sentReg = COIL(0);
31-
uint16_t maxRegs = 0x007D;
31+
uint16_t maxRegs = MODBUS_MAX_WORDS;
3232
uint8_t address = 0;
3333

3434
uint16_t send(uint8_t slaveId, TAddress startreg, cbTransaction cb, uint8_t unit = MODBUSIP_UNIT, uint8_t* data = nullptr, bool waitResponse = true);

src/ModbusSettings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ If defined regisers count will be limited.
5858
#define MODBUS_MAX_WORDS 0x007D
5959
#define MODBUS_MAX_BITS 0x07D0
6060
#define MODBUS_FILES
61+
#define MODBUS_MAX_FILES 0x270F
6162
#define MODBUSTCP_PORT 502
6263
#define MODBUSTLS_PORT 802
6364
#define MODBUSIP_MAXFRAME 200

src/ModbusTCPTemplate.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ void ModbusTCPTemplate<SERVER, CLIENT>::task() {
259259
Serial.print(n);
260260
Serial.print(": Bytes available ");
261261
Serial.println(tcpclient[n]->available());
262+
#endif
262263
#endif
263264
tcpclient[n]->readBytes(_MBAP.raw, sizeof(_MBAP.raw)); // Get MBAP
264265

@@ -309,7 +310,7 @@ void ModbusTCPTemplate<SERVER, CLIENT>::task() {
309310
if (trans) { // if valid transaction id
310311
if ((_frame[0] & 0x7F) == trans->_frame[0]) { // Check if function code the same as requested
311312
if (_reply == EX_PASSTHROUGH)
312-
masterPDU(_frame, trans->_frame, trans->startreg, trans->data); // Procass incoming frame as master
313+
masterPDU(_frame, trans->_frame, trans->startreg, trans->data); // Process incoming frame as master
313314
}
314315
else {
315316
_reply = EX_UNEXPECTED_RESPONSE;

0 commit comments

Comments
 (0)