Skip to content

Commit 7273f82

Browse files
committed
Server: Fix callback called twice on register read
1 parent ac78bff commit 7273f82

File tree

5 files changed

+15
-16
lines changed

5 files changed

+15
-16
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ V1.02](http://www.modbus.org/docs/Modbus_over_serial_line_V1_02.pdf)
6363
## Last Changes
6464

6565
```diff
66+
// 3.0.6
67+
+ Slave/Server: Fix callback called twice on register read
6668
// 3.0.5
6769
+ ModbusRTU: Fix early bus release if no callback
6870
+ ModbusRTU ESP32: Fix potential responce loss

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=3.0.5
2+
version=3.0.6
33
author=Andre Sarmento Barbosa, Alexander Emelianov
44
maintainer=Alexander Emelianov<a.m.emelianov@gmail.com>
55
sentence=Modbus RTU and Modbus TCP Library for ESP8266/ESP32

src/Modbus.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ void Modbus::getMultipleBits(uint8_t* frame, TAddress startreg, uint16_t numregs
224224

225225
void Modbus::getMultipleWords(uint16_t* frame, TAddress startreg, uint16_t numregs) {
226226
for (uint8_t i = 0; i < numregs; i++) {
227-
frame[i] = __bswap_16(Reg(startreg + i));
227+
frame[i] = __swap_16(Reg(startreg + i));
228228
}
229229
}
230230

@@ -298,7 +298,7 @@ void Modbus::setMultipleBits(uint8_t* frame, TAddress startreg, uint16_t numoutp
298298

299299
void Modbus::setMultipleWords(uint16_t* frame, TAddress startreg, uint16_t numregs) {
300300
for (uint8_t i = 0; i < numregs; i++) {
301-
Reg(startreg + i, __bswap_16(frame[i]));
301+
Reg(startreg + i, __swap_16(frame[i]));
302302
}
303303
}
304304

@@ -405,7 +405,7 @@ bool Modbus::writeSlaveWords(TAddress startreg, uint16_t to, uint16_t numregs, F
405405
if (data) {
406406
uint16_t* frame = (uint16_t*)(_frame + 6);
407407
for (uint8_t i = 0; i < numregs; i++) {
408-
frame[i] = __bswap_16(data[i]);
408+
frame[i] = __swap_16(data[i]);
409409
}
410410
} else {
411411
getMultipleWords((uint16_t*)(_frame + 6), startreg, numregs);
@@ -469,7 +469,7 @@ void Modbus::masterPDU(uint8_t* frame, uint8_t* sourceFrame, TAddress startreg,
469469
if (output) {
470470
frame += 2;
471471
while(field2) {
472-
*((uint16_t*)output) = __bswap_16(*((uint16_t*)frame));
472+
*((uint16_t*)output) = __swap_16(*((uint16_t*)frame));
473473
frame += 2;
474474
output += 2;
475475
field2--;

src/Modbus.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212
#include <byteswap.h>
1313
#endif
1414

15-
#ifndef __bswap_16
16-
#define __bswap_16(num) (((uint16_t)num>>8) | ((uint16_t)num<<8))
17-
#endif
18-
15+
static inline uint16_t __swap_16(uint16_t num) { return (num >> 8) | (num << 8); }
1916

2017
#define MB_GLOBAL_REGS
2118
//#define MB_MAX_REGS 32

src/ModbusIP_ESP8266.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ void ModbusIP::task() {
8181
while (millis() - readStart < MODBUSIP_MAX_READMS && tcpclient[n]->available() > sizeof(_MBAP)) {
8282
tcpclient[n]->readBytes(_MBAP.raw, sizeof(_MBAP.raw)); // Get MBAP
8383

84-
if (__bswap_16(_MBAP.protocolId) != 0) { // Check if MODBUSIP packet. __bswap is usless there.
84+
if (__swap_16(_MBAP.protocolId) != 0) { // Check if MODBUSIP packet. __bswap is usless there.
8585
while (tcpclient[n]->available()) // Drop all incoming if wrong packet
8686
tcpclient[n]->read();
8787
continue;
8888
}
89-
_len = __bswap_16(_MBAP.length);
89+
_len = __swap_16(_MBAP.length);
9090
_len--; // Do not count with last byte from MBAP
9191
if (_len > MODBUSIP_MAXFRAME) { // Length is over MODBUSIP_MAXFRAME
9292
exceptionResponse((FunctionCode)tcpclient[n]->read(), EX_SLAVE_FAILURE);
@@ -112,7 +112,7 @@ void ModbusIP::task() {
112112
} else {
113113
// Process reply to master request
114114
_reply = EX_SUCCESS;
115-
TTransaction* trans = searchTransaction(__bswap_16(_MBAP.transactionId));
115+
TTransaction* trans = searchTransaction(__swap_16(_MBAP.transactionId));
116116
if (trans) { // if valid transaction id
117117
if ((_frame[0] & 0x7F) == trans->_frame[0]) { // Check if function code the same as requested
118118
// Procass incoming frame as master
@@ -135,7 +135,7 @@ void ModbusIP::task() {
135135
}
136136
if (tcpclient[n]->localPort() != serverPort) _reply = REPLY_OFF; // No replay if it was responce to master
137137
if (_reply != REPLY_OFF) {
138-
_MBAP.length = __bswap_16(_len+1); // _len+1 for last byte from MBAP
138+
_MBAP.length = __swap_16(_len+1); // _len+1 for last byte from MBAP
139139
size_t send_len = (uint16_t)_len + sizeof(_MBAP.raw);
140140
uint8_t sbuf[send_len];
141141
memcpy(sbuf, _MBAP.raw, sizeof(_MBAP.raw));
@@ -164,9 +164,9 @@ uint16_t ModbusIP::send(IPAddress ip, TAddress startreg, cbTransaction cb, uint8
164164
return autoConnectMode?connect(ip):false;
165165
transactionId++;
166166
if (!transactionId) transactionId = 1;
167-
_MBAP.transactionId = __bswap_16(transactionId);
168-
_MBAP.protocolId = __bswap_16(0);
169-
_MBAP.length = __bswap_16(_len+1); //_len+1 for last byte from MBAP
167+
_MBAP.transactionId = __swap_16(transactionId);
168+
_MBAP.protocolId = __swap_16(0);
169+
_MBAP.length = __swap_16(_len+1); //_len+1 for last byte from MBAP
170170
_MBAP.unitId = unit;
171171
size_t send_len = _len + sizeof(_MBAP.raw);
172172
uint8_t sbuf[send_len];

0 commit comments

Comments
 (0)