From d72f479e575d9b83207117209cbb7db6202e5488 Mon Sep 17 00:00:00 2001 From: "Tod E. Kurt" Date: Mon, 29 Apr 2013 17:15:05 -0700 Subject: added timeout to reads --- arduino-serial-lib.c | 11 +++++++++-- arduino-serial-lib.h | 3 ++- 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 @@ -89,6 +89,12 @@ int serialport_init(const char* serialport, int baud) return fd; } +// +int serialport_close( int fd ) +{ + return close( fd ); +} + // int serialport_writebyte( int fd, uint8_t b) { @@ -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 // 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 -- cgit v1.2.3