Skip to content

Commit 5e0df95

Browse files
author
gabrielsan
authored
Merge pull request #2 from gabrielsan/gabrielsan-patch-millis
Fixing millis() overflow on timeouts
2 parents 5f3ce55 + 92bae41 commit 5e0df95

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
@@ -498,7 +498,7 @@ void Modbus::setTimeOut( uint16_t u16timeOut)
498498
*/
499499
boolean Modbus::getTimeOutState()
500500
{
501-
return (millis() > u32timeOut);
501+
return ((unsigned long)(millis() -u32timeOut) > (unsigned long)u16timeout);
502502
}
503503

504504
/**
@@ -679,7 +679,7 @@ int8_t Modbus::poll()
679679
else
680680
u8current = softPort->available();
681681

682-
if (millis() > u32timeOut)
682+
if ((unsigned long)(millis() -u32timeOut) > (unsigned long)u16timeout)
683683
{
684684
u8state = COM_IDLE;
685685
u8lastError = NO_REPLY;
@@ -693,10 +693,10 @@ int8_t Modbus::poll()
693693
if (u8current != u8lastRec)
694694
{
695695
u8lastRec = u8current;
696-
u32time = millis() + T35;
696+
u32time = millis();
697697
return 0;
698698
}
699-
if (millis() < u32time) return 0;
699+
if ((unsigned long)(millis() -u32time) > (unsigned long)T35) return 0;
700700

701701
// transfer Serial buffer frame to auBuffer
702702
u8lastRec = 0;
@@ -775,10 +775,10 @@ int8_t Modbus::poll( uint16_t *regs, uint8_t u8size )
775775
if (u8current != u8lastRec)
776776
{
777777
u8lastRec = u8current;
778-
u32time = millis() + T35;
778+
u32time = millis();
779779
return 0;
780780
}
781-
if (millis() < u32time) return 0;
781+
if ((unsigned long)(millis() -u32time) > (unsigned long)T35) return 0;
782782

783783
u8lastRec = 0;
784784
int8_t i8state = getRxBuffer();
@@ -801,7 +801,7 @@ int8_t Modbus::poll( uint16_t *regs, uint8_t u8size )
801801
return u8exception;
802802
}
803803

804-
u32timeOut = millis() + long(u16timeOut);
804+
u32timeOut = millis();
805805
u8lastError = 0;
806806

807807
// process message
@@ -947,7 +947,7 @@ void Modbus::sendTxBuffer()
947947
u8BufferSize = 0;
948948

949949
// set time-out for master
950-
u32timeOut = millis() + (unsigned long) u16timeOut;
950+
u32timeOut = millis();
951951

952952
// increase message counter
953953
u16OutCnt++;

0 commit comments

Comments
 (0)