]> sigrok.org Git - libsigrok.git/blobdiff - hardware/common/scpi_serial.c
Route sr_source_remove for all serial devices through a wrapper.
[libsigrok.git] / hardware / common / scpi_serial.c
index c56e524ff052dff694481be86c06b72704d6843e..6c5415c5daeb26ea2693359025f7706c3326b97d 100644 (file)
@@ -53,33 +53,37 @@ SR_PRIV int scpi_serial_source_add(void *priv, int events, int timeout,
 {
        struct sr_serial_dev_inst *serial = priv;
 
-       return sr_source_add(serial->fd, events, timeout, cb, cb_data);
+       return serial_source_add(serial, events, timeout, cb, cb_data);
 }
 
 SR_PRIV int scpi_serial_source_remove(void *priv)
 {
        struct sr_serial_dev_inst *serial = priv;
 
-       return sr_source_remove(serial->fd);
+       return serial_source_remove(serial);
 }
 
 SR_PRIV int scpi_serial_send(void *priv, const char *command)
 {
-       int len, out;
+       int len, result, written;
        gchar *terminated_command;
        struct sr_serial_dev_inst *serial = priv;
 
        terminated_command = g_strconcat(command, "\n", NULL);
        len = strlen(terminated_command);
-       out = serial_write(serial, terminated_command, len);
-       g_free(terminated_command);
-
-       if (out != len) {
-               sr_dbg("Only sent %d/%d bytes of SCPI command: '%s'.", out,
-                      len, command);
-               return SR_ERR;
+       written = 0;
+       while (written < len) {
+               result = serial_write(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;
        }
 
+       g_free(terminated_command);
+
        sr_spew("Successfully sent SCPI command: '%s'.", command);
 
        return SR_OK;
@@ -138,6 +142,20 @@ SR_PRIV int scpi_serial_receive(void *priv, char **scpi_response)
        return ret;
 }
 
+/* Some stubs to keep the compiler from whining. */
+static int scpi_serial_read(void *priv, char *buf, int maxlen)
+{
+       return serial_read(priv, buf, maxlen);
+}
+static int scpi_serial_close(void *priv)
+{
+       return serial_close(priv);
+}
+static void scpi_serial_free(void *priv)
+{
+       return sr_serial_dev_inst_free(priv);
+}
+
 SR_PRIV struct sr_scpi_dev_inst *scpi_serial_dev_inst_new(const char *port,
                const char *serialcomm)
 {
@@ -157,9 +175,9 @@ SR_PRIV struct sr_scpi_dev_inst *scpi_serial_dev_inst_new(const char *port,
        scpi->source_remove = scpi_serial_source_remove;
        scpi->send = scpi_serial_send;
        scpi->receive = scpi_serial_receive;
-       scpi->read = serial_read;
-       scpi->close = serial_close;
-       scpi->free = sr_serial_dev_inst_free;
+       scpi->read = scpi_serial_read;
+       scpi->close = scpi_serial_close;
+       scpi->free = scpi_serial_free;
        scpi->priv = serial;
 
        return scpi;