1010template <class T >
1111class 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 ) \
166166template <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 ) \
200200template <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
213280template <class T > \
214281bool 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
341408template <class T > \
342409template <typename TYPEID> \
343410uint16_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};
348415template <class T > \
349416template <typename TYPEID> \
350417uint16_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};
355422template <class T > \
356423template <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
420487template <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
433500template <class T >
434501template <typename TYPEID>
435502uint16_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}
0 commit comments