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.
Diff: BlockingSerial.h
- Revision:
- 0:5de67898f1cc
diff -r 000000000000 -r 5de67898f1cc BlockingSerial.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/BlockingSerial.h Tue Dec 14 15:08:27 2010 +0000
@@ -0,0 +1,54 @@
+#ifndef _BLOCKING_SERIAL
+#define _BLOCKING_SERIAL
+
+#include "mbed.h"
+
+class BlockingSerial:public SerialHalfDuplex
+{
+ public:
+
+ BlockingSerial(PinName tx, PinName rx, char * name = NULL): SerialHalfDuplex(tx, rx, name)
+ {
+ }
+
+ int putc(int c)
+ {
+ if (writeable())
+ {
+ //printf("++++"); // MPN: for some reason this needs to be here??
+ return Serial::putc(c);
+ }
+
+ return -1;
+ }
+
+ int getc( int timeout = 1)
+ {
+ // if infinite timeout or we have a character, call base getc()
+ if ( timeout == -1 || readable() )
+ return Serial::getc();
+
+ // no character yet
+ bool has_data = false;
+ // count elapsed time
+ Timer timer;
+ timer.start();
+ // loop until we have data or timeout elapses
+ while ( !has_data && timer.read_ms() < timeout )
+ {
+ // wait a short time
+ wait_ms(1);
+ // check again
+ has_data = readable();
+ }
+ // do we have anything?
+ if ( has_data )
+ // yes, read it
+ return Serial::getc();
+ else
+ // no, timed out
+ return -1;
+ };
+
+};
+#endif _BLOCKING_SERIAL
\ No newline at end of file