]> sigrok.org Git - libsigrok.git/commitdiff
spci: Terminate all commands with a linefeed for all transports
authorStefan Brüns <redacted>
Sat, 16 Apr 2016 21:38:05 +0000 (23:38 +0200)
committerUwe Hermann <redacted>
Sat, 23 Apr 2016 15:08:50 +0000 (17:08 +0200)
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.

src/scpi/scpi.c
src/scpi/scpi_serial.c
src/scpi/scpi_tcp.c
src/scpi/scpi_visa.c
src/scpi/scpi_vxi.c

index fc7e9939c0e72ae5dfe173b25147ead17ffe5143..9f5dba43ae493a5b4558b1970186c07ddd69b417 100644 (file)
@@ -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);
index 096a77c1e74fefe823523dafd950496883f6c366..02242ef66b5dc86d36502e6b352e9e6b5b1079b1 100644 (file)
@@ -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;
index 62974680aa93bdc99b321c5daeff5196af842134..e87f5e784fb1d0ab61b0eaac8f44b06091b04076 100644 (file)
@@ -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));
index 220ead4fd3e31db1a568ddeb62240cb69db960cd..a8f50e4f65c71fde5451d19032e80a78e610d0e8 100644 (file)
@@ -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;
index 8a99ec7474b3e5cee740f9e6367e0faa4651d315..9f6b2c356ea1444ce4def493014fca4766e1ef0d 100644 (file)
@@ -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);