diff options
Diffstat (limited to 'arduino-serial-lib.c')
-rw-r--r-- | arduino-serial-lib.c | 39 |
1 files changed, 30 insertions, 9 deletions
diff --git a/arduino-serial-lib.c b/arduino-serial-lib.c index 39461ce..360e747 100644 --- a/arduino-serial-lib.c +++ b/arduino-serial-lib.c @@ -25,15 +25,15 @@ int serialport_init(const char* serialport, int baud) { struct termios toptions; int fd; - + //fd = open(serialport, O_RDWR | O_NOCTTY | O_NDELAY); fd = open(serialport, O_RDWR | O_NONBLOCK ); - + if (fd == -1) { perror("serialport_init: Unable to open port "); return -1; } - + //int iflags = TIOCM_DTR; //ioctl(fd, TIOCMBIS, &iflags); // turn on DTR //ioctl(fd, TIOCMBIC, &iflags); // turn off DTR @@ -80,7 +80,7 @@ int serialport_init(const char* serialport, int baud) toptions.c_cc[VMIN] = 0; toptions.c_cc[VTIME] = 0; //toptions.c_cc[VTIME] = 20; - + tcsetattr(fd, TCSANOW, &toptions); if( tcsetattr(fd, TCSAFLUSH, &toptions) < 0) { perror("init_serialport: Couldn't set term attributes"); @@ -122,26 +122,47 @@ int serialport_read_until(int fd, char* buf, char until, int buf_max, int timeou { char b[1]; // read expects an array, so we give it a 1-byte array int i=0; - do { + do { int n = read(fd, b, 1); // read a char at a time if( n==-1) return -1; // couldn't read if( n==0 ) { usleep( 1 * 1000 ); // wait 1 msec try again - timeout--; + --timeout; if( timeout==0 ) return -2; continue; } -#ifdef SERIALPORTDEBUG + +#ifdef SERIALPORTDEBUG printf("serialport_read_until: i=%d, n=%d b='%c'\n",i,n,b[0]); // debug #endif - buf[i] = b[0]; - i++; + buf[i] = b[0]; + ++i; } while( b[0] != until && i < buf_max && timeout>0 ); buf[i] = 0; // null terminate the string return 0; } +//return input forever +int serialport_read_unlimited(int fd, char* buf, char until, int buf_max) +{ + char b[1]; // read expects an array, so we give it a 1-byte array + int i = 0; + do { + int n = read(fd, b, 1); // read a char at a time + if( n==-1) return -1; // couldn't read + if( n==0 ) { + usleep( 1 * 1000 ); // wait 1 msec try again + continue; + } + buf[i] = b[0]; + ++i; + } while( b[0] != until && i < buf_max ); + + buf[i] = 0; // null terminate the string + return 0; +} + // int serialport_flush(int fd) { |