2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Martin Ling <martin-sigrok@earth.li>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "libsigrok.h"
21 #include "libsigrok-internal.h"
29 /* Message logging helpers with subsystem-specific prefix string. */
30 #define LOG_PREFIX "scpi_usbtmc: "
31 #define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args)
32 #define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args)
33 #define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args)
34 #define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args)
35 #define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args)
37 SR_PRIV int scpi_usbtmc_open(void *priv)
39 struct sr_usbtmc_dev_inst *usbtmc = priv;
41 if ((usbtmc->fd = open(usbtmc->device, O_RDWR)) < 0)
47 SR_PRIV int scpi_usbtmc_source_add(void *priv, int events, int timeout,
48 sr_receive_data_callback_t cb, void *cb_data)
50 struct sr_usbtmc_dev_inst *usbtmc = priv;
52 return sr_source_add(usbtmc->fd, events, timeout, cb, cb_data);
55 SR_PRIV int scpi_usbtmc_source_remove(void *priv)
57 struct sr_usbtmc_dev_inst *usbtmc = priv;
59 return sr_source_remove(usbtmc->fd);
62 SR_PRIV int scpi_usbtmc_send(void *priv, const char *command)
64 struct sr_usbtmc_dev_inst *usbtmc = priv;
67 len = strlen(command);
68 out = write(usbtmc->fd, command, len);
71 sr_err("Write error: %s", strerror(errno));
76 sr_dbg("Only sent %d/%d bytes of SCPI command: '%s'.", out,
80 sr_spew("Successfully sent SCPI command: '%s'.", command);
85 SR_PRIV int scpi_usbtmc_receive(void *priv, char **scpi_response)
87 struct sr_usbtmc_dev_inst *usbtmc = priv;
92 response = g_string_sized_new(1024);
94 len = read(usbtmc->fd, buf, sizeof(buf));
97 sr_err("Read error: %s", strerror(errno));
98 g_string_free(response, TRUE);
102 response = g_string_append_len(response, buf, len);
104 *scpi_response = response->str;
106 sr_dbg("SCPI response received (length %d): '%.50s'",
107 response->len, response->str);
109 g_string_free(response, FALSE);
114 SR_PRIV int scpi_usbtmc_read(void *priv, char *buf, int maxlen)
116 struct sr_usbtmc_dev_inst *usbtmc = priv;
119 len = read(usbtmc->fd, buf, maxlen);
122 sr_err("Read error: %s", strerror(errno));
129 SR_PRIV int scpi_usbtmc_close(void *priv)
131 struct sr_usbtmc_dev_inst *usbtmc = priv;
133 if (close(usbtmc->fd) < 0)
139 static void scpi_usbtmc_free(void *priv)
141 return sr_usbtmc_dev_inst_free(priv);
144 SR_PRIV struct sr_scpi_dev_inst *scpi_usbtmc_dev_inst_new(const char *device)
146 struct sr_scpi_dev_inst *scpi;
147 struct sr_usbtmc_dev_inst *usbtmc;
149 scpi = g_try_malloc(sizeof(struct sr_scpi_dev_inst));
151 if (!(usbtmc = sr_usbtmc_dev_inst_new(device)))
157 scpi->open = scpi_usbtmc_open;
158 scpi->source_add = scpi_usbtmc_source_add;
159 scpi->source_remove = scpi_usbtmc_source_remove;
160 scpi->send = scpi_usbtmc_send;
161 scpi->receive = scpi_usbtmc_receive;
162 scpi->read = scpi_usbtmc_read;
163 scpi->close = scpi_usbtmc_close;
164 scpi->free = scpi_usbtmc_free;