Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
SerialStream.h@1:9437ea7d0799, 2020-08-25 (annotated)
- Committer:
 - Jamie Smith 
 - Date:
 - Tue Aug 25 17:53:20 2020 -0700
 - Revision:
 - 1:9437ea7d0799
 - Parent:
 - 0:889cfd2df4cd
 - Child:
 - 2:3736580f2dbe
 
Allow including as the first header file
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| Jamie Smith  | 
0:889cfd2df4cd | 1 | #ifndef SERIALSTREAM_H | 
| Jamie Smith  | 
0:889cfd2df4cd | 2 | #define SERIALSTREAM_H | 
| Jamie Smith  | 
0:889cfd2df4cd | 3 | |
| Jamie Smith  | 
1:9437ea7d0799 | 4 | #include <mbed.h> | 
| Jamie Smith  | 
0:889cfd2df4cd | 5 | #include <platform/Stream.h> | 
| Jamie Smith  | 
0:889cfd2df4cd | 6 | |
| Jamie Smith  | 
0:889cfd2df4cd | 7 | /** | 
| Jamie Smith  | 
0:889cfd2df4cd | 8 | * SerialStream | 
| Jamie Smith  | 
0:889cfd2df4cd | 9 | * Bringing MBed serial ports back like it's 1999... or at least 2019. | 
| Jamie Smith  | 
0:889cfd2df4cd | 10 | * | 
| Jamie Smith  | 
0:889cfd2df4cd | 11 | * This class adapts an MBed 6.0 serial port class into a Stream instance. | 
| Jamie Smith  | 
0:889cfd2df4cd | 12 | * This lets you do two useful things with it: | 
| Jamie Smith  | 
0:889cfd2df4cd | 13 | * - Call printf() and scanf() on it | 
| Jamie Smith  | 
0:889cfd2df4cd | 14 | * - Pass it to code that expects a Stream to print things on. | 
| Jamie Smith  | 
0:889cfd2df4cd | 15 | * | 
| Jamie Smith  | 
0:889cfd2df4cd | 16 | */ | 
| Jamie Smith  | 
0:889cfd2df4cd | 17 | template<class SerialClass> | 
| Jamie Smith  | 
0:889cfd2df4cd | 18 | class SerialStream : public Stream | 
| Jamie Smith  | 
0:889cfd2df4cd | 19 | { | 
| Jamie Smith  | 
0:889cfd2df4cd | 20 | SerialClass & serialClass; | 
| Jamie Smith  | 
0:889cfd2df4cd | 21 | |
| Jamie Smith  | 
0:889cfd2df4cd | 22 | public: | 
| Jamie Smith  | 
0:889cfd2df4cd | 23 | |
| Jamie Smith  | 
0:889cfd2df4cd | 24 | /** | 
| Jamie Smith  | 
0:889cfd2df4cd | 25 | * Create a SerialStream from a serial port. | 
| Jamie Smith  | 
0:889cfd2df4cd | 26 | * @param _serialClass BufferedSerial or UnbufferedSerial instance | 
| Jamie Smith  | 
0:889cfd2df4cd | 27 | * @param name The name of the stream associated with this serial port (optional) | 
| Jamie Smith  | 
0:889cfd2df4cd | 28 | */ | 
| Jamie Smith  | 
0:889cfd2df4cd | 29 | SerialStream(SerialClass & _serialClass, const char *name = nullptr): | 
| Jamie Smith  | 
0:889cfd2df4cd | 30 | Stream(name), | 
| Jamie Smith  | 
0:889cfd2df4cd | 31 | serialClass(_serialClass) | 
| Jamie Smith  | 
0:889cfd2df4cd | 32 | { | 
| Jamie Smith  | 
0:889cfd2df4cd | 33 | } | 
| Jamie Smith  | 
0:889cfd2df4cd | 34 | |
| Jamie Smith  | 
0:889cfd2df4cd | 35 | |
| Jamie Smith  | 
0:889cfd2df4cd | 36 | private: | 
| Jamie Smith  | 
0:889cfd2df4cd | 37 | |
| Jamie Smith  | 
0:889cfd2df4cd | 38 | // override Stream::read() and write() to call serial class directly. | 
| Jamie Smith  | 
0:889cfd2df4cd | 39 | // This avoids the overhead of feeding in individual characters. | 
| Jamie Smith  | 
0:889cfd2df4cd | 40 | virtual ssize_t write(const void *buffer, size_t length) | 
| Jamie Smith  | 
0:889cfd2df4cd | 41 | { | 
| Jamie Smith  | 
0:889cfd2df4cd | 42 | return serialClass.write(buffer, length); | 
| Jamie Smith  | 
0:889cfd2df4cd | 43 | } | 
| Jamie Smith  | 
0:889cfd2df4cd | 44 | |
| Jamie Smith  | 
0:889cfd2df4cd | 45 | virtual ssize_t read(void *buffer, size_t length) | 
| Jamie Smith  | 
0:889cfd2df4cd | 46 | { | 
| Jamie Smith  | 
0:889cfd2df4cd | 47 | return serialClass.read(buffer, length); | 
| Jamie Smith  | 
0:889cfd2df4cd | 48 | } | 
| Jamie Smith  | 
0:889cfd2df4cd | 49 | |
| Jamie Smith  | 
0:889cfd2df4cd | 50 | // Dummy implementations -- these will never be called because we override write() and read() instead. | 
| Jamie Smith  | 
0:889cfd2df4cd | 51 | // but we have to override them since they're pure virtual. | 
| Jamie Smith  | 
0:889cfd2df4cd | 52 | virtual int _putc(int c) { return 0; } | 
| Jamie Smith  | 
0:889cfd2df4cd | 53 | virtual int _getc() { return 0; } | 
| Jamie Smith  | 
0:889cfd2df4cd | 54 | }; | 
| Jamie Smith  | 
0:889cfd2df4cd | 55 | |
| Jamie Smith  | 
0:889cfd2df4cd | 56 | #endif //SERIALSTREAM_H |