From: Stefan BrĂ¼ns Date: Sat, 16 Apr 2016 21:38:05 +0000 (+0200) Subject: spci: Terminate all commands with a linefeed for all transports X-Git-Tag: libsigrok-0.5.0~506 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=055804e89ea0f6b4145040a0eeb2f4e6951692fe;p=libsigrok.git spci: Terminate all commands with a linefeed for all transports While some transports add a terminating (carriagereturn+)linefeed unconditionally, the USBTMC transport does not. At least the R&S HMO1002 requires the linefeed and locks up otherwise. Fixes bug #784. This changes the TCP and VXI transport from CR+LF to LF only. Also fixes a possible memory leak for VXI, where the temporary command buffer was not freed in case of a write error. --- diff --git a/src/scpi/scpi.c b/src/scpi/scpi.c index fc7e9939..9f5dba43 100644 --- a/src/scpi/scpi.c +++ b/src/scpi/scpi.c @@ -296,8 +296,10 @@ SR_PRIV int sr_scpi_send_variadic(struct sr_scpi_dev_inst *scpi, va_end(args_copy); /* Allocate buffer and write out command. */ - buf = g_malloc(len + 1); + buf = g_malloc0(len + 2); vsprintf(buf, format, args); + if (buf[len - 1] != '\n') + buf[len] = '\n'; /* Send command. */ ret = scpi->send(scpi->priv, buf); diff --git a/src/scpi/scpi_serial.c b/src/scpi/scpi_serial.c index 096a77c1..02242ef6 100644 --- a/src/scpi/scpi_serial.c +++ b/src/scpi/scpi_serial.c @@ -123,26 +123,21 @@ 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; struct scpi_serial *sscpi = priv; struct sr_serial_dev_inst *serial = sscpi->serial; - terminated_command = g_strconcat(command, "\n", NULL); - len = strlen(terminated_command); + len = strlen(command); written = 0; while (written < len) { result = serial_write_nonblocking(serial, - terminated_command + written, len - written); + 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; } - g_free(terminated_command); - sr_spew("Successfully sent SCPI command: '%s'.", command); return SR_OK; diff --git a/src/scpi/scpi_tcp.c b/src/scpi/scpi_tcp.c index 62974680..e87f5e78 100644 --- a/src/scpi/scpi_tcp.c +++ b/src/scpi/scpi_tcp.c @@ -135,12 +135,9 @@ static int scpi_tcp_send(void *priv, const char *command) { struct scpi_tcp *tcp = priv; int len, out; - char *terminated_command; - terminated_command = g_strdup_printf("%s\r\n", command); - len = strlen(terminated_command); - out = send(tcp->socket, terminated_command, len, 0); - g_free(terminated_command); + len = strlen(command); + out = send(tcp->socket, command, len, 0); if (out < 0) { sr_err("Send error: %s", g_strerror(errno)); diff --git a/src/scpi/scpi_visa.c b/src/scpi/scpi_visa.c index 220ead4f..a8f50e4f 100644 --- a/src/scpi/scpi_visa.c +++ b/src/scpi/scpi_visa.c @@ -87,21 +87,16 @@ static int scpi_visa_source_remove(struct sr_session *session, void *priv) static int scpi_visa_send(void *priv, const char *command) { struct scpi_visa *vscpi = priv; - gchar *terminated_command; ViUInt32 written = 0; int len; - terminated_command = g_strconcat(command, "\n", NULL); - len = strlen(terminated_command); - if (viWrite(vscpi->vi, (ViBuf) (terminated_command + written), len, + len = strlen(command); + if (viWrite(vscpi->vi, (ViBuf) (command + written), len, &written) != VI_SUCCESS) { sr_err("Error while sending SCPI command: '%s'.", command); - g_free(terminated_command); return SR_ERR; } - g_free(terminated_command); - sr_spew("Successfully sent SCPI command: '%s'.", command); return SR_OK; diff --git a/src/scpi/scpi_vxi.c b/src/scpi/scpi_vxi.c index 8a99ec74..9f6b2c35 100644 --- a/src/scpi/scpi_vxi.c +++ b/src/scpi/scpi_vxi.c @@ -118,18 +118,16 @@ static int scpi_vxi_send(void *priv, const char *command) struct scpi_vxi *vxi = priv; Device_WriteResp *write_resp; Device_WriteParms write_parms; - char *terminated_command; unsigned long len; - terminated_command = g_strdup_printf("%s\r\n", command); - len = strlen(terminated_command); + len = strlen(command); write_parms.lid = vxi->link; write_parms.io_timeout = VXI_DEFAULT_TIMEOUT_MS; write_parms.lock_timeout = VXI_DEFAULT_TIMEOUT_MS; write_parms.flags = DF_END; write_parms.data.data_len = MIN(len, vxi->max_send_size); - write_parms.data.data_val = terminated_command; + write_parms.data.data_val = command; if (!(write_resp = device_write_1(&write_parms, vxi->client)) || write_resp->error) { @@ -138,8 +136,6 @@ static int scpi_vxi_send(void *priv, const char *command) return SR_ERR; } - g_free(terminated_command); - if (write_resp->size < len) sr_dbg("Only sent %lu/%lu bytes of SCPI command: '%s'.", write_resp->size, len, command);