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"
26 #define LOG_PREFIX "scpi_visa"
34 static int scpi_visa_dev_inst_new(void *priv, struct drv_context *drvc,
35 const char *resource, char **params, const char *serialcomm)
37 struct scpi_visa *vscpi = priv;
43 if (!params || !params[1]) {
44 sr_err("Invalid parameters.");
48 vscpi->resource = g_strdup(params[1]);
53 static int scpi_visa_open(void *priv)
55 struct scpi_visa *vscpi = priv;
57 if (viOpenDefaultRM(&vscpi->rmgr) != VI_SUCCESS) {
58 sr_err("Cannot open default resource manager.");
62 if (viOpen(vscpi->rmgr, vscpi->resource, VI_NO_LOCK, 0, &vscpi->vi) != VI_SUCCESS) {
63 sr_err("Cannot open resource.");
70 static int scpi_visa_source_add(void *priv, int events, int timeout,
71 sr_receive_data_callback cb, void *cb_data)
75 /* Hook up a dummy handler to receive data from the device. */
76 return sr_source_add(-1, events, timeout, cb, cb_data);
79 static int scpi_visa_source_remove(void *priv)
83 return sr_source_remove(-1);
86 static int scpi_visa_send(void *priv, const char *command)
88 struct scpi_visa *vscpi = priv;
89 gchar *terminated_command;
93 terminated_command = g_strconcat(command, "\n", NULL);
94 len = strlen(terminated_command);
95 if (viWrite(vscpi->vi, (ViBuf) (terminated_command + written), len,
96 &written) != VI_SUCCESS) {
97 sr_err("Error while sending SCPI command: '%s'.", command);
98 g_free(terminated_command);
102 g_free(terminated_command);
104 sr_spew("Successfully sent SCPI command: '%s'.", command);
109 static int scpi_visa_read_begin(void *priv)
116 static int scpi_visa_read_data(void *priv, char *buf, int maxlen)
118 struct scpi_visa *vscpi = priv;
121 if (viRead(vscpi->vi, (ViBuf) buf, maxlen, &count) != VI_SUCCESS) {
122 sr_err("Read failed.");
129 static int scpi_visa_read_complete(void *priv)
131 struct scpi_visa *vscpi = priv;
134 if (viReadSTB(vscpi->vi, &status) != VI_SUCCESS) {
135 sr_err("Failed to read status.");
139 return !(status & 16);
142 static int scpi_visa_close(void *priv)
144 struct scpi_visa *vscpi = priv;
147 viClose(vscpi->rmgr);
152 static void scpi_visa_free(void *priv)
154 struct scpi_visa *vscpi = priv;
156 g_free(vscpi->resource);
160 SR_PRIV const struct sr_scpi_dev_inst scpi_visa_dev = {
163 .priv_size = sizeof(struct scpi_visa),
164 .dev_inst_new = scpi_visa_dev_inst_new,
165 .open = scpi_visa_open,
166 .source_add = scpi_visa_source_add,
167 .source_remove = scpi_visa_source_remove,
168 .send = scpi_visa_send,
169 .read_begin = scpi_visa_read_begin,
170 .read_data = scpi_visa_read_data,
171 .read_complete = scpi_visa_read_complete,
172 .close = scpi_visa_close,
173 .free = scpi_visa_free,