@@ -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
150153uint16_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}
396397bool ModbusIP::isConnected (IPAddress ip) {
@@ -408,5 +409,19 @@ bool ModbusIP::disconnect(IPAddress ip) {
408409}
409410
410411void 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}
0 commit comments