Flush serial port input first
authorCyprien Laplace <cyprien@cypou.net>
Sat, 5 Aug 2017 20:15:18 +0000 (16:15 -0400)
committerNathael Pajani <nathael.pajani@ed3l.fr>
Sat, 16 Feb 2019 09:40:02 +0000 (10:40 +0100)
There can be few characters in the serial port buffer. Draining
them before attempting synchronization avoids synchronization
issues.

isp_commands.c
isp_utils.c
isp_utils.h

index 00a4578..5168f36 100644 (file)
@@ -117,6 +117,9 @@ int isp_connect(unsigned int crystal_freq, int quiet)
 
        snprintf(freq, 8, "%d\r\n", crystal_freq);
 
+       /* Flush input (previously buffered garbage) */
+       isp_serial_flush_input();
+
        /* Send synchronize request */
        if (isp_serial_write(SYNCHRO_START, strlen(SYNCHRO_START)) != strlen(SYNCHRO_START)) {
                printf("Unable to send synchronize request.\n");
index 7592a77..c5cc4e9 100644 (file)
@@ -165,6 +165,20 @@ int isp_serial_write(const char* buf, unsigned int buf_size)
        return count;
 }
 
+void isp_serial_flush_input()
+{
+       int nb;
+       char unused;
+       do {
+               nb = read(serial_fd, &unused, 1);
+       } while (nb > 0);
+       if (nb == 0) {
+               printf("serial_read: end of file !!!!\n");
+       } else if ((errno != EAGAIN) && (errno != EWOULDBLOCK)) {
+               perror("Serial read error");
+       }
+}
+
 static char next_read_char = 0;
 void isp_serial_empty_buffer()
 {
index 21b08ea..6c488bd 100644 (file)
@@ -45,6 +45,7 @@ void isp_dump(const unsigned char* buf, unsigned int buf_size);
  */
 int isp_serial_open(int baudrate, char* serial_device);
 void isp_serial_close(void);
+void isp_serial_flush_input(void);
 
 /* Simple write() wrapper, with trace if enabled */
 int isp_serial_write(const char* buf, unsigned int buf_size);