Skip to content

Commit

Permalink
Merge pull request #64 from brunojoyal/master
Browse files Browse the repository at this point in the history
Added timeout parameter to Packet and SerialTransfer class
  • Loading branch information
PowerBroker2 authored Apr 20, 2021
2 parents ef550c8 + cbc39fc commit 3458920
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
12 changes: 12 additions & 0 deletions examples/uart_rx_file/uart_rx_file.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ void setup()
Serial1.begin(115200);

myTransfer.begin(Serial1);

/*
Or, use the full constructor:
myTransfer.begin(Serial1, true, Serial, 50);
With the timeout parameter set to 50ms, a packet must be fully received and parsed within 50ms,
or it will be discarded.
The timeout value should depend on the baud rate and on the application.
Example back-of-the-envelope calculation:
115200bps = 14400Bps
One packet = 264B (max) should take max 264/11400 s = 0.02s = 20ms
to transfer. Include some time for parsing the packet (which depends on the frequency
of whatever task is calling transfer.available()) - and 50ms does not sound unreasonable.
}
Expand Down
23 changes: 20 additions & 3 deletions src/Packet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ void Packet::begin(const configST configs)
debug = configs.debug;
callbacks = configs.callbacks;
callbacksLen = configs.callbacksLen;
timeout = configs.timeout;

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


Expand All @@ -47,8 +46,15 @@ void Packet::begin(const bool _debug, Stream& _debugPort)
{
debugPort = &_debugPort;
debug = _debug;
timeout = __UINT32_MAX__;
}

void Packet::begin(const bool _debug, Stream& _debugPort, uint32_t _timeout)
{
debugPort = &_debugPort;
debug = _debug;
timeout = _timeout;
}

/*
uint8_t Packet::constructPacket(const uint16_t &messageLen, const uint8_t packetID)
Expand Down Expand Up @@ -114,16 +120,27 @@ uint8_t Packet::constructPacket(const uint16_t& messageLen, const uint8_t packet
-------
* uint8_t - Num bytes in RX buffer
*/

uint8_t Packet::parse(uint8_t recChar, bool valid)
{
bool packet_fresh = packetStart==0 || millis()-packetStart<timeout;
if(!packet_fresh){ //packet is stale, start over.
debugPort->println("STALE PACKET");
bytesRead = 0;
state = find_start_byte;
packetStart=0;
return bytesRead;
}
if (valid)
{
switch (state)
{
case find_start_byte: /////////////////////////////////////////
{
if (recChar == START_BYTE)
if (recChar == START_BYTE){
state = find_id_byte;
packetStart=millis();
}
break;
}

Expand Down
6 changes: 4 additions & 2 deletions src/Packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ struct configST
bool debug = true;
const functionPtr* callbacks = NULL;
uint8_t callbacksLen = 0;
uint32_t timeout = __UINT32_MAX__;
};


Expand All @@ -58,6 +59,7 @@ class Packet

void begin(const configST configs);
void begin(const bool _debug = true, Stream& _debugPort = Serial);
void begin(const bool _debug, Stream& _debugPort, uint32_t _timeout);
uint8_t constructPacket(const uint16_t& messageLen, const uint8_t packetID = 0);
uint8_t parse(uint8_t recChar, bool valid = true);
uint8_t currentPacketID();
Expand Down Expand Up @@ -171,8 +173,8 @@ class Packet
uint8_t idByte = 0;
uint8_t overheadByte = 0;
uint8_t recOverheadByte = 0;


uint32_t packetStart = 0;
uint32_t timeout;
void calcOverhead(uint8_t arr[], const uint8_t& len);
int16_t findLast(uint8_t arr[], const uint8_t& len);
void stuffPacket(uint8_t arr[], const uint8_t& len);
Expand Down
7 changes: 7 additions & 0 deletions src/SerialTransfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ void SerialTransfer::begin(Stream& _port, const bool _debug, Stream& _debugPort)
packet.begin(_debug, _debugPort);
}

void SerialTransfer::begin(Stream& _port, const bool _debug, Stream& _debugPort, uint32_t _timeout)
{
port = &_port;
timeout = _timeout;
packet.begin(_debug, _debugPort, _timeout);
}


/*
uint8_t SerialTransfer::sendData(const uint16_t &messageLen, const uint8_t packetID)
Expand Down
2 changes: 2 additions & 0 deletions src/SerialTransfer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class SerialTransfer

void begin(Stream& _port, const configST configs);
void begin(Stream& _port, const bool _debug = true, Stream& _debugPort = Serial);
void begin(Stream& _port, const bool _debug, Stream& _debugPort, uint32_t _timeout);
uint8_t sendData(const uint16_t& messageLen, const uint8_t packetID = 0);
uint8_t available();
bool tick();
Expand Down Expand Up @@ -97,4 +98,5 @@ class SerialTransfer

private: // <<---------------------------------------//private
Stream* port;
uint32_t timeout;
};

0 comments on commit 3458920

Please sign in to comment.