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 = %d.",
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: iberr = %d.", iberr);
125 gscpi->read_started = 1;
130 static int scpi_gpib_read_complete(void *priv)
132 struct scpi_gpib *gscpi = priv;
134 return gscpi->read_started && (ibsta & END);
137 static int scpi_gpib_close(struct sr_scpi_dev_inst *scpi)
139 struct scpi_gpib *gscpi = scpi->priv;
141 ibonl(gscpi->descriptor, 0);
146 static void scpi_gpib_free(void *priv)
148 struct scpi_gpib *gscpi = priv;
153 SR_PRIV const struct sr_scpi_dev_inst scpi_libgpib_dev = {
156 .priv_size = sizeof(struct scpi_gpib),
157 .dev_inst_new = scpi_gpib_dev_inst_new,
158 .open = scpi_gpib_open,
159 .source_add = scpi_gpib_source_add,
160 .source_remove = scpi_gpib_source_remove,
161 .send = scpi_gpib_send,
162 .read_begin = scpi_gpib_read_begin,
163 .read_data = scpi_gpib_read_data,
164 .read_complete = scpi_gpib_read_complete,
165 .close = scpi_gpib_close,
166 .free = scpi_gpib_free,