From 6dc0007c7152b9dc3af49f85f0c33b31813c72a6 Mon Sep 17 00:00:00 2001 From: Gerhard Sittig Date: Mon, 17 Aug 2020 18:48:50 +0200 Subject: [PATCH] scpi_serial: Rephrase NL and NL+CR line termination check. Rearrange the check for line termination in SCPI receive data, and add a comment in that spot. Keep related conditions together, avoid line breaks for complex terms. This shall simplify review, and raise awareness during maintenance. This change amends commit a0ade2f933d6. --- src/scpi/scpi_serial.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/scpi/scpi_serial.c b/src/scpi/scpi_serial.c index f0ec4c4d..f627deef 100644 --- a/src/scpi/scpi_serial.c +++ b/src/scpi/scpi_serial.c @@ -181,22 +181,22 @@ static int scpi_serial_read_data(void *priv, char *buf, int maxlen) /* Try to read new data into the buffer. */ ret = serial_read_nonblocking(sscpi->serial, buf, maxlen); - if (ret < 0) return ret; - if (ret > 0) { - if (buf[ret - 1] == '\n') { - sscpi->got_newline = TRUE; - sr_spew("Received NL terminator"); - } else if (ret > 1 && - buf[ret - 2] == '\n' && buf[ret - 1] == '\r') { - sscpi->got_newline = TRUE; - sr_spew("Received NL+CR terminator"); - ret--; - } else { - sscpi->got_newline = FALSE; - } + /* + * Check for line termination at the end of the receive data. + * Handle the usual case of NL, as well as the unusual NL+CR + * combination (some GWInstek DMMs were found to do this). + */ + sscpi->got_newline = FALSE; + if (ret >= 1 && buf[ret - 1] == '\n') { + sscpi->got_newline = TRUE; + sr_spew("Received NL terminator"); + } else if (ret >= 2 && buf[ret - 2] == '\n' && buf[ret - 1] == '\r') { + ret--; + sscpi->got_newline = TRUE; + sr_spew("Received NL+CR terminator"); } return ret;