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.
This commit is contained in:
Stefan Brüns 2016-04-16 23:38:05 +02:00 committed by Uwe Hermann
parent 66836720f7
commit 055804e89e
5 changed files with 11 additions and 26 deletions

View 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);

View 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;

View 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));

View 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;

View 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);