Skip to content

Commit 604d170

Browse files
committed
2.1 BETA
1 parent 20a78c5 commit 604d170

File tree

6 files changed

+47
-23
lines changed

6 files changed

+47
-23
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,19 @@ http://www.modbus.org/docs/Modbus_over_serial_line_V1_02.pdf
7373
// 2.0.1
7474
+ Fix readCoil\Hreg\Ists\Ireg not read value from slave
7575
+ Fix cresh on disconnect with Arduino Core 2.5.x
76-
// ToDo for 2.0.2
77-
- modify examples
78-
- code cleanup
76+
// 2.1.0
77+
+ dropTransactions()
78+
+ Fix slave error response on write multiple Hreg\Coils
79+
+ Implement disconnect()
80+
- Modify slave task() for high query rate
81+
- Create destructor for ModbusIP
82+
- Modify examples
7983
// ToDo later
84+
- code cleanup
8085
- ModbusSerial (over RS-485)
8186
- Modbus Read/Write File Records function
8287
- Modbus Write Mask Register function
8388
- Modbus Serial line-specific functions
84-
- Create destructor for ModbusIP
8589
```
8690

8791
## Contributions

keywords.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ removeIreg KEYWORD2
6363
removeCoil KEYWORD2
6464
removeIsts KEYWORD2
6565
autoConnect KEYWORD2
66+
disconnect KEYWORD2
67+
dropTransactions KEYWORD2
6668

6769
# Constants and Macros (LITERAL1)
6870
BIT_VAL LITERAL1
@@ -88,4 +90,5 @@ EX_DATA_MISMACH LITERAL1
8890
EX_UNEXPECTED_RESPONSE LITERAL1
8991
EX_TIMEOUT LITERAL1
9092
EX_CONNECTION_LOST LITERAL1
93+
EX_CANCEL LITERAL1
9194

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=2.0.1
2+
version=2.1.0
33
author=Andre Sarmento Barbosa
44
maintainer=Alexander Emelianov<[email protected]>
55
sentence=Modbus Master-Slave Library for ESP8266/ESP32

src/Modbus.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ void Modbus::bitsToBool(bool* dst, uint8_t* src, uint16_t numregs) {
469469
//1 void Modbus::masterPDU(uint8_t* frame, uint8_t* sourceFrame, void* output) {
470470
void Modbus::masterPDU(uint8_t* frame, uint8_t* sourceFrame, TAddress startreg, void* output) {
471471
uint8_t fcode = frame[0];
472-
_reply = 0;
472+
_reply = EX_SUCCESS;
473473
if ((fcode & 0x80) != 0) {
474474
_reply = _frame[1];
475475
return;

src/ModbusIP_ESP8266.cpp

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,28 +73,28 @@ void ModbusIP::task() {
7373
if (!client[n]->connected()) continue;
7474
if (client[n]->available() < sizeof(_MBAP) + 1) continue;
7575

76-
client[n]->readBytes(_MBAP.raw, sizeof(_MBAP.raw)); //Get MBAP
77-
_len = __bswap_16(_MBAP.length);
78-
_len--; // Do not count with last byte from MBAP
76+
client[n]->readBytes(_MBAP.raw, sizeof(_MBAP.raw)); // Get MBAP
7977

80-
if (__bswap_16(_MBAP.protocolId) != 0) { //Check if MODBUSIP packet. __bswap is usless there.
78+
if (__bswap_16(_MBAP.protocolId) != 0) { // Check if MODBUSIP packet. __bswap is usless there.
8179
client[n]->readBytes((uint8_t*)nullptr, client[n]->available());
82-
client[n]->flush();
80+
//client[n]->flush();
8381
continue; // for (n)
8482
}
85-
if (_len > MODBUSIP_MAXFRAME) { //Length is over MODBUSIP_MAXFRAME
83+
_len = __bswap_16(_MBAP.length);
84+
_len--; // Do not count with last byte from MBAP
85+
if (_len > MODBUSIP_MAXFRAME) { // Length is over MODBUSIP_MAXFRAME
8686
exceptionResponse((FunctionCode)client[n]->read(), EX_SLAVE_FAILURE);
87-
client[n]->readBytes((uint8_t*)nullptr, client[n]->available());
87+
client[n]->readBytes((uint8_t*)nullptr, _len);
8888
//client[n]->flush();
8989
} else {
9090
free(_frame);
9191
_frame = (uint8_t*) malloc(_len);
9292
if (!_frame) {
9393
exceptionResponse((FunctionCode)client[n]->read(), EX_SLAVE_FAILURE);
94-
client[n]->readBytes((uint8_t*)nullptr, client[n]->available());
94+
client[n]->readBytes((uint8_t*)nullptr, _len);
9595
//client[n]->flush();
9696
} else {
97-
if (client[n]->readBytes(_frame, _len) < _len) { //Try to read MODBUS frame
97+
if (client[n]->readBytes(_frame, _len) < _len) { // Try to read MODBUS frame
9898
exceptionResponse((FunctionCode)_frame[0], EX_ILLEGAL_VALUE);
9999
client[n]->readBytes((uint8_t*)nullptr, client[n]->available());
100100
//client[n]->flush();
@@ -128,9 +128,9 @@ void ModbusIP::task() {
128128
}
129129
}
130130
}
131-
if (client[n]->localPort() != MODBUSIP_PORT) _reply = REPLY_OFF; // No replay if it was request to master
131+
if (client[n]->localPort() != MODBUSIP_PORT) _reply = REPLY_OFF; // No replay if it was responce to master
132132
if (_reply != REPLY_OFF) {
133-
_MBAP.length = __bswap_16(_len+1); //_len+1 for last byte from MBAP
133+
_MBAP.length = __bswap_16(_len+1); // _len+1 for last byte from MBAP
134134
size_t send_len = (uint16_t)_len + sizeof(_MBAP.raw);
135135
uint8_t sbuf[send_len];
136136
memcpy(sbuf, _MBAP.raw, sizeof(_MBAP.raw));
@@ -142,17 +142,18 @@ void ModbusIP::task() {
142142
free(_frame);
143143
_frame = nullptr;
144144
_len = 0;
145+
//n--;
145146
}
147+
//for (n = 0; n < MODBUSIP_MAX_CLIENTS; n++)
148+
// if (client[n] && client[n]->connected())
149+
// client[n]->flush();
146150
n = -1;
147151
}
148152

149-
// Prepare and send ModbusIP frame. _frame buffer should be filled with Modbus data
150153
uint16_t ModbusIP::send(IPAddress ip, TAddress startreg, cbTransaction cb, uint8_t unit, void* data, bool waitResponse) {
151154
#ifdef MODBUSIP_MAX_TRANSACIONS
152-
if (_trans.size() >= MODBUSIP_MAX_TRANSACIONS) {
153-
Serial.println(_trans.size());
155+
if (_trans.size() >= MODBUSIP_MAX_TRANSACIONS)
154156
return false;
155-
}
156157
#endif
157158
int8_t p = getSlave(ip);
158159
if (p == -1 || !client[p]->connected())
@@ -390,7 +391,7 @@ uint16_t ModbusIP::pullCoilToIsts(IPAddress ip, uint16_t from, uint16_t to, uint
390391
return send(ip, ISTS(to), cb, unit);
391392
}
392393

393-
bool ModbusIP::isTransaction(uint16_t id) { // Check if transaction is in progress (by ID)
394+
bool ModbusIP::isTransaction(uint16_t id) {
394395
return searchTransaction(id) != nullptr;
395396
}
396397
bool ModbusIP::isConnected(IPAddress ip) {
@@ -408,5 +409,19 @@ bool ModbusIP::disconnect(IPAddress ip) {
408409
}
409410

410411
void ModbusIP::dropTransactions() {
411-
_trans.resize(0);
412+
for (auto &t : _trans) {
413+
if (t.cb)
414+
t.cb(EX_CANCEL, t.transactionId, nullptr);
415+
free(t._frame);
416+
}
417+
_trans.clear();
418+
}
419+
420+
ModbusIP::~ModbusIP() {
421+
free(_frame);
422+
dropTransactions();
423+
for (uint8_t i = 0; i < MODBUSIP_MAX_CLIENTS; i++) {
424+
client[i]->stop();
425+
delete client[i];
426+
}
412427
}

src/ModbusIP_ESP8266.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,15 @@ class ModbusIP : public Modbus {
6464
int8_t getSlave(IPAddress ip);
6565
int8_t getMaster(IPAddress ip);
6666
uint16_t send(IPAddress ip, TAddress startreg, cbTransaction cb, uint8_t unit = MODBUSIP_UNIT, void* data = nullptr, bool waitResponse = true);
67+
// Prepare and send ModbusIP frame. _frame buffer and _len should be filled with Modbus data
6768
// ip - slave ip address
6869
// startreg - first local register to save returned data to (miningless for write to slave operations)
6970
// cb - transaction callback function
7071
// unit - slave modbus unit id
7172
// data - if not null use buffer to save returned data instead of local registers
7273
public:
7374
ModbusIP();
75+
~ModbusIP();
7476
bool isTransaction(uint16_t id);
7577
bool isConnected(IPAddress ip);
7678
bool connect(IPAddress ip);

0 commit comments

Comments
 (0)