From 863d5a8f3f80d5f0e78d77d5b72d260ed7a06215 Mon Sep 17 00:00:00 2001 From: Pavel Vymetálek Date: Tue, 13 Feb 2024 16:24:10 +0100 Subject: Flush serial reads data as long as there is any left - fix length of received data with ACK message --- sercp.c | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) (limited to 'sercp.c') diff --git a/sercp.c b/sercp.c index 9c8cf51..9607482 100644 --- a/sercp.c +++ b/sercp.c @@ -68,7 +68,7 @@ #define FALSE 0 #define true 1 #define TRUE 1 -const char* _version = "v0.4.0"; +const char* _version = "v0.8.0-pre1"; // SERIAL FILE *tapout_fd = NULL; int is_outfile = 0; @@ -230,15 +230,27 @@ void TestArgs (int argc, char *argv[]) //***************************************************************************** +// TODO read bytes from serial port for 200ms void FlushSerialPort() { -if (serial_fd) { - sleep_ms(20); + ssize_t len = 0; + if (serial_fd) { + sleep_ms(20); #ifdef _WIN32 + unsigned long ulNumBytes = 0; PurgeComm(serial_fd, PURGE_RXABORT| PURGE_TXABORT | PURGE_RXCLEAR | PURGE_TXCLEAR); + do { + sleep_ms(100); + ReadFile(serial_fd, p_buff, 100, &ulNumBytes, NULL); + len = (size_t) ulNumBytes; + } while (len > 1); #else tcflush(serial_fd, TCIOFLUSH); - #endif + do { + sleep_ms(100); + len = read (serial_fd, p_buff, 100); + } while (len > 1); +#endif } } @@ -455,7 +467,7 @@ uint8_t WaitReadAck(void) { unsigned long ulNumBytes =0; while (1) { sleep_ms(10); - ReadFile(serial_fd, &buf_ack[len], 6, &ulNumBytes, NULL); + ReadFile(serial_fd, &buf_ack[len], sizeof(buf_ack)-len, &ulNumBytes, NULL); len += (size_t) ulNumBytes; if (len == 6) break; } @@ -473,7 +485,7 @@ uint8_t WaitReadAck(void) { CloseSerialPort(); exit(EXIT_FAILURE); } else if (result) { - len += read (serial_fd, &buf_ack[len], 6); + len += read (serial_fd, &buf_ack[len], sizeof(buf_ack)-len); if (len == 6) { break; } @@ -503,6 +515,10 @@ void RecvAckFileinfo(void) { if (WaitReadAck() == 6) { if (CheckAckSum()) { printf("\nErr Ack fileinfo\n"); + for (int x=0; x<6; x++) { + printf("%2X ",buf_ack[x]); + } + printf ("\n"); exit(-1); }; }; @@ -514,6 +530,10 @@ void RecvAckBlock(uint8_t block_number) { if (WaitReadAck() == 6) { if (CheckAckSum() || (buf_ack[3] != block_number)) { printf("\nErr ACK block\n"); + for (int x=0; x<6; x++) { + printf("%2X ",buf_ack[x]); + } + printf ("\n"); exit(-1); } } @@ -849,7 +869,6 @@ int OpenUart() { if (SetCommTimeouts(serial_fd, &sCommTimeouts) == 0) { return (-1); } - FlushSerialPort(); return (0); #else struct termios oldtio, newtio; @@ -939,7 +958,6 @@ int OpenUart() { newtio.c_cc[VTIME] = 1; // time to wait tcsetattr(serial_fd, TCSANOW, &newtio); sleep_ms(20); - tcflush(serial_fd, TCIOFLUSH); return 0; #endif } @@ -963,7 +981,7 @@ int main(int argc, char** argv, char** env) printf ("Can't open serial port\n"); exit (EXIT_FAILURE); } - FlushSerialPort(); + FlushSerialPort(); // make the input fifo empty #ifndef _WIN32 spolfd_serial[0].fd = serial_fd; // watching descriptor spolfd_serial[0].events = POLLIN; // watch data on input -- cgit