From bb10042aaebaa69ab4764265310d9ee1c4549538 Mon Sep 17 00:00:00 2001 From: tth Date: Thu, 20 Dec 2018 11:57:23 +0100 Subject: [PATCH] blocking read is not blocking --- README.md | 5 +++ serial/README.md | 6 +++ serial/serial.c | 45 ++++++++++++++-------- serial/serial.h | 2 +- serial/t.c | 28 +++++++++----- simulator/send-random/send-random.ino | 55 +++++++++++++++++++++++++++ 6 files changed, 115 insertions(+), 26 deletions(-) create mode 100644 simulator/send-random/send-random.ino diff --git a/README.md b/README.md index 315e02f..9404e2c 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,11 @@ Pour en savoir plus sur ce passionnant projet, il y a le canal IRC `#tetalab` sur le réseau Freenode et/ou les rencontres du mercredi soir au DD2, à Mixart-Myrys. +# WTF status + +Le `read` bloquant ne bloque pas. Ça craint grave. + + diff --git a/serial/README.md b/serial/README.md index 2a541de..cfe5c6a 100644 --- a/serial/README.md +++ b/serial/README.md @@ -22,6 +22,12 @@ par le professeur Cispeo. Un petit exemple ? +Oui, voilà. À ce jour (20 déc. 2018), on va dire que ça ne marche pas. +Il faut dire que les `serial devices` ont toujours étés un peu le +domaine de la magie noire. Mais quand même, coincer sur un `read` qui +ne bloque pas, c'est un peu ironique. + + diff --git a/serial/serial.c b/serial/serial.c index 1c04a8a..bec0bf8 100644 --- a/serial/serial.c +++ b/serial/serial.c @@ -1,7 +1,9 @@ #include #include +#include #include +#include #include //Used for UART #include //Used for UART #include //Used for UART @@ -60,12 +62,14 @@ fprintf(stderr, "%s ( %s %d )\n", __func__, port, baudrate); // shall not cause the terminal device to become the controlling terminal // for the process. -uart0 = open(port, O_RDWR | O_NOCTTY); -if (uart0== -1) +uart0 = open(port, O_RDONLY | O_NOCTTY); +if (uart0 < 0) { perror("unable to open uart "); return -1; } + +return uart0; /* WTF ??? */ // CONFIGURE THE UART // The flags defined in /usr/include/termios.h - @@ -87,6 +91,9 @@ baudbits = baudrate2const(baudrate); #if DEBUG_LEVEL fprintf(stderr, "%d -> 0x%04x\n", baudrate, baudbits); #endif + +memset(&options, 0, sizeof(options)); + tcgetattr(uart0, &options); options.c_cflag = baudbits | CS8 | CLOCAL | CREAD; // %d\n", __func__, retval); #endif -retval = retval < 0 ? -1 : retval; - switch (retval) { case -1: fprintf(stderr, "omg ?\n"); - byte = -1; + retval = -1; break; case 0: fprintf(stderr, "timeout\n"); - byte = -99; + retval = -99; break; default: + fprintf(stderr, "%s default -> %d\n", __func__, retval); if (retval==fd) { read(fd, &byte, 1); fprintf(stderr, "got 0x%02x\n", byte); + retval = byte; } else { fprintf(stderr, "%d bad fd ?\n", retval); - byte = -3; + retval = -3; } break; } -return byte; +return retval; } /* -------------------------------------------------------------------- */ /* timeout is in milliseconds */ @@ -195,15 +204,21 @@ for(;;) { retval = curpos; } - - + if (curpos < szm) { /* ya de la place */ + where[curpos] = byte; + curpos++; + } + else { /* oups overflow */ + retval = -6; + break; + } } fprintf(stderr, "%s -> '%s'\n", __func__, where); -return -666; +return retval; } /* -------------------------------------------------------------------- */ diff --git a/serial/serial.h b/serial/serial.h index 9da35b3..a13d292 100644 --- a/serial/serial.h +++ b/serial/serial.h @@ -6,7 +6,7 @@ int prepare_UART(char *port, int bauds); -int getbyte(int fd); +int getbyte(int fd); /* really brotched func */ /* timeout is exprimed in milliseconds. */ diff --git a/serial/t.c b/serial/t.c index 8a75043..4fed028 100644 --- a/serial/t.c +++ b/serial/t.c @@ -1,28 +1,36 @@ #include +#include #include #include "serial.h" +int verbosity; + int main (int argc, char *argv[]) { -int serial_in; -int byte, foo, to; +int serial_in; +int byte; -serial_in = prepare_UART("/dev/ttyS0", 9600); +serial_in = prepare_UART("/dev/ttyACM0", 9600); fprintf(stderr, "prepare uart -> %d\n", serial_in); -for (foo=0; foo<20; foo++) { - to = (foo+1) * 666; - byte = getbyte_to(serial_in, to); +if (serial_in < 0) { + exit(1); + } + +for (;;) { + + byte = getbyte(serial_in); + if (byte < 0) { fprintf(stderr, "get byte : err is %d\n", byte); } - // XXX else { - printf("%9ld %6d %6d %02x\n", - time(NULL), foo, to, byte); - // XXX } + else { + printf("%9ld %02x/%d\n", + time(NULL), byte, byte); + } } return 0; diff --git a/simulator/send-random/send-random.ino b/simulator/send-random/send-random.ino new file mode 100644 index 0000000..16cd63f --- /dev/null +++ b/simulator/send-random/send-random.ino @@ -0,0 +1,55 @@ +/* + * simulateur de telemesure automate + */ + +/* -------------------------------------------------- */ + +#define NBVAL 4 + +int values[NBVAL]; + +/* -------------------------------------------------- */ +void setup() { + int foo; + Serial.begin(9600); + pinMode(LED_BUILTIN, OUTPUT); + for (foo=0; foo 1023) { + values[foo] = rand()%15; + } + } +} +/* -------------------------------------------------- */ + +void sendvalues(void) +{ + int foo; + + Serial.print("X "); + for (foo=0; foo