2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 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/>.
23 #include <libsigrok/libsigrok.h>
24 #include "libsigrok-internal.h"
27 #define LOG_PREFIX "scpi_gpib"
35 static int scpi_gpib_dev_inst_new(void *priv, struct drv_context *drvc,
36 const char *resource, char **params, const char *serialcomm)
38 struct scpi_gpib *gscpi = priv;
44 if (!params || !params[1])
47 gscpi->name = g_strdup(params[1]);
52 static int scpi_gpib_open(struct sr_scpi_dev_inst *scpi)
54 struct scpi_gpib *gscpi = scpi->priv;
56 if ((gscpi->descriptor = ibfind(gscpi->name)) < 0)
62 static int scpi_gpib_source_add(struct sr_session *session, void *priv,
63 int events, int timeout, sr_receive_data_callback cb, void *cb_data)
67 /* Hook up a dummy handler to receive data from the device. */
68 return sr_session_source_add(session, -1, events, timeout, cb, cb_data);
71 static int scpi_gpib_source_remove(struct sr_session *session, void *priv)
75 return sr_session_source_remove(session, -1);
78 static int scpi_gpib_send(void *priv, const char *command)
80 struct scpi_gpib *gscpi = priv;
81 int len = strlen(command);
83 ibwrt(gscpi->descriptor, command, len);
87 sr_err("Error while sending SCPI command: '%s': iberr = %s.",
88 command, gpib_error_string(iberr));
94 sr_err("Failed to send all of SCPI command: '%s': "
95 "len = %d, ibcnt = %d.", command, len, ibcnt);
99 sr_spew("Successfully sent SCPI command: '%s'.", command);
104 static int scpi_gpib_read_begin(void *priv)
106 struct scpi_gpib *gscpi = priv;
108 gscpi->read_started = 0;
113 static int scpi_gpib_read_data(void *priv, char *buf, int maxlen)
115 struct scpi_gpib *gscpi = priv;
117 ibrd(gscpi->descriptor, buf, maxlen);
121 sr_err("Error while reading SCPI response: "
122 "iberr = %s, ibsta = %d.",
123 gpib_error_string(iberr), ibsta);
127 gscpi->read_started = 1;
132 static int scpi_gpib_read_complete(void *priv)
134 struct scpi_gpib *gscpi = priv;
136 return gscpi->read_started && (ibsta & END);
139 static int scpi_gpib_close(struct sr_scpi_dev_inst *scpi)
141 struct scpi_gpib *gscpi = scpi->priv;
143 /* Put device in back local mode to prevent lock-out of front panel. */
144 ibloc(gscpi->descriptor);
145 /* Now it's safe to close the handle. */
146 ibonl(gscpi->descriptor, 0);
151 static void scpi_gpib_free(void *priv)
153 struct scpi_gpib *gscpi = priv;
158 SR_PRIV int sr_scpi_gpib_spoll(struct sr_scpi_dev_inst *scpi, char *buf)
160 struct scpi_gpib *gscpi = scpi->priv;
162 ibrsp(gscpi->descriptor, buf);
165 sr_err("Error while serial polling: iberr = %s.",
166 gpib_error_string(iberr));
169 sr_spew("Successful serial poll: 0x%x", (uint8_t)buf[0]);
174 SR_PRIV const struct sr_scpi_dev_inst scpi_libgpib_dev = {
177 .priv_size = sizeof(struct scpi_gpib),
178 .dev_inst_new = scpi_gpib_dev_inst_new,
179 .open = scpi_gpib_open,
180 .source_add = scpi_gpib_source_add,
181 .source_remove = scpi_gpib_source_remove,
182 .send = scpi_gpib_send,
183 .read_begin = scpi_gpib_read_begin,
184 .read_data = scpi_gpib_read_data,
185 .read_complete = scpi_gpib_read_complete,
186 .close = scpi_gpib_close,
187 .free = scpi_gpib_free,