A library to send and receive packets over serial, uses MODSERIAL
Dependents: SimpleSerialProtocolExample SerialFileReceiver
Revision 3:5caff50e14a7, committed 2014-09-19
- Comitter:
- p3p
- Date:
- Fri Sep 19 15:51:05 2014 +0000
- Parent:
- 2:43794e0b738f
- Commit message:
- fixed packet detection when struggling to keep up with data stream
Changed in this revision
diff -r 43794e0b738f -r 5caff50e14a7 Codec.h --- a/Codec.h Thu Sep 18 17:37:04 2014 +0000 +++ b/Codec.h Fri Sep 19 15:51:05 2014 +0000 @@ -41,8 +41,8 @@ return false; } - buffer->write(255); - buffer->write(127); + buffer->write(PACKET_BYTE1); + buffer->write(PACKET_BYTE2); buffer->write(packet->_size); for (int i = 0; i < packet->_size; i++) { buffer->write(packet->_data[i]);
diff -r 43794e0b738f -r 5caff50e14a7 Protocol.cpp --- a/Protocol.cpp Thu Sep 18 17:37:04 2014 +0000 +++ b/Protocol.cpp Fri Sep 19 15:51:05 2014 +0000 @@ -4,7 +4,7 @@ Protocol::Protocol(PinName tx, PinName rx, PinName led_pin) : MODSERIAL(tx, rx, 64, 512), _receive_led(led_pin), _send_buffer(512){ _receive_timeout.start(); - codec = 0; + codec = &default_codec; } Protocol::~Protocol() { @@ -39,6 +39,7 @@ } } _packet.reset(); + _receive_led = 0; } } @@ -66,7 +67,7 @@ void Protocol::receive() { _receive_timeout.reset(); - while (MODSERIAL::rxBufferGetCount() > 0 && _receive_timeout.read_us() < 50) { + while ((MODSERIAL::rxBufferGetCount() > 0 && _receive_timeout.read_us() < 50) && !_packet._valid) { if(codec){ codec->decode(&_packet, MODSERIAL::getc()); }
diff -r 43794e0b738f -r 5caff50e14a7 Protocol.h --- a/Protocol.h Thu Sep 18 17:37:04 2014 +0000 +++ b/Protocol.h Fri Sep 19 15:51:05 2014 +0000 @@ -27,9 +27,12 @@ Protocol(PinName tx, PinName rx, PinName led_pin); virtual ~Protocol(); - virtual void initialise(uint32_t baud, ICodec* stream_codec) { + virtual void initialise(uint32_t baud) { + MODSERIAL::baud(baud); + } + + void setCodec(ICodec* stream_codec){ codec = stream_codec; - MODSERIAL::baud(baud); } virtual void update(); @@ -103,6 +106,7 @@ std::map<uint8_t, std::vector<FPointer> > _callback; std::vector<TimerData> _transmit_callback; ICodec* codec; + Codec default_codec; }; }