From c1652b68e24e1f84f3af9dac44ab334c67d39a9e Mon Sep 17 00:00:00 2001 From: tth Date: Wed, 12 Dec 2018 22:26:54 +0100 Subject: [PATCH] serial : timeout in prgress --- serial/serial.c | 27 +++++++++++++++++++++++++++ serial/t.c | 8 +++++--- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/serial/serial.c b/serial/serial.c index 471907c..5a51a57 100644 --- a/serial/serial.c +++ b/serial/serial.c @@ -121,6 +121,8 @@ int getbyte_to (int fd, int to_ms) { unsigned char byte; struct timeval timeout; +fd_set rfds; +int retval; timeout.tv_sec = to_ms / 1000; timeout.tv_usec = (to_ms % 1000) * 1000; @@ -130,6 +132,31 @@ fprintf(stderr, "timeout %6d is %4ld %6ld\n", to_ms, timeout.tv_sec, timeout.tv_usec); #endif +FD_ZERO (&rfds); +FD_SET (fd, &rfds); + +retval = select(1, &rfds, NULL, NULL, &timeout); + +#if DEBUG_LEVEL +fprintf(stderr, "%s : select -> %d\n", __func__, retval); +#endif + +retval = retval < 0 ? -1 : retval; + +switch (retval) { + + case -1: + fprintf(stderr, "omg ?\n"); break; + + case 0: + fprintf(stderr, "timeout\n"); break; + + + + default: + return -99; + } + diff --git a/serial/t.c b/serial/t.c index cbc43aa..8a75043 100644 --- a/serial/t.c +++ b/serial/t.c @@ -1,6 +1,7 @@ #include +#include #include "serial.h" @@ -18,9 +19,10 @@ for (foo=0; foo<20; foo++) { if (byte < 0) { fprintf(stderr, "get byte : err is %d\n", byte); } - else { - printf("%6d %6d %02x\n", foo, to, byte); - } + // XXX else { + printf("%9ld %6d %6d %02x\n", + time(NULL), foo, to, byte); + // XXX } } return 0;