comparison sw/sniff-rx/initflush.c @ 22:b112c2df6c43

sw: simtrace3-sniff-rx program written, compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 22 Aug 2023 06:16:44 +0000
parents
children
comparison
equal deleted inserted replaced
21:c4346cdc9641 22:b112c2df6c43
1 /*
2 * FTDI chip+driver combo (specifically the combination of FTDI chips and
3 * ftdi_sio driver in Linux, not sure who is the actual culprit) exhibits
4 * this unpleasant behaviour: even though we request "please flush all
5 * previous input" when we set our termios params, old accumulated serial
6 * Rx bytes still remain in some buffer somewhere, and a newly started
7 * serial application receives this stale garbage. As a workaround,
8 * we do an additional flush of our own: we put the fd in non-blocking mode
9 * and keep reading and discarding data until we get EAGAIN or EWOULDBLOCK.
10 */
11
12 #include <sys/types.h>
13 #include <sys/errno.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17
18 extern int target_fd;
19
20 void
21 init_serial_flush()
22 {
23 u_char buf[512];
24 int cc;
25
26 set_serial_nonblock(1);
27 for (;;) {
28 cc = read(target_fd, buf, sizeof buf);
29 if (cc <= 0)
30 break;
31 }
32 if (cc == 0) {
33 fprintf(stderr,
34 "read EOF from serial port during initial flush\n");
35 exit(1);
36 }
37 if (errno == EAGAIN || errno == EWOULDBLOCK)
38 return; /* success */
39 perror("serial port read");
40 exit(1);
41 }