Skip to content

Commit e909c88

Browse files
committed
Disconnect initial implementation
1 parent e5c7341 commit e909c88

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

src/Modbus.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ class Modbus {
127127
EX_DATA_MISMACH = 0xE2, // Custom. Inpud data size mismach
128128
EX_UNEXPECTED_RESPONSE = 0xE3, // Custom. Returned result doesn't mach transaction
129129
EX_TIMEOUT = 0xE4, // Custom. Operation not finished within reasonable time
130-
EX_CONNECTION_LOST = 0xE5 // Custom. Connection with device lost
130+
EX_CONNECTION_LOST = 0xE5, // Custom. Connection with device lost
131+
EX_CANCEL = 0xE6 // Custom. Transaction/request canceled
131132
};
132133
~Modbus();
133134
bool addHreg(uint16_t offset, uint16_t value = 0, uint16_t numregs = 1);

src/ModbusIP_ESP8266.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,10 @@ void ModbusIP::task() {
142142
// Prepare and send ModbusIP frame. _frame buffer should be filled with Modbus data
143143
uint16_t ModbusIP::send(IPAddress ip, TAddress startreg, cbTransaction cb, uint8_t unit, void* data, bool waitResponse) {
144144
#ifdef MODBUSIP_MAX_TRANSACIONS
145-
if (_trans.size() >= MODBUSIP_MAX_TRANSACIONS)
145+
if (_trans.size() >= MODBUSIP_MAX_TRANSACIONS) {
146+
Serial.println(_trans.size());
146147
return false;
148+
}
147149
#endif
148150
int8_t p = getSlave(ip);
149151
if (p == -1 || !client[p]->connected())
@@ -160,7 +162,8 @@ uint16_t ModbusIP::send(IPAddress ip, TAddress startreg, cbTransaction cb, uint8
160162
memcpy(sbuf + sizeof(_MBAP.raw), _frame, _len);
161163
if (client[p]->write(sbuf, send_len) != send_len)
162164
return false;
163-
if (waitResponse) {
165+
client[p]->flush();
166+
if (waitResponse || true) {
164167
TTransaction tmp;
165168
tmp.transactionId = transactionId;
166169
tmp.timestamp = millis();
@@ -390,4 +393,13 @@ bool ModbusIP::isConnected(IPAddress ip) {
390393

391394
void ModbusIP::autoConnect(bool enabled) {
392395
autoConnectMode = enabled;
396+
}
397+
398+
bool ModbusIP::disconnect(IPAddress ip) {
399+
int8_t p = getSlave(ip);
400+
if (p != -1) client[p]->stop();
401+
}
402+
403+
void ModbusIP::dropTransactions() {
404+
_trans.resize(0);
393405
}

src/ModbusIP_ESP8266.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ typedef struct TTransaction {
3232
uint8_t* _frame = nullptr;
3333
void* data = nullptr;
3434
TAddress startreg;
35-
bool operator ==(const TTransaction &obj) const {
35+
bool operator ==(const TTransaction &obj) const {
3636
return transactionId == obj.transactionId;
3737
}
3838
};
@@ -70,7 +70,7 @@ class ModbusIP : public Modbus {
7070
bool isTransaction(uint16_t id);
7171
bool isConnected(IPAddress ip);
7272
bool connect(IPAddress ip);
73-
bool disconnect(IPAddress addr) {} // Not implemented yet
73+
bool disconnect(IPAddress ip);
7474
void slave();
7575
void master();
7676
void task();
@@ -79,8 +79,9 @@ class ModbusIP : public Modbus {
7979
void onDisconnect(cbModbusConnect cb = nullptr);
8080
IPAddress eventSource();
8181
void autoConnect(bool enabled = true);
82+
void dropTransactions();
8283

83-
uint16_t writeCoil(IPAddress ip, uint16_t offset, bool value, cbTransaction cb = nullptr, uint8_t unit = MODBUSIP_UNIT);
84+
uint16_t writeCoil(IPAddress ip, uint16_t offset, bool value, cbTransaction cb = nullptr, uint8_t unit = MODBUSIP_UNIT);
8485
uint16_t writeHreg(IPAddress ip, uint16_t offset, uint16_t value, cbTransaction cb = nullptr, uint8_t unit = MODBUSIP_UNIT);
8586
uint16_t writeCoil(IPAddress ip, uint16_t offset, bool* value, uint16_t numregs = 1, cbTransaction cb = nullptr, uint8_t unit = MODBUSIP_UNIT);
8687
uint16_t writeHreg(IPAddress ip, uint16_t offset, uint16_t* value, uint16_t numregs = 1, cbTransaction cb = nullptr, uint8_t unit = MODBUSIP_UNIT);

0 commit comments

Comments
 (0)