Skip to content

Commit 3458920

Browse files
authored
Merge pull request #64 from brunojoyal/master
Added timeout parameter to Packet and SerialTransfer class
2 parents ef550c8 + cbc39fc commit 3458920

File tree

5 files changed

+45
-5
lines changed

5 files changed

+45
-5
lines changed

examples/uart_rx_file/uart_rx_file.ino

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ void setup()
1414
Serial1.begin(115200);
1515

1616
myTransfer.begin(Serial1);
17+
18+
/*
19+
Or, use the full constructor:
20+
myTransfer.begin(Serial1, true, Serial, 50);
21+
With the timeout parameter set to 50ms, a packet must be fully received and parsed within 50ms,
22+
or it will be discarded.
23+
The timeout value should depend on the baud rate and on the application.
24+
Example back-of-the-envelope calculation:
25+
115200bps = 14400Bps
26+
One packet = 264B (max) should take max 264/11400 s = 0.02s = 20ms
27+
to transfer. Include some time for parsing the packet (which depends on the frequency
28+
of whatever task is calling transfer.available()) - and 50ms does not sound unreasonable.
1729
}
1830
1931

src/Packet.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ void Packet::begin(const configST configs)
2323
debug = configs.debug;
2424
callbacks = configs.callbacks;
2525
callbacksLen = configs.callbacksLen;
26+
timeout = configs.timeout;
2627

27-
// need to give the debug port a kick to get things working for some strange reason...
28-
debugPort->println();
2928
}
3029

3130

@@ -47,8 +46,15 @@ void Packet::begin(const bool _debug, Stream& _debugPort)
4746
{
4847
debugPort = &_debugPort;
4948
debug = _debug;
49+
timeout = __UINT32_MAX__;
5050
}
5151

52+
void Packet::begin(const bool _debug, Stream& _debugPort, uint32_t _timeout)
53+
{
54+
debugPort = &_debugPort;
55+
debug = _debug;
56+
timeout = _timeout;
57+
}
5258

5359
/*
5460
uint8_t Packet::constructPacket(const uint16_t &messageLen, const uint8_t packetID)
@@ -114,16 +120,27 @@ uint8_t Packet::constructPacket(const uint16_t& messageLen, const uint8_t packet
114120
-------
115121
* uint8_t - Num bytes in RX buffer
116122
*/
123+
117124
uint8_t Packet::parse(uint8_t recChar, bool valid)
118125
{
126+
bool packet_fresh = packetStart==0 || millis()-packetStart<timeout;
127+
if(!packet_fresh){ //packet is stale, start over.
128+
debugPort->println("STALE PACKET");
129+
bytesRead = 0;
130+
state = find_start_byte;
131+
packetStart=0;
132+
return bytesRead;
133+
}
119134
if (valid)
120135
{
121136
switch (state)
122137
{
123138
case find_start_byte: /////////////////////////////////////////
124139
{
125-
if (recChar == START_BYTE)
140+
if (recChar == START_BYTE){
126141
state = find_id_byte;
142+
packetStart=millis();
143+
}
127144
break;
128145
}
129146

src/Packet.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ struct configST
4141
bool debug = true;
4242
const functionPtr* callbacks = NULL;
4343
uint8_t callbacksLen = 0;
44+
uint32_t timeout = __UINT32_MAX__;
4445
};
4546

4647

@@ -58,6 +59,7 @@ class Packet
5859

5960
void begin(const configST configs);
6061
void begin(const bool _debug = true, Stream& _debugPort = Serial);
62+
void begin(const bool _debug, Stream& _debugPort, uint32_t _timeout);
6163
uint8_t constructPacket(const uint16_t& messageLen, const uint8_t packetID = 0);
6264
uint8_t parse(uint8_t recChar, bool valid = true);
6365
uint8_t currentPacketID();
@@ -171,8 +173,8 @@ class Packet
171173
uint8_t idByte = 0;
172174
uint8_t overheadByte = 0;
173175
uint8_t recOverheadByte = 0;
174-
175-
176+
uint32_t packetStart = 0;
177+
uint32_t timeout;
176178
void calcOverhead(uint8_t arr[], const uint8_t& len);
177179
int16_t findLast(uint8_t arr[], const uint8_t& len);
178180
void stuffPacket(uint8_t arr[], const uint8_t& len);

src/SerialTransfer.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ void SerialTransfer::begin(Stream& _port, const bool _debug, Stream& _debugPort)
4242
packet.begin(_debug, _debugPort);
4343
}
4444

45+
void SerialTransfer::begin(Stream& _port, const bool _debug, Stream& _debugPort, uint32_t _timeout)
46+
{
47+
port = &_port;
48+
timeout = _timeout;
49+
packet.begin(_debug, _debugPort, _timeout);
50+
}
51+
4552

4653
/*
4754
uint8_t SerialTransfer::sendData(const uint16_t &messageLen, const uint8_t packetID)

src/SerialTransfer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class SerialTransfer
1313

1414
void begin(Stream& _port, const configST configs);
1515
void begin(Stream& _port, const bool _debug = true, Stream& _debugPort = Serial);
16+
void begin(Stream& _port, const bool _debug, Stream& _debugPort, uint32_t _timeout);
1617
uint8_t sendData(const uint16_t& messageLen, const uint8_t packetID = 0);
1718
uint8_t available();
1819
bool tick();
@@ -97,4 +98,5 @@ class SerialTransfer
9798

9899
private: // <<---------------------------------------//private
99100
Stream* port;
101+
uint32_t timeout;
100102
};

0 commit comments

Comments
 (0)