Skip to content

Commit 48ff2fa

Browse files
committed
for merge
2 parents 7846e2d + b0cacea commit 48ff2fa

File tree

9 files changed

+345
-156
lines changed

9 files changed

+345
-156
lines changed

README.md

Lines changed: 87 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

65147
https://github.com/emelianov/modbus-esp8266<br>
66148
67149

68-
Original version:
150+
Original version:<br>
69151
http://github.com/andresarmento/modbus-esp8266<br>
70152
prof (at) andresarmento (dot) com
71153

examples/TestCallback/TestCallback.ino

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@
2020
//Modbus Registers Offsets (0-9999)
2121
const int LED_COIL = 100;
2222
//Used Pins
23-
const int ledPin = D4; // Builtin ESP8266 LED
24-
23+
#ifdef ESP8266
24+
const int ledPin = D4; // Builtin ESP8266 LED
25+
#else
26+
const int ledPin = TX; // ESP32 TX LED
27+
#endif
2528
//ModbusIP object
2629
ModbusIP mb;
2730

@@ -50,14 +53,14 @@ void setup() {
5053

5154
Serial.println("");
5255
Serial.println("WiFi connected");
53-
Serial.println("IP address: ");
56+
Serial.print("IP address: ");
5457
Serial.println(WiFi.localIP());
5558

5659
mb.onConnect(cbConn); // Add callback on connection event
5760
mb.begin();
5861

5962
pinMode(ledPin, OUTPUT);
60-
mb.addCoil(LED_COIL); // Add Coil
63+
mb.addReg(COIL(LED_COIL)); // Add Coil. The same as mb.addCoil(COIL_BASE, false, LEN)
6164
mb.onSet(COIL(LED_COIL), cbLed); // Add callback on Coil LED_COIL value set
6265
}
6366

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
Modbus-Arduino Example - Publish multiple DI as coils (Modbus IP ESP8266/ESP32)
3+
4+
Original library
5+
Copyright by André Sarmento Barbosa
6+
http://github.com/andresarmento/modbus-arduino
7+
8+
Current version
9+
(c)2017 Alexander Emelianov ([email protected])
10+
https://github.com/emelianov/modbus-esp8266
11+
*/
12+
13+
#ifdef ESP8266
14+
#include <ESP8266WiFi.h>
15+
#else //ESP32
16+
#include <WiFi.h>
17+
#endif
18+
#include <ModbusIP_ESP8266.h>
19+
20+
//Used Pins
21+
#ifdef ESP8266
22+
uint8_t pinList[] = {D0, D1, D2, D3, D4, D5, D6, D7, D8};
23+
#else //ESP32
24+
uint8_t pinList[] = {12, 13, 14, 14, 16, 17, 18, 21, 22, 23};
25+
#endif
26+
#define LEN sizeof(pinList)/sizeof(uint8_t)
27+
28+
//ModbusIP object
29+
ModbusIP mb;
30+
31+
// Callback function to read corresponding DI
32+
uint16_t cbRead(TRegister* reg, uint16_t val) {
33+
if(reg->address < COIL_BASE)
34+
return 0;
35+
uint8_t offset = reg->address - COIL_BASE;
36+
if(offset >= LEN)
37+
return 0;
38+
return COIL_VAL(digitalRead(pinList[offset]));
39+
}
40+
// Callback function to write-protect DI
41+
uint16_t cbWrite(TRegister* reg, uint16_t val) {
42+
return reg->value;
43+
}
44+
45+
// Callback function for client connect. Returns true to allow connection.
46+
bool cbConn(IPAddress ip) {
47+
Serial.println(ip);
48+
return true;
49+
}
50+
51+
void setup() {
52+
Serial.begin(74880);
53+
54+
WiFi.begin("ssid", "password");
55+
56+
while (WiFi.status() != WL_CONNECTED) {
57+
delay(500);
58+
Serial.print(".");
59+
}
60+
61+
Serial.println("");
62+
Serial.println("WiFi connected");
63+
Serial.print("IP address: ");
64+
Serial.println(WiFi.localIP());
65+
for (uint8_t i = 0; i < LEN; i++)
66+
pinMode(pinList[i], INPUT);
67+
mb.onConnect(cbConn); // Add callback on connection event
68+
mb.begin();
69+
70+
mb.addReg(COIL(COIL_BASE), COIL_VAL(false), LEN); // Add Coils. The same as mb.addCoil(COIL_BASE, false, LEN)
71+
mb.onGet(COIL(COIL_BASE), cbRead, LEN); // Add callback on Coils value get
72+
mb.onSet(COIL(COIL_BASE), cbWrite, LEN);
73+
}
74+
75+
void loop() {
76+
//Call once inside loop() - all magic here
77+
mb.task();
78+
yield();
79+
}

keywords.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ begin KEYWORD2
99
task KEYWORD2
1010
onGet KEYWORD2
1111
onSet KEYWORD2
12-
onConnect KEYWORD2
12+
onConnect KEYWORD2
1313

1414
# Constants (LITERAL1)
1515

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=modbus-esp8266
2-
version=0.1
2+
version=1.1
33
author=Andre Sarmento Barbosa
44
maintainer=Alexander Emelianov<[email protected]>
55
sentence=Modbus Library for ESP8266/ESP32

0 commit comments

Comments
 (0)