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/>.
22 #include "libsigrok.h"
23 #include "libsigrok-internal.h"
25 #define LOG_PREFIX "scpi_visa"
33 static int scpi_visa_dev_inst_new(void *priv, struct drv_context *drvc,
34 const char *resource, char **params, const char *serialcomm)
36 struct scpi_visa *vscpi = priv;
42 if (!params || !params[1]) {
43 sr_err("Invalid parameters.");
47 vscpi->resource = g_strdup(params[1]);
52 static int scpi_visa_open(void *priv)
54 struct scpi_visa *vscpi = priv;
56 if (viOpenDefaultRM(&vscpi->rmgr) != VI_SUCCESS) {
57 sr_err("Cannot open default resource manager.");
61 if (viOpen(vscpi->rmgr, vscpi->resource, VI_NO_LOCK, 0, &vscpi->vi) != VI_SUCCESS) {
62 sr_err("Cannot open resource.");
69 static int scpi_visa_source_add(struct sr_session *session, void *priv,
70 int events, int timeout, sr_receive_data_callback cb, void *cb_data)
74 /* Hook up a dummy handler to receive data from the device. */
75 return sr_session_source_add(session, -1, events, timeout, cb, cb_data);
78 static int scpi_visa_source_remove(struct sr_session *session, void *priv)
82 return sr_session_source_remove(session, -1);
85 static int scpi_visa_send(void *priv, const char *command)
87 struct scpi_visa *vscpi = priv;
88 gchar *terminated_command;
92 terminated_command = g_strconcat(command, "\n", NULL);
93 len = strlen(terminated_command);
94 if (viWrite(vscpi->vi, (ViBuf) (terminated_command + written), len,
95 &written) != VI_SUCCESS) {
96 sr_err("Error while sending SCPI command: '%s'.", command);
97 g_free(terminated_command);
101 g_free(terminated_command);
103 sr_spew("Successfully sent SCPI command: '%s'.", command);
108 static int scpi_visa_read_begin(void *priv)
115 static int scpi_visa_read_data(void *priv, char *buf, int maxlen)
117 struct scpi_visa *vscpi = priv;
120 if (viRead(vscpi->vi, (ViBuf) buf, maxlen, &count) != VI_SUCCESS) {
121 sr_err("Read failed.");
128 static int scpi_visa_read_complete(void *priv)
130 struct scpi_visa *vscpi = priv;
133 if (viReadSTB(vscpi->vi, &status) != VI_SUCCESS) {
134 sr_err("Failed to read status.");
138 return !(status & 16);
141 static int scpi_visa_close(void *priv)
143 struct scpi_visa *vscpi = priv;
146 viClose(vscpi->rmgr);
151 static void scpi_visa_free(void *priv)
153 struct scpi_visa *vscpi = priv;
155 g_free(vscpi->resource);
159 SR_PRIV const struct sr_scpi_dev_inst scpi_visa_dev = {
162 .priv_size = sizeof(struct scpi_visa),
163 .dev_inst_new = scpi_visa_dev_inst_new,
164 .open = scpi_visa_open,
165 .source_add = scpi_visa_source_add,
166 .source_remove = scpi_visa_source_remove,
167 .send = scpi_visa_send,
168 .read_begin = scpi_visa_read_begin,
169 .read_data = scpi_visa_read_data,
170 .read_complete = scpi_visa_read_complete,
171 .close = scpi_visa_close,
172 .free = scpi_visa_free,