@@ -12,8 +12,6 @@ http://www.modbus.org/docs/Modbus_Messaging_Implementation_Guide_V1_0b.pdf
1212
1313## Features
1414
15- Keep alive and ESP32 support added to original library. This features is under testing.
16-
1715<ul >
1816<li >Operates as a slave</li >
1917<li >Supports Modbus IP (TCP)</li >
@@ -29,6 +27,12 @@ Keep alive and ESP32 support added to original library. This features is under t
2927 <li>0x0F - Write Multiple Coils</li>
3028 <li>0x10 - Write Multiple Registers</li>
3129</ul >
30+ <li >Callbacks for</li >
31+ <ul >
32+ <li> - Incoming IP connection</li>
33+ <li> - Read specific Register</li>
34+ <li> - Write specific Register</li>
35+ </ul >
3236</ul >
3337
3438<b >Notes:</b >
@@ -54,18 +58,96 @@ Thus, only the following functions are supported:
5458</ul >
5559
5660
57- ## How to
61+ ## API
62+
63+ ### Add [ multiple] regs
64+ ```
65+ bool addReg(uint16_t address, uint16_t value = 0, uint16_t numregs = 1)
66+ bool addHreg(uint16_t offset, uint16_t value = 0, uint16_t numregs = 1)
67+ bool addCoil(uint16_t offset, bool value = false, uint16_t numregs = 1)
68+ bool addIsts(uint16_t offset, bool value = false, uint16_t numregs = 1)
69+ bool addIreg(uint16_t offset, uint16_t value = 0, uint16_t nemregs = 1)
70+ ```
71+ ### Write regs
72+ ```
73+ bool Reg(uint16_t address, uint16_t value)
74+ bool Hreg(uint16_t offset, uint16_t value)
75+ bool Coil(uint16_t offset, bool value)
76+ bool Ists(uint16_t offset, bool value)
77+ bool Ireg(uint16_t offset, uint16_t value)
78+ ```
79+ ### Read regs
80+ ```
81+ uint16_t Reg(uint16_t address)
82+ uint16_t Hreg(uint16_t offset)
83+ bool Coil(uint16_t offset)
84+ bool Ists(uint16_t offset)
85+ uint16_t Ireg(uint16_t offset)
86+ ```
87+ ### Callbacks
88+ ```
89+ bool onGet(uint16_t address, cbModbus cb = cbDefault, uint16_t numregs = 1)
90+ bool onSet(uint16_t address, cbModbus cb = cbDefault, uint16_t numregs = 1)
91+ void onConnect(cbModbusConnect cb)
92+ typedef uint16_t (*cbModbus)(TRegister* reg, uint16_t val)
93+ typedef bool (*cbModbusConnect)(IPAddress ip)
94+ ```
95+ ### Macros
96+ ```
97+ #define COIL(n)
98+ #define ISTS(n)
99+ #define IREG(n)
100+ #define HREG(n)
101+ #define COIL_VAL(v)
102+ #define COIL_BOOL(v)
103+ #define ISTS_VAL(v)
104+ #define ISTS_BOOL(v)
105+ ```
106+ ### ModBus IP specific
107+ ```
108+ void begin()
109+ void task()
110+ ```
111+
112+ ### Callback example
58113
59114```
60- This README is under development, for now, see the examples of the library.
115+ bool coil = false; // Define external variable to get/set value
116+ uint16_t cbCoilSet(TRegister* reg, uint16_t val) { // 'reg' is pointer to reg to modify, 'val' is new register value
117+ coil = COIL_BOOL(val);
118+ return val; // Returns value to be saved to TRegister structure
119+ }
120+ uint16_t cbCoilGet(TRegister* reg, uint16_t val) {
121+ return COIL_VAL(coil); // Returns value to be returned to ModBus master as reply for current request
122+ }
123+ bool cbConn(IPAddress ip) {
124+ Serial.println(ip);
125+ return true; // Return 'true' to allow connection or 'false' to drop connection
126+ }
127+ ModbusIP mb; // ModbusIP object
128+ void setup() {
129+ ...
130+ mb.onConnect(cbConn); // Add callback on connection event
131+ mb.begin();
132+ mb.addCoil(COIL_NR); // Add Coil
133+ mb.onSet(COIL(COIL_NR), cbCoilSet); // Add callback on Coil COIL_NR value set
134+ mb.onGet(COIL(COIL_NR), cbCoilGet); // Add callback on Coil COIL_NR value get
135+ ...
136+ }
137+ void loop() {
138+ ...
139+ mb.task();
140+ ...
141+ }
61142```
62143
144+
63145## Contributions
64146
65147https://github.com/emelianov/modbus-esp8266 <br >
6614867149
68- Original version:
150+ Original version:< br >
69151http://github.com/andresarmento/modbus-esp8266 <br >
70152prof (at) andresarmento (dot) com
71153
0 commit comments