From 560edb14e891c4d6da526a40f4ef0f0620d6581a Mon Sep 17 00:00:00 2001 From: tth Date: Sat, 29 Dec 2018 12:09:34 +0100 Subject: [PATCH] select in 'get_byte_to' don't work --- serial/serial.c | 14 +++++++++----- serial/t.c | 51 +++++++++++++------------------------------------ 2 files changed, 22 insertions(+), 43 deletions(-) diff --git a/serial/serial.c b/serial/serial.c index 527d304..ac2abc3 100644 --- a/serial/serial.c +++ b/serial/serial.c @@ -86,7 +86,7 @@ if (uart0 < 0) baudbits = baudrate2const(baudrate); -#if DEBUG_LEVEL +#if DEBUG_LEVEL > 1 fprintf(stderr, "%d -> 0x%04x\n", baudrate, baudbits); #endif @@ -97,6 +97,9 @@ options.c_cflag = baudbits | CS8 | CLOCAL | CREAD; options.c_iflag = IGNPAR; options.c_oflag = 0; options.c_lflag = 0; + +options.c_cc[VMIN] = 1; /* ask for blocking read */ + tcflush(uart0, TCIFLUSH); tcsetattr(uart0, TCSANOW, &options); @@ -133,9 +136,9 @@ 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; -#if DEBUG_LEVEL +#if DEBUG_LEVEL > 1 fprintf(stderr, "timeout %6d is %4ld.%6ld\n", to_ms, timeout.tv_sec, timeout.tv_usec); #endif @@ -145,7 +148,7 @@ FD_SET (fd, &rfds); retval = select(1, &rfds, NULL, NULL, &timeout); #if DEBUG_LEVEL -fprintf(stderr, "%s : select -> %d\n", __func__, retval); +fprintf(stderr, "%s : select on %d -> %d\n", __func__, fd, retval); #endif switch (retval) { @@ -156,7 +159,8 @@ switch (retval) { break; case 0: - fprintf(stderr, "timeout\n"); + fprintf(stderr, "timeout %ld.%ld\n", + timeout.tv_sec, timeout.tv_usec); retval = -99; break; diff --git a/serial/t.c b/serial/t.c index 2a72e37..25e0cf7 100644 --- a/serial/t.c +++ b/serial/t.c @@ -1,4 +1,7 @@ - +/* + * Experiments with the serial input + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ #include #include @@ -14,63 +17,35 @@ int verbosity; -/* ----------------------------------------------------- */ -int openserial(char *dev) -{ -int uart; - -fprintf(stderr, "%s ( %s )\n", __func__, dev); -uart = open(dev, O_RDONLY | O_NOCTTY); -if (uart < 0) - { - perror("unable to open uart"); - exit(1); - } -return uart; -} -/* ----------------------------------------------------- */ -int configserial(int fd) -{ -struct termios tios; -bzero(&tios, sizeof(tios)); -tios.c_cflag = B9600 | CRTSCTS | CS8 | CLOCAL | CREAD; -tios.c_lflag = ICANON; -tios.c_cc[VMIN] = 1; /* read bloquant ??? */ -tcsetattr(fd,TCSANOW,&tios); -return 0; -} -/* ----------------------------------------------------- */ +/* ---------------------------------------------------------------- */ int main (int argc, char *argv[]) { int serial_in, foo; -unsigned char byte; if (2 != argc) { fprintf(stderr, "give me a device name, please.\n"); return 2; } -serial_in = openserial(argv[1]); -fprintf(stderr, "openserial -> %d\n", serial_in); - -(void)configserial(serial_in); +serial_in = prepare_UART(argv[1], 9600); +fprintf(stderr, "going to listen on %d\n", serial_in); for (;;) { - foo = read(serial_in, &byte, 1); - if (1 != foo) { + foo = getbyte_to(serial_in, 50000); + if (foo < 0) { fprintf(stderr, "get byte : got %d, err is %d\n", foo, errno); } else { - printf("%9ld $%02x ", time(NULL), byte); - if (isprint(byte)) putchar(byte); + printf("%9ld $%02x ", time(NULL), foo); + if (isprint(foo)) putchar(foo); puts(""); - if ('\n'==byte) puts(""); + if ('\n'==foo) puts(""); } } return 0; } -/* ----------------------------------------------------- */ +/* ---------------------------------------------------------------- */