]> sigrok.org Git - libsigrok.git/blobdiff - hardware/common/serial.c
comment fix
[libsigrok.git] / hardware / common / serial.c
index d688f7d1f090cdebd0a0f29b064ef07d30d0d8a7..220533a41b753f9d6844801574963b1d95f4f657 100644 (file)
@@ -126,6 +126,8 @@ SR_PRIV int serial_open(const char *pathname, int flags)
                 */
                sr_dbg("Error opening serial port '%s': %s.", pathname,
                       strerror(errno));
+       } else {
+               sr_dbg("Opened serial port '%s' as FD %d.", pathname, fd);
        }
 
        return fd;
@@ -195,8 +197,6 @@ SR_PRIV int serial_flush(int fd)
  */
 SR_PRIV int serial_write(int fd, const void *buf, size_t count)
 {
-       sr_spew("FD %d: Writing %d bytes.", fd, count);
-
 #ifdef _WIN32
        DWORD tmp = 0;
 
@@ -210,8 +210,8 @@ SR_PRIV int serial_write(int fd, const void *buf, size_t count)
        ret = write(fd, buf, count);
        if (ret < 0)
                sr_err("FD %d: Write error: %s.", fd, strerror(errno));
-       else if ((size_t)ret != count)
-               sr_spew("FD %d: Only wrote %d/%d bytes.", fd, ret, count);
+       else
+               sr_spew("FD %d: Wrote %d/%d bytes.", fd, ret, count);
 
        return ret;
 #endif
@@ -228,8 +228,6 @@ SR_PRIV int serial_write(int fd, const void *buf, size_t count)
  */
 SR_PRIV int serial_read(int fd, void *buf, size_t count)
 {
-       sr_spew("FD %d: Reading %d bytes.", fd, count);
-
 #ifdef _WIN32
        DWORD tmp = 0;
 
@@ -247,8 +245,8 @@ SR_PRIV int serial_read(int fd, void *buf, size_t count)
                 * "Resource temporarily unavailable" messages.
                 */
                sr_spew("FD %d: Read error: %s.", fd, strerror(errno));
-       } else if ((size_t)ret != count) {
-               sr_spew("FD %d: Only read %d/%d bytes.", fd, ret, count);
+       } else {
+               sr_spew("FD %d: Read %d/%d bytes.", fd, ret, count);
        }
 
        return ret;
@@ -303,6 +301,11 @@ SR_PRIV int serial_restore_params(int fd, void *backup)
 {
        sr_dbg("FD %d: Restoring serial parameters from backup.", fd);
 
+       if (!backup) {
+               sr_err("FD %d: Cannot restore serial params (NULL).", fd);
+               return -1;
+       }
+
 #ifdef _WIN32
        /* TODO */
 #else
@@ -384,7 +387,7 @@ SR_PRIV int serial_set_params(int fd, int baudrate, int bits, int parity,
 
        sr_dbg("FD %d: Getting terminal settings.", fd);
        if (tcgetattr(fd, &term) < 0) {
-               sr_err("tcgetattr() error: %ѕ.", strerror(errno));
+               sr_err("tcgetattr() error: %s.", strerror(errno));
                return SR_ERR;
        }
 
@@ -596,9 +599,9 @@ SR_PRIV int serial_set_paramstr(int fd, const char *paramstr)
 }
 
 SR_PRIV int serial_readline(int fd, char **buf, int *buflen,
-                           uint64_t timeout_ms)
+                           gint64 timeout_ms)
 {
-       uint64_t start;
+       gint64 start;
        int maxlen, len;
 
        timeout_ms *= 1000;
@@ -614,8 +617,9 @@ SR_PRIV int serial_readline(int fd, char **buf, int *buflen,
                if (len > 0) {
                        *buflen += len;
                        *(*buf + *buflen) = '\0';
-                       if (*buflen > 0 && *(*buf + *buflen - 1) == '\r') {
-                               /* Strip LF and terminate. */
+                       if (*buflen > 0 && (*(*buf + *buflen - 1) == '\r'
+                                       || *(*buf + *buflen - 1) == '\n')) {
+                               /* Strip CR/LF and terminate. */
                                *(*buf + --*buflen) = '\0';
                                break;
                        }
@@ -625,7 +629,8 @@ SR_PRIV int serial_readline(int fd, char **buf, int *buflen,
                        break;
                g_usleep(2000);
        }
-       sr_dbg("Received %d: '%s'.", *buflen, *buf);
+       if (*buflen)
+               sr_dbg("Received %d: '%s'.", *buflen, *buf);
 
        return SR_OK;
 }