]> sigrok.org Git - libsigrok.git/blobdiff - src/scpi/scpi_serial.c
scpi_serial: add "GWInstek VCP" (PID 0x0030) as seen in GDM-834x
[libsigrok.git] / src / scpi / scpi_serial.c
index 257ff88380c55096688c50422addd1ac5220a41d..067838adce5f04bc19e379b0a6e13412da2870f3 100644 (file)
 
 #define LOG_PREFIX "scpi_serial"
 
-#define BUFFER_SIZE 1024
+#ifdef HAVE_SERIAL_COMM
 
 struct scpi_serial {
        struct sr_serial_dev_inst *serial;
-       char buffer[BUFFER_SIZE];
-       size_t count;
-       size_t read;
+       gboolean got_newline;
 };
 
+/* Default serial port options for some known USB devices */
 static const struct {
        uint16_t vendor_id;
        uint16_t product_id;
@@ -44,6 +43,11 @@ static const struct {
 } scpi_serial_usb_ids[] = {
        { 0x0403, 0xed72, "115200/8n1/flow=1" }, /* Hameg HO720 */
        { 0x0403, 0xed73, "115200/8n1/flow=1" }, /* Hameg HO730 */
+       { 0x0aad, 0x0117, "115200/8n1" },        /* R&S HMO series, previously branded as Hameg HMO */
+       { 0x0aad, 0x0118, "115200/8n1" },        /* R&S HMO series, previously branded as Hameg HMO */
+       { 0x0aad, 0x0119, "115200/8n1" },        /* R&S HMO series, previously branded as Hameg HMO */
+       { 0x2184, 0x0030, "115200/8n1" },        /* GW-Instek GDM-8341 (VCP, SiLabs CP210x) */
+       { 0x2184, 0x0058, "115200/8n1" },        /* GW-Instek GDM-9061 (USBCDC mode) */
 };
 
 static GSList *scpi_serial_scan(struct drv_context *drvc)
@@ -75,29 +79,55 @@ static GSList *scpi_serial_scan(struct drv_context *drvc)
 static int scpi_serial_dev_inst_new(void *priv, struct drv_context *drvc,
                const char *resource, char **params, const char *serialcomm)
 {
+       GSList *l, *r;
+       unsigned i;
        struct scpi_serial *sscpi = priv;
 
        (void)drvc;
        (void)params;
 
+       /* If no serial port option is specified on the command-line using the
+        * "serialcomm" driver option, but the device is connected through USB
+        * and it requires a known default serial port option, then used it in
+        * order to avoid data corruption or even worse problems.
+        */
+       if (!serialcomm) {
+               for (i = 0; i < ARRAY_SIZE(scpi_serial_usb_ids); i++) {
+                       if (!(l = sr_serial_find_usb(scpi_serial_usb_ids[i].vendor_id,
+                                               scpi_serial_usb_ids[i].product_id)))
+                               continue;
+                       for (r = l; r; r = r->next)
+                               if (!strcmp(resource, r->data) && scpi_serial_usb_ids[i].serialcomm)
+                                       serialcomm = scpi_serial_usb_ids[i].serialcomm;
+                       g_slist_free_full(l, g_free);
+               }
+       }
+
        sscpi->serial = sr_serial_dev_inst_new(resource, serialcomm);
 
        return SR_OK;
 }
 
-static int scpi_serial_open(void *priv)
+static int scpi_serial_open(struct sr_scpi_dev_inst *scpi)
 {
-       struct scpi_serial *sscpi = priv;
+       struct scpi_serial *sscpi = scpi->priv;
        struct sr_serial_dev_inst *serial = sscpi->serial;
 
        if (serial_open(serial, SERIAL_RDWR) != SR_OK)
                return SR_ERR;
 
-       if (serial_flush(serial) != SR_OK)
-               return SR_ERR;
+       sscpi->got_newline = FALSE;
+
+       return SR_OK;
+}
 
-       sscpi->count = 0;
-       sscpi->read = 0;
+static int scpi_serial_connection_id(struct sr_scpi_dev_inst *scpi,
+               char **connection_id)
+{
+       struct scpi_serial *sscpi = scpi->priv;
+       struct sr_serial_dev_inst *serial = sscpi->serial;
+
+       *connection_id = g_strdup(serial->port);
 
        return SR_OK;
 }
@@ -121,27 +151,17 @@ static int scpi_serial_source_remove(struct sr_session *session, void *priv)
 
 static int scpi_serial_send(void *priv, const char *command)
 {
-       int len, result, written;
-       gchar *terminated_command;
+       int result;
        struct scpi_serial *sscpi = priv;
        struct sr_serial_dev_inst *serial = sscpi->serial;
 
-       terminated_command = g_strconcat(command, "\n", NULL);
-       len = strlen(terminated_command);
-       written = 0;
-       while (written < len) {
-               result = serial_write_nonblocking(serial,
-                               terminated_command + written, len - written);
-               if (result < 0) {
-                       sr_err("Error while sending SCPI command: '%s'.", command);
-                       g_free(terminated_command);
-                       return SR_ERR;
-               }
-               written += result;
+       result = serial_write_blocking(serial, command, strlen(command), 0);
+       if (result < 0) {
+               sr_err("Error while sending SCPI command '%s': %d.",
+                       command, result);
+               return SR_ERR;
        }
 
-       g_free(terminated_command);
-
        sr_spew("Successfully sent SCPI command: '%s'.", command);
 
        return SR_OK;
@@ -149,7 +169,8 @@ static int scpi_serial_send(void *priv, const char *command)
 
 static int scpi_serial_read_begin(void *priv)
 {
-       (void) priv;
+       struct scpi_serial *sscpi = priv;
+       sscpi->got_newline = FALSE;
 
        return SR_OK;
 }
@@ -157,61 +178,41 @@ static int scpi_serial_read_begin(void *priv)
 static int scpi_serial_read_data(void *priv, char *buf, int maxlen)
 {
        struct scpi_serial *sscpi = priv;
-       int len, ret;
-
-       len = BUFFER_SIZE - sscpi->count;
-
-       /* Try to read new data into the buffer if there is space. */
-       if (len > 0) {
-               ret = serial_read_nonblocking(sscpi->serial, sscpi->buffer + sscpi->count,
-                               BUFFER_SIZE - sscpi->count);
-
-               if (ret < 0)
-                       return ret;
-
-               sscpi->count += ret;
-
-               if (ret > 0)
-                       sr_spew("Read %d bytes into buffer.", ret);
+       int ret;
+
+       /* Try to read new data into the buffer. */
+       ret = serial_read_nonblocking(sscpi->serial, buf, maxlen);
+       if (ret < 0)
+               return ret;
+
+       /*
+        * 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 as many bytes as possible from buffer, excluding any trailing newline. */
-       if (sscpi->read < sscpi->count) {
-               len = sscpi->count - sscpi->read;
-               if (len > maxlen)
-                       len = maxlen;
-               if (sscpi->buffer[sscpi->read + len - 1] == '\n')
-                       len--;
-               sr_spew("Returning %d bytes from buffer.", len);
-               memcpy(buf, sscpi->buffer + sscpi->read, len);
-               sscpi->read += len;
-               if (sscpi->read == BUFFER_SIZE) {
-                       sr_spew("Resetting buffer.");
-                       sscpi->count = 0;
-                       sscpi->read = 0;
-               }
-               return len;
-       }
-
-       return 0;
+       return ret;
 }
 
 static int scpi_serial_read_complete(void *priv)
 {
        struct scpi_serial *sscpi = priv;
 
-       /* If the next character is a newline, discard it and report complete. */
-       if (sscpi->read < sscpi->count && sscpi->buffer[sscpi->read] == '\n') {
-               sscpi->read++;
-               return 1;
-       } else {
-               return 0;
-       }
+       return sscpi->got_newline;
 }
 
-static int scpi_serial_close(void *priv)
+static int scpi_serial_close(struct sr_scpi_dev_inst *scpi)
 {
-       struct scpi_serial *sscpi = priv;
+       struct scpi_serial *sscpi = scpi->priv;
 
        return serial_close(sscpi->serial);
 }
@@ -226,10 +227,12 @@ static void scpi_serial_free(void *priv)
 SR_PRIV const struct sr_scpi_dev_inst scpi_serial_dev = {
        .name          = "serial",
        .prefix        = "",
+       .transport     = SCPI_TRANSPORT_SERIAL,
        .priv_size     = sizeof(struct scpi_serial),
        .scan          = scpi_serial_scan,
        .dev_inst_new  = scpi_serial_dev_inst_new,
        .open          = scpi_serial_open,
+       .connection_id = scpi_serial_connection_id,
        .source_add    = scpi_serial_source_add,
        .source_remove = scpi_serial_source_remove,
        .send          = scpi_serial_send,
@@ -239,3 +242,5 @@ SR_PRIV const struct sr_scpi_dev_inst scpi_serial_dev = {
        .close         = scpi_serial_close,
        .free          = scpi_serial_free,
 };
+
+#endif