@@ -152,8 +152,7 @@ void ModbusIP::task() {
152152
153153uint16_t ModbusIP::send (IPAddress ip, TAddress startreg, cbTransaction cb, uint8_t unit, void * data, bool waitResponse) {
154154#ifdef MODBUSIP_MAX_TRANSACIONS
155- if (_trans.size () >= MODBUSIP_MAX_TRANSACIONS)
156- return false ;
155+ if (this ->_trans .size () >= MODBUSIP_MAX_TRANSACIONS) return false ;
157156#endif
158157 int8_t p = getSlave (ip);
159158 if (p == -1 || !client[p]->connected ())
@@ -171,7 +170,7 @@ uint16_t ModbusIP::send(IPAddress ip, TAddress startreg, cbTransaction cb, uint8
171170 if (client[p]->write (sbuf, send_len) != send_len)
172171 return false ;
173172 client[p]->flush ();
174- if (waitResponse || true ) {
173+ if (waitResponse) {
175174 TTransaction tmp;
176175 tmp.transactionId = transactionId;
177176 tmp.timestamp = millis ();
@@ -194,16 +193,8 @@ void ModbusIP::onDisconnect(cbModbusConnect cb) {
194193 cbDisconnect = cb;
195194}
196195
197- bool ifExpired (TTransaction& t) {
198- if (millis () - t.timestamp > MODBUSIP_TIMEOUT) {
199- // if (t.cb)
200- // t.cb(Modbus::EX_TIMEOUT, t.transactionId, nullptr);
201- // free(t._frame);
202- return true ;
203- }
204- return false ;
205- }
206- void ModbusIP::cleanup () { // Free clients if not connected and remove timedout transactions
196+ void ModbusIP::cleanup () {
197+ // Free clients if not connected
207198 for (uint8_t i = 0 ; i < MODBUSIP_MAX_CLIENTS; i++) {
208199 if (client[i] && !client[i]->connected ()) {
209200 // IPAddress ip = client[i]->remoteIP();
@@ -213,20 +204,19 @@ void ModbusIP::cleanup() { // Free clients if not connected and remove timedout
213204 cbDisconnect (IPADDR_NONE);
214205 }
215206 }
216- // _trans.erase(remove_if( _trans.begin(), _trans.end(), ifExpired ), _trans.end() );
217- std::vector<TTransaction>::iterator it = std::find_if (_trans.begin (), _trans.end (), ifExpired);
218- while (it != _trans.end ()) {
219- if (it->cb )
220- it->cb (Modbus::EX_TIMEOUT, it->transactionId , nullptr );
221- free (it->_frame );
222- _trans.erase (it);
223- it = std::find_if (it, _trans.end (), ifExpired);
207+ // Remove timedout transactions and forced event
208+ for (auto it = _trans.begin (); it != _trans.end ();) {
209+ if (millis () - it->timestamp > MODBUSIP_TIMEOUT || it->forcedEvent != Modbus::EX_SUCCESS) {
210+ Modbus::ResultCode res = (it->forcedEvent != Modbus::EX_SUCCESS)?it->forcedEvent :Modbus::EX_TIMEOUT;
211+ it->cb (res, it->transactionId , nullptr );
212+ free (it->_frame );
213+ it = _trans.erase (it);
214+ } else
215+ it++;
224216 }
225-
226217}
227218
228- int8_t ModbusIP::getFreeClient () { // Returns free slot position
229- // clientsCleanup();
219+ int8_t ModbusIP::getFreeClient () {
230220 for (uint8_t i = 0 ; i < MODBUSIP_MAX_CLIENTS; i++)
231221 if (!client[i])
232222 return i;
@@ -409,17 +399,13 @@ bool ModbusIP::disconnect(IPAddress ip) {
409399}
410400
411401void ModbusIP::dropTransactions () {
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 ();
402+ for (auto &t : _trans) t.forcedEvent = EX_CANCEL;
418403}
419404
420405ModbusIP::~ModbusIP () {
421406 free (_frame);
422407 dropTransactions ();
408+ cleanup ();
423409 for (uint8_t i = 0 ; i < MODBUSIP_MAX_CLIENTS; i++) {
424410 client[i]->stop ();
425411 delete client[i];
0 commit comments