Skip to content

Commit 2f14138

Browse files
committed
Memory leak, examples and API fixes
- Fix memory leak (issue #208) - Extend cbEnable/cbDisable API - Fix TCP-to-RTU example
1 parent 6600ea5 commit 2f14138

File tree

6 files changed

+28
-16
lines changed

6 files changed

+28
-16
lines changed

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
set(COMPONENT_ADD_INCLUDEDIRS src)
2+
set(COMPONENT_SRCS "src/Modbus.cpp" "src/ModbusRTU.cpp")
3+
set(COMPONENT_PRIV_REQUIRES arduino)
4+
register_component()
5+
6+

examples/Bridge/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,22 @@ struct frame_arg_t {
4242
uint8_t slaveId;
4343
// For ModbusTCP/TLS
4444
struct {
45-
uint8_t unitId; // UnitId as passed in Modbus header
45+
uint8_t unitId; // UnitId as passed in MBAP header
4646
uint32_t ipaddr; // IP address from which frame is received
47-
uint16_t transactionId; // TransactionId as passed im Modbus header
47+
uint16_t transactionId; // TransactionId as passed in MBAP header
4848
};
4949
};
5050
};
5151
typedef std::function<ResultCode(uint8_t*, uint8_t, void*)> cbRaw; // Callback function Type for STL
5252
typedef ResultCode (*cbRaw)(uint8_t* frame, uint8 len, void* data); // Callback function Type
5353
bool onRaw(cbRaw cb = nullptr);
5454
```
55-
- `frame` Modbus payload frame with stripped MBAP, slaveid and crc
55+
- `frame` Modbus payload frame with stripped MBAP/slaveid and crc
5656
- `len` frame size in bytes
5757
- `data` Pointer to frame_arg_t filled with frame header information
58+
5859
*Returns:*
59-
- If a special error code `Modbus::EX_PASSTHROU` returned frame will be processed normally
60+
- If a special error code `Modbus::EX_PASSTHROUGH` returned frame will be processed normally
6061
- If a special error code `Modbus::EX_FORCE_PROCESS` returned frame will be processed even if addressed to another Modbus unit
6162
- Any other return code disables normal frame processing. Only transactional callback will be executed (if any and transaction data is correct)
6263
The callback is executed only on Modbus frame with valid header and CRC.

examples/Bridge/TCP-to-RTU-Simulator/TCP-to-RTU-Simulator.ino

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,24 +58,24 @@ Modbus::ResultCode cbTcpRaw(uint8_t* data, uint8_t len, void* custom) {
5858

5959
if (transRunning) { // Note that we can't process new requests from TCP-side while waiting for responce from RTU-side.
6060
tcp.setTransactionId(transRunning); // Set transaction id as per incoming request
61-
tcp.errorResponce(src->ipaddr, (Modbus::FunctionCode)data[0], Modbus::EX_SLAVE_DEVICE_BUSY);
61+
tcp.errorResponce(IPAddress((src->ipaddr), (Modbus::FunctionCode)data[0], Modbus::EX_SLAVE_DEVICE_BUSY);
6262
return Modbus::EX_SLAVE_DEVICE_BUSY;
6363
}
6464

65-
rtu.rawRequest(slaveRunning, data, len, cbRtuTrans);
65+
rtu.rawRequest(src->unitId, data, len, cbRtuTrans);
6666

6767
if (src->unitId) {
6868
tcp.setTransactionId(transRunning); // Set transaction id as per incoming request
6969

7070
//uint16_t succeed = tcp.rawResponce(src->ipaddr, data, len, slaveRunning);
7171

72-
tcp.errorResponce(src->ipaddr, (Modbus::FunctionCode)data[0], Modbus::EX_ACKNOWLEDGE);
72+
tcp.errorResponce(IPAddress(src->ipaddr), (Modbus::FunctionCode)data[0], Modbus::EX_ACKNOWLEDGE);
7373
return Modbus::EX_ACKNOWLEDGE;
7474
}
7575

76-
srcIp = src->ipaddr;
76+
srcIp = IPAddress(src->ipaddr);
7777

78-
slaveRunning = src->slaveId;
78+
slaveRunning = src->unitId;
7979

8080
transRunning = src->transactionId;
8181

src/Modbus.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -824,11 +824,13 @@ void Modbus::masterPDU(uint8_t* frame, uint8_t* sourceFrame, TAddress startreg,
824824
}
825825
}
826826

827-
void Modbus::cbEnable(bool state) {
827+
bool Modbus::cbEnable(const bool state) {
828+
const bool old_state = state;
828829
cbEnabled = state;
830+
return old_state;
829831
}
830-
void Modbus::cbDisable() {
831-
cbEnable(false);
832+
bool Modbus::cbDisable() {
833+
return cbEnable(false);
832834
}
833835
Modbus::~Modbus() {
834836
free(_frame);

src/Modbus.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ class Modbus {
193193

194194
~Modbus();
195195

196-
void cbEnable(bool state = true);
197-
void cbDisable();
196+
bool cbEnable(const bool state = true);
197+
bool cbDisable();
198198

199199
private:
200200
ResultCode readBits(TAddress startreg, uint16_t numregs, FunctionCode fn);

src/ModbusTCPTemplate.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,10 @@ void ModbusTCPTemplate<SERVER, CLIENT>::task() {
219219
Serial.println("IP: Accepted");
220220
#endif
221221
CLIENT* currentClient = new CLIENT(c);
222-
if (!currentClient || !currentClient->connected())
222+
if (!currentClient || !currentClient->connected()) {
223+
delete currentClient;
223224
continue;
225+
}
224226
#if defined(MODBUSRTU_DEBUG)
225227
Serial.println("IP: Connected");
226228
#endif
@@ -544,7 +546,7 @@ bool ModbusTCPTemplate<SERVER, CLIENT>::disconnect(IPAddress ip) {
544546
return false;
545547
int8_t p = getSlave(ip);
546548
if (p != -1) {
547-
tcpclient[p]->stop();
549+
//tcpclient[p]->stop();
548550
delete tcpclient[p];
549551
tcpclient[p] = nullptr;
550552
return true;
@@ -565,6 +567,7 @@ void ModbusTCPTemplate<SERVER, CLIENT>::dropTransactions() {
565567
template <class SERVER, class CLIENT>
566568
ModbusTCPTemplate<SERVER, CLIENT>::~ModbusTCPTemplate() {
567569
free(_frame);
570+
_frame = nullptr;
568571
dropTransactions();
569572
cleanupConnections();
570573
cleanupTransactions();

0 commit comments

Comments
 (0)