serial : timeout in prgress

This commit is contained in:
tth 2018-12-12 22:26:54 +01:00
parent cda7b7f45e
commit c1652b68e2
2 changed files with 32 additions and 3 deletions

View File

@ -121,6 +121,8 @@ int getbyte_to (int fd, int to_ms)
{ {
unsigned char byte; unsigned char byte;
struct timeval timeout; struct timeval timeout;
fd_set rfds;
int retval;
timeout.tv_sec = to_ms / 1000; timeout.tv_sec = to_ms / 1000;
timeout.tv_usec = (to_ms % 1000) * 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); timeout.tv_sec, timeout.tv_usec);
#endif #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;
}

View File

@ -1,6 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include <time.h>
#include "serial.h" #include "serial.h"
@ -18,9 +19,10 @@ for (foo=0; foo<20; foo++) {
if (byte < 0) { if (byte < 0) {
fprintf(stderr, "get byte : err is %d\n", byte); fprintf(stderr, "get byte : err is %d\n", byte);
} }
else { // XXX else {
printf("%6d %6d %02x\n", foo, to, byte); printf("%9ld %6d %6d %02x\n",
} time(NULL), foo, to, byte);
// XXX }
} }
return 0; return 0;