![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Serial buffer problem
SerialBuffered.h@0:56a10c93329f, 2010-10-11 (annotated)
- Committer:
- pd0wm
- Date:
- Mon Oct 11 18:46:18 2010 +0000
- Revision:
- 0:56a10c93329f
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pd0wm | 0:56a10c93329f | 1 | #pragma once |
pd0wm | 0:56a10c93329f | 2 | |
pd0wm | 0:56a10c93329f | 3 | // This is a buffered serial reading class, using the serial interrupt introduced in mbed library version 18 on 17/11/09 |
pd0wm | 0:56a10c93329f | 4 | |
pd0wm | 0:56a10c93329f | 5 | // In the simplest case, construct it with a buffer size at least equal to the largest message you |
pd0wm | 0:56a10c93329f | 6 | // expect your program to receive in one go. |
pd0wm | 0:56a10c93329f | 7 | |
pd0wm | 0:56a10c93329f | 8 | class SerialBuffered : public Serial |
pd0wm | 0:56a10c93329f | 9 | { |
pd0wm | 0:56a10c93329f | 10 | public: |
pd0wm | 0:56a10c93329f | 11 | SerialBuffered( size_t bufferSize, PinName tx, PinName rx ); |
pd0wm | 0:56a10c93329f | 12 | virtual ~SerialBuffered(); |
pd0wm | 0:56a10c93329f | 13 | |
pd0wm | 0:56a10c93329f | 14 | int getc(); // will block till the next character turns up, or return -1 if there is a timeout |
pd0wm | 0:56a10c93329f | 15 | |
pd0wm | 0:56a10c93329f | 16 | int readable(); // returns 1 if there is a character available to read, 0 otherwise |
pd0wm | 0:56a10c93329f | 17 | |
pd0wm | 0:56a10c93329f | 18 | void setTimeout( float seconds ); // maximum time in seconds that getc() should block |
pd0wm | 0:56a10c93329f | 19 | // while waiting for a character |
pd0wm | 0:56a10c93329f | 20 | // Pass -1 to disable the timeout. |
pd0wm | 0:56a10c93329f | 21 | |
pd0wm | 0:56a10c93329f | 22 | size_t readBytes( uint8_t *bytes, size_t requested ); // read requested bytes into a buffer, |
pd0wm | 0:56a10c93329f | 23 | // return number actually read, |
pd0wm | 0:56a10c93329f | 24 | // which may be less than requested if there has been a timeout |
pd0wm | 0:56a10c93329f | 25 | |
pd0wm | 0:56a10c93329f | 26 | |
pd0wm | 0:56a10c93329f | 27 | private: |
pd0wm | 0:56a10c93329f | 28 | |
pd0wm | 0:56a10c93329f | 29 | void handleInterrupt(); |
pd0wm | 0:56a10c93329f | 30 | |
pd0wm | 0:56a10c93329f | 31 | |
pd0wm | 0:56a10c93329f | 32 | uint8_t *m_buff; // points at a circular buffer, containing data from m_contentStart, for m_contentSize bytes, wrapping when you get to the end |
pd0wm | 0:56a10c93329f | 33 | uint16_t m_contentStart; // index of first bytes of content |
pd0wm | 0:56a10c93329f | 34 | uint16_t m_contentEnd; // index of bytes after last byte of content |
pd0wm | 0:56a10c93329f | 35 | uint16_t m_buffSize; |
pd0wm | 0:56a10c93329f | 36 | float m_timeout; |
pd0wm | 0:56a10c93329f | 37 | Timer m_timer; |
pd0wm | 0:56a10c93329f | 38 | |
pd0wm | 0:56a10c93329f | 39 | }; |