Skip to content

Commit ea5ef95

Browse files
committed
added packet timeout
1 parent ef550c8 commit ea5ef95

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/Packet.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,27 @@ uint8_t Packet::constructPacket(const uint16_t& messageLen, const uint8_t packet
114114
-------
115115
* uint8_t - Num bytes in RX buffer
116116
*/
117+
#define PACKET_TIMEOUT 1000;
117118
uint8_t Packet::parse(uint8_t recChar, bool valid)
118119
{
120+
bool packet_fresh = packetStart==0 || millis()-packetStart<PACKET_TIMEOUT;
121+
if(!packet_fresh){
122+
123+
bytesRead = 0;
124+
state = find_start_byte;
125+
packetStart=0;
126+
return bytesRead;
127+
}
119128
if (valid)
120129
{
121130
switch (state)
122131
{
123132
case find_start_byte: /////////////////////////////////////////
124133
{
125-
if (recChar == START_BYTE)
134+
if (recChar == START_BYTE){
126135
state = find_id_byte;
136+
packetStart=millis();
137+
}
127138
break;
128139
}
129140

src/Packet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ class Packet
171171
uint8_t idByte = 0;
172172
uint8_t overheadByte = 0;
173173
uint8_t recOverheadByte = 0;
174-
174+
uint32_t packetStart = 0;
175175

176176
void calcOverhead(uint8_t arr[], const uint8_t& len);
177177
int16_t findLast(uint8_t arr[], const uint8_t& len);

0 commit comments

Comments
 (0)