From c3e4c91e2d803d51ebd4ec751915c57714de6ea2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Sat, 23 Dec 2017 16:21:37 +0100 Subject: [PATCH] fixed the bug with corrupted 13rd message --- gex/gex_client.c | 2 +- gex/serial/serial.c | 120 ++++++++++++++++++++------------------------ main.c | 37 +++++++------- 3 files changed, 75 insertions(+), 84 deletions(-) diff --git a/gex/gex_client.c b/gex/gex_client.c index 37e4edb..7dfbfb2 100644 --- a/gex/gex_client.c +++ b/gex/gex_client.c @@ -175,7 +175,7 @@ GexClient *GEX_Init(const char *device, int timeout_ms) /** Try to read from the serial port and process any received bytes with TF */ void GEX_Poll(GexClient *gex) { - uint8_t pollbuffer[TF_MAX_PAYLOAD_RX]; + static uint8_t pollbuffer[TF_MAX_PAYLOAD_RX]; assert(gex != NULL); diff --git a/gex/serial/serial.c b/gex/serial/serial.c index b5d102f..fa04e1e 100644 --- a/gex/serial/serial.c +++ b/gex/serial/serial.c @@ -8,86 +8,74 @@ #include #include -static int set_interface_attribs(int fd, int speed, int parity) +static int set_interface_attribs(int fd) { - struct termios tty; - memset(&tty, 0, sizeof tty); - if (tcgetattr(fd, &tty) != 0) { - printf("error %d from tcgetattr\n", errno); - return -1; - } - - cfsetospeed(&tty, (speed_t) speed); - cfsetispeed(&tty, (speed_t) speed); - - tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars - // disable IGNBRK for mismatched speed tests; otherwise receive break - // as \000 chars - tty.c_iflag &= ~IGNBRK; // disable break processing - tty.c_lflag = 0; // no signaling chars, no echo, - // no canonical processing - tty.c_oflag = 0; // no remapping, no delays - tty.c_cc[VMIN] = 0; // read doesn't block - tty.c_cc[VTIME] = 2; // 0.2 seconds read timeout - - tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl - - tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls, - // enable reading - tty.c_cflag &= ~(PARENB | PARODD); // shut off parity - tty.c_cflag |= parity; - tty.c_cflag &= ~CSTOPB; - tty.c_cflag &= ~CRTSCTS; - - if (tcsetattr(fd, TCSANOW, &tty) != 0) { - printf("error %d from tcsetattr\n", errno); - return -1; - } - return 0; + struct termios tty; + memset(&tty, 0, sizeof tty); + if (tcgetattr(fd, &tty) != 0) { + printf("error %d from tcgetattr\n", errno); + return -1; + } + + cfsetospeed(&tty, (speed_t) __MAX_BAUD); + cfsetispeed(&tty, (speed_t) __MAX_BAUD); + + tty.c_cflag = CLOCAL | CREAD | CS8; // 8-bit chars + tty.c_iflag = 0; + tty.c_lflag = 0; + tty.c_oflag = 0; + tty.c_cc[VMIN] = 0; // read doesn't block + tty.c_cc[VTIME] = 2; // 0.2 seconds read timeout + + if (tcsetattr(fd, TCSANOW, &tty) != 0) { + printf("error %d from tcsetattr\n", errno); + return -1; + } + return 0; } int serial_open(const char *device) { - int fd = open (device, O_RDWR | O_NOCTTY | O_SYNC); - if (fd < 0) { - fprintf (stderr, "FAILED TO OPEN SERIAL! Error %d opening %s: %s\n", errno, device, strerror (errno)); - return -1; - } + int fd = open(device, O_RDWR | O_NOCTTY | O_SYNC); + if (fd < 0) { + fprintf(stderr, "FAILED TO OPEN SERIAL! Error %d opening %s: %s\n", errno, device, strerror(errno)); + return -1; + } - set_interface_attribs(fd, B115200, 0); + set_interface_attribs(fd); - return fd; + return fd; } void serial_noblock(int fd) { - struct termios tty; - memset(&tty, 0, sizeof tty); - if (tcgetattr(fd, &tty) != 0) { - fprintf(stderr, "error %d from tggetattr\n", errno); - return; - } - - tty.c_cc[VMIN] = (cc_t) 0; - tty.c_cc[VTIME] = (cc_t) 0; - - if (tcsetattr(fd, TCSANOW, &tty) != 0) - fprintf(stderr, "error %d setting term attributes\n", errno); + struct termios tty; + memset(&tty, 0, sizeof tty); + if (tcgetattr(fd, &tty) != 0) { + fprintf(stderr, "error %d from tggetattr\n", errno); + return; + } + + tty.c_cc[VMIN] = (cc_t) 0; + tty.c_cc[VTIME] = (cc_t) 0; + + if (tcsetattr(fd, TCSANOW, &tty) != 0) + fprintf(stderr, "error %d setting term attributes\n", errno); } void serial_shouldwait(int fd, int ms) { - struct termios tty; - memset(&tty, 0, sizeof tty); - if (tcgetattr(fd, &tty) != 0) { - fprintf(stderr, "error %d from tggetattr\n", errno); - return; - } - - tty.c_cc[VMIN] = (cc_t) 0; - tty.c_cc[VTIME] = (cc_t) (ms+50/100); - - if (tcsetattr(fd, TCSANOW, &tty) != 0) - fprintf(stderr, "error %d setting term attributes\n", errno); + struct termios tty; + memset(&tty, 0, sizeof tty); + if (tcgetattr(fd, &tty) != 0) { + fprintf(stderr, "error %d from tggetattr\n", errno); + return; + } + + tty.c_cc[VMIN] = (cc_t) 0; + tty.c_cc[VTIME] = (cc_t) (ms + 50 / 100); + + if (tcsetattr(fd, TCSANOW, &tty) != 0) + fprintf(stderr, "error %d setting term attributes\n", errno); } diff --git a/main.c b/main.c index 65d81e0..7042721 100644 --- a/main.c +++ b/main.c @@ -35,7 +35,7 @@ int main(void) // Bind ^C handler for safe shutdown signal(SIGINT, sigintHandler); - gex = GEX_Init("/dev/ttyACM0", 100); + gex = GEX_Init("/dev/ttyACM0", 200); if (!gex) exit(1); TF_AddGenericListener(GEX_GetTF(gex), hdl_default); @@ -49,23 +49,9 @@ int main(void) // the "PING" command GexMsg msg; -#if 1 - // Simple response - - // It looks like the ID listeners are not being freed! - // May be a bug both here and on the GEX side. - for(int i=0; i<30; i++) { - fprintf(stderr, "\n%d \"PING\"\n", i); - msg = GEX_Query0(test, 0); - assert(msg.type == MSG_SUCCESS); - fprintf(stderr, "\"PING\" OK!\n"); - usleep(100000); - } -#endif - #if 1 // Test a echo command that returns back what was sent to it as useful payload - const char *s = "I am returning this otherwise good typing paper to you because someone " + const char *s = "I am \r\nreturning this otherwise good typing paper to you because someone " "has printed gibberish all over it and put your name at the top. Read the communist manifesto via bulk transfer. Read the communist manifesto via bulk transfer. Technology is a constand battle between manufacturers producing bigger and " "more idiot-proof systems and nature producing bigger and better idiots. END"; msg = GEX_Query(test, 1, (const uint8_t *) s, (uint32_t) strlen(s)); @@ -73,6 +59,23 @@ int main(void) assert(0==strncmp((char*)msg.payload, s, strlen(s))); #endif +#if 1 + // Simple response + int failures = 0; + for(int i=0; i<20; i++) { + fprintf(stderr, "\nSending \"PING\" #%d\n", i); + msg = GEX_Query0(test, 0); + fprintf(stderr, "\"PING\" resp = %d\n", msg.type); + if (msg.type != 0) { + fprintf(stderr, "---------------------ERRR-------------------\n"); + if (msg.type == 2) fprintf(stderr, "%.*s\n", msg.len, msg.payload); + failures++; + } + //usleep(20000); + } + fprintf(stderr, "FAILURES = %d\n", failures); +#endif + #if 0 // Read the communist manifesto via bulk transfer uint8_t buffr[10000]; @@ -88,7 +91,7 @@ int main(void) fprintf(stderr, "%.*s", actuallyRead, buffr); #endif - fprintf(stderr, "ALL OK, ending.\n"); + fprintf(stderr, "ALL done, ending.\n"); GEX_DeInit(gex); return 0; }