Skip to content

Commit 92bae41

Browse files
author
gabrielsan
authored
Fixing millis() overflow on timeouts
Eliminating millis() overflow limitation by keeping millis() and using a cast to unsigned long on the comparison
1 parent ebf5036 commit 92bae41

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

ModbusRtu.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ void Modbus::setTimeOut( uint16_t u16timeOut)
481481
*/
482482
boolean Modbus::getTimeOutState()
483483
{
484-
return (millis() > u32timeOut);
484+
return ((unsigned long)(millis() -u32timeOut) > (unsigned long)u16timeout);
485485
}
486486

487487
/**
@@ -662,7 +662,7 @@ int8_t Modbus::poll()
662662
else
663663
u8current = softPort->available();
664664

665-
if (millis() > u32timeOut)
665+
if ((unsigned long)(millis() -u32timeOut) > (unsigned long)u16timeout)
666666
{
667667
u8state = COM_IDLE;
668668
u8lastError = NO_REPLY;
@@ -676,10 +676,10 @@ int8_t Modbus::poll()
676676
if (u8current != u8lastRec)
677677
{
678678
u8lastRec = u8current;
679-
u32time = millis() + T35;
679+
u32time = millis();
680680
return 0;
681681
}
682-
if (millis() < u32time) return 0;
682+
if ((unsigned long)(millis() -u32time) > (unsigned long)T35) return 0;
683683

684684
// transfer Serial buffer frame to auBuffer
685685
u8lastRec = 0;
@@ -758,10 +758,10 @@ int8_t Modbus::poll( uint16_t *regs, uint8_t u8size )
758758
if (u8current != u8lastRec)
759759
{
760760
u8lastRec = u8current;
761-
u32time = millis() + T35;
761+
u32time = millis();
762762
return 0;
763763
}
764-
if (millis() < u32time) return 0;
764+
if ((unsigned long)(millis() -u32time) > (unsigned long)T35) return 0;
765765

766766
u8lastRec = 0;
767767
int8_t i8state = getRxBuffer();
@@ -784,7 +784,7 @@ int8_t Modbus::poll( uint16_t *regs, uint8_t u8size )
784784
return u8exception;
785785
}
786786

787-
u32timeOut = millis() + long(u16timeOut);
787+
u32timeOut = millis();
788788
u8lastError = 0;
789789

790790
// process message
@@ -974,7 +974,7 @@ void Modbus::sendTxBuffer()
974974
u8BufferSize = 0;
975975

976976
// set time-out for master
977-
u32timeOut = millis() + (unsigned long) u16timeOut;
977+
u32timeOut = millis();
978978

979979
// increase message counter
980980
u16OutCnt++;

0 commit comments

Comments
 (0)