summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTod E. Kurt <tod@todbot.com>2013-04-29 17:15:05 -0700
committerTod E. Kurt <tod@todbot.com>2013-04-29 17:15:05 -0700
commitd72f479e575d9b83207117209cbb7db6202e5488 (patch)
tree059d525277512b6ec722e36595fe44f6df747b64
parentf15c116590b4ead96858ac1df012b2e20926a19c (diff)
downloadarduino-serial-d72f479e575d9b83207117209cbb7db6202e5488.tar.gz
arduino-serial-d72f479e575d9b83207117209cbb7db6202e5488.tar.bz2
arduino-serial-d72f479e575d9b83207117209cbb7db6202e5488.zip
added timeout to reads
-rw-r--r--arduino-serial-lib.c11
-rw-r--r--arduino-serial-lib.h3
2 files changed, 11 insertions, 3 deletions
diff --git a/arduino-serial-lib.c b/arduino-serial-lib.c
index a76dc65..7ec100c 100644
--- a/arduino-serial-lib.c
+++ b/arduino-serial-lib.c
@@ -90,6 +90,12 @@ int serialport_init(const char* serialport, int baud)
}
//
+int serialport_close( int fd )
+{
+ return close( fd );
+}
+
+//
int serialport_writebyte( int fd, uint8_t b)
{
int n = write(fd,&b,1);
@@ -111,7 +117,7 @@ int serialport_write(int fd, const char* str)
}
//
-int serialport_read_until(int fd, char* buf, char until, int buf_max)
+int serialport_read_until(int fd, char* buf, char until, int buf_max, int timeout)
{
char b[1]; // read expects an array, so we give it a 1-byte array
int i=0;
@@ -120,6 +126,7 @@ int serialport_read_until(int fd, char* buf, char until, int buf_max)
if( n==-1) return -1; // couldn't read
if( n==0 ) {
usleep( 1 * 1000 ); // wait 1 msec try again
+ timeout--;
continue;
}
#ifdef SERIALPORTDEBUG
@@ -127,7 +134,7 @@ int serialport_read_until(int fd, char* buf, char until, int buf_max)
#endif
buf[i] = b[0];
i++;
- } while( b[0] != until && i < buf_max );
+ } while( b[0] != until && i < buf_max && timeout>0 );
buf[i] = 0; // null terminate the string
return 0;
diff --git a/arduino-serial-lib.h b/arduino-serial-lib.h
index 234ec27..a0731e1 100644
--- a/arduino-serial-lib.h
+++ b/arduino-serial-lib.h
@@ -11,9 +11,10 @@
#include <stdint.h> // Standard types
int serialport_init(const char* serialport, int baud);
+int serialport_close(int fd);
int serialport_writebyte( int fd, uint8_t b);
int serialport_write(int fd, const char* str);
-int serialport_read_until(int fd, char* buf, char until, int buf_max);
+int serialport_read_until(int fd, char* buf, char until, int buf_max,int timeout);
int serialport_flush(int fd);
#endif