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/>.
20 #include "libsigrok.h"
21 #include "libsigrok-internal.h"
26 #define LOG_PREFIX "scpi_gpib"
34 static int scpi_gpib_dev_inst_new(void *priv, struct drv_context *drvc,
35 const char *resource, char **params, const char *serialcomm)
37 struct scpi_gpib *gscpi = priv;
43 if (!params || !params[1])
46 gscpi->name = g_strdup(params[1]);
51 static int scpi_gpib_open(void *priv)
53 struct scpi_gpib *gscpi = priv;
55 if ((gscpi->descriptor = ibfind(gscpi->name)) < 0)
61 static int scpi_gpib_source_add(struct sr_session *session, void *priv,
62 int events, int timeout, sr_receive_data_callback cb, void *cb_data)
66 /* Hook up a dummy handler to receive data from the device. */
67 return sr_session_source_add(session, -1, events, timeout, cb, cb_data);
70 static int scpi_gpib_source_remove(struct sr_session *session, void *priv)
74 return sr_session_source_remove(session, -1);
77 static int scpi_gpib_send(void *priv, const char *command)
79 struct scpi_gpib *gscpi = priv;
80 int len = strlen(command);
82 ibwrt(gscpi->descriptor, command, len);
86 sr_err("Error while sending SCPI command: '%s': iberr = %d.",
93 sr_err("Failed to send all of SCPI command: '%s': "
94 "len = %d, ibcnt = .", command, len, ibcnt);
98 sr_spew("Successfully sent SCPI command: '%s'.", command);
103 static int scpi_gpib_read_begin(void *priv)
105 struct scpi_gpib *gscpi = priv;
107 gscpi->read_started = 0;
112 static int scpi_gpib_read_data(void *priv, char *buf, int maxlen)
114 struct scpi_gpib *gscpi = priv;
116 ibrd(gscpi->descriptor, buf, maxlen);
120 sr_err("Error while reading SCPI response: iberr = %d.", iberr);
124 gscpi->read_started = 1;
129 static int scpi_gpib_read_complete(void *priv)
131 struct scpi_gpib *gscpi = priv;
133 return gscpi->read_started && (ibsta & END);
136 static int scpi_gpib_close(void *priv)
138 struct scpi_gpib *gscpi = priv;
140 ibonl(gscpi->descriptor, 0);
145 static void scpi_gpib_free(void *priv)
147 struct scpi_gpib *gscpi = priv;
152 SR_PRIV const struct sr_scpi_dev_inst scpi_libgpib_dev = {
155 .priv_size = sizeof(struct scpi_gpib),
156 .dev_inst_new = scpi_gpib_dev_inst_new,
157 .open = scpi_gpib_open,
158 .source_add = scpi_gpib_source_add,
159 .source_remove = scpi_gpib_source_remove,
160 .send = scpi_gpib_send,
161 .read_begin = scpi_gpib_read_begin,
162 .read_data = scpi_gpib_read_data,
163 .read_complete = scpi_gpib_read_complete,
164 .close = scpi_gpib_close,
165 .free = scpi_gpib_free,