X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fscpi%2Fscpi_serial.c;h=067838adce5f04bc19e379b0a6e13412da2870f3;hb=22fdb67fa0714c11cc0a58ee1423f55d18a4f080;hp=255416743cfda05e37b370b5da3efee1ddcd945a;hpb=8107a9a650f4db08423c3fc79c1b7e7d826ffd7f;p=libsigrok.git diff --git a/src/scpi/scpi_serial.c b/src/scpi/scpi_serial.c index 25541674..067838ad 100644 --- a/src/scpi/scpi_serial.c +++ b/src/scpi/scpi_serial.c @@ -28,11 +28,14 @@ #define LOG_PREFIX "scpi_serial" +#ifdef HAVE_SERIAL_COMM + struct scpi_serial { struct sr_serial_dev_inst *serial; gboolean got_newline; }; +/* Default serial port options for some known USB devices */ static const struct { uint16_t vendor_id; uint16_t product_id; @@ -40,7 +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, 0x0118, "115200/8n1" }, /* R&S HMO1002 */ + { 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) @@ -72,11 +79,30 @@ 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; @@ -90,9 +116,6 @@ static int scpi_serial_open(struct sr_scpi_dev_inst *scpi) 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; @@ -159,17 +182,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 terminator"); - } 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; @@ -199,6 +227,7 @@ 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, @@ -213,3 +242,5 @@ SR_PRIV const struct sr_scpi_dev_inst scpi_serial_dev = { .close = scpi_serial_close, .free = scpi_serial_free, }; + +#endif