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 #define LOG_PREFIX "scpi_usbtmc"
31 SR_PRIV int scpi_usbtmc_open(void *priv)
33 struct sr_usbtmc_dev_inst *usbtmc = priv;
35 if ((usbtmc->fd = open(usbtmc->device, O_RDWR)) < 0)
41 SR_PRIV int scpi_usbtmc_source_add(void *priv, int events, int timeout,
42 sr_receive_data_callback_t cb, void *cb_data)
44 struct sr_usbtmc_dev_inst *usbtmc = priv;
46 return sr_source_add(usbtmc->fd, events, timeout, cb, cb_data);
49 SR_PRIV int scpi_usbtmc_source_remove(void *priv)
51 struct sr_usbtmc_dev_inst *usbtmc = priv;
53 return sr_source_remove(usbtmc->fd);
56 SR_PRIV int scpi_usbtmc_send(void *priv, const char *command)
58 struct sr_usbtmc_dev_inst *usbtmc = priv;
61 len = strlen(command);
62 out = write(usbtmc->fd, command, len);
65 sr_err("Write error: %s", strerror(errno));
70 sr_dbg("Only sent %d/%d bytes of SCPI command: '%s'.", out,
74 sr_spew("Successfully sent SCPI command: '%s'.", command);
79 SR_PRIV int scpi_usbtmc_receive(void *priv, char **scpi_response)
81 struct sr_usbtmc_dev_inst *usbtmc = priv;
86 response = g_string_sized_new(1024);
88 len = read(usbtmc->fd, buf, sizeof(buf));
91 sr_err("Read error: %s", strerror(errno));
92 g_string_free(response, TRUE);
96 response = g_string_append_len(response, buf, len);
98 *scpi_response = response->str;
100 sr_dbg("SCPI response received (length %d): '%.50s'",
101 response->len, response->str);
103 g_string_free(response, FALSE);
108 SR_PRIV int scpi_usbtmc_read(void *priv, char *buf, int maxlen)
110 struct sr_usbtmc_dev_inst *usbtmc = priv;
113 len = read(usbtmc->fd, buf, maxlen);
116 sr_err("Read error: %s", strerror(errno));
123 SR_PRIV int scpi_usbtmc_close(void *priv)
125 struct sr_usbtmc_dev_inst *usbtmc = priv;
127 if (close(usbtmc->fd) < 0)
133 static void scpi_usbtmc_free(void *priv)
135 return sr_usbtmc_dev_inst_free(priv);
138 SR_PRIV struct sr_scpi_dev_inst *scpi_usbtmc_dev_inst_new(const char *device)
140 struct sr_scpi_dev_inst *scpi;
141 struct sr_usbtmc_dev_inst *usbtmc;
143 scpi = g_try_malloc(sizeof(struct sr_scpi_dev_inst));
145 if (!(usbtmc = sr_usbtmc_dev_inst_new(device)))
151 scpi->open = scpi_usbtmc_open;
152 scpi->source_add = scpi_usbtmc_source_add;
153 scpi->source_remove = scpi_usbtmc_source_remove;
154 scpi->send = scpi_usbtmc_send;
155 scpi->receive = scpi_usbtmc_receive;
156 scpi->read = scpi_usbtmc_read;
157 scpi->close = scpi_usbtmc_close;
158 scpi->free = scpi_usbtmc_free;