]> sigrok.org Git - libsigrok.git/blame - src/scpi/scpi_visa.c
Build: Set local include directories in Makefile.am
[libsigrok.git] / src / scpi / scpi_visa.c
CommitLineData
1fb2312f
ML
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 Martin Ling <martin-sigrok@earth.li>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
1fb2312f
ML
20#include <visa.h>
21#include <string.h>
c1aae900 22#include <libsigrok/libsigrok.h>
515ab088 23#include "libsigrok-internal.h"
1fb2312f
ML
24
25#define LOG_PREFIX "scpi_visa"
26
27struct scpi_visa {
28 char *resource;
29 ViSession rmgr;
30 ViSession vi;
31};
32
d8b6b28b
DE
33static int scpi_visa_dev_inst_new(void *priv, struct drv_context *drvc,
34 const char *resource, char **params, const char *serialcomm)
1fb2312f
ML
35{
36 struct scpi_visa *vscpi = priv;
37
d8b6b28b 38 (void)drvc;
1fb2312f
ML
39 (void)resource;
40 (void)serialcomm;
41
42 if (!params || !params[1]) {
43 sr_err("Invalid parameters.");
7ad4e2b8 44 return SR_ERR_BUG;
1fb2312f
ML
45 }
46
47 vscpi->resource = g_strdup(params[1]);
48
49 return SR_OK;
50}
51
52static int scpi_visa_open(void *priv)
53{
54 struct scpi_visa *vscpi = priv;
55
56 if (viOpenDefaultRM(&vscpi->rmgr) != VI_SUCCESS) {
57 sr_err("Cannot open default resource manager.");
58 return SR_ERR;
59 }
60
7ad4e2b8 61 if (viOpen(vscpi->rmgr, vscpi->resource, VI_NO_LOCK, 0, &vscpi->vi) != VI_SUCCESS) {
1fb2312f
ML
62 sr_err("Cannot open resource.");
63 return SR_ERR;
64 }
65
66 return SR_OK;
67}
68
102f1239
BV
69static int scpi_visa_source_add(struct sr_session *session, void *priv,
70 int events, int timeout, sr_receive_data_callback cb, void *cb_data)
1fb2312f
ML
71{
72 (void) priv;
73
74 /* Hook up a dummy handler to receive data from the device. */
102f1239 75 return sr_session_source_add(session, -1, events, timeout, cb, cb_data);
1fb2312f
ML
76}
77
102f1239 78static int scpi_visa_source_remove(struct sr_session *session, void *priv)
1fb2312f
ML
79{
80 (void) priv;
81
102f1239 82 return sr_session_source_remove(session, -1);
1fb2312f
ML
83}
84
85static int scpi_visa_send(void *priv, const char *command)
86{
87 struct scpi_visa *vscpi = priv;
88 gchar *terminated_command;
dc05dd60 89 ViUInt32 written = 0;
1fb2312f
ML
90 int len;
91
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);
98 return SR_ERR;
99 }
100
101 g_free(terminated_command);
102
103 sr_spew("Successfully sent SCPI command: '%s'.", command);
104
105 return SR_OK;
106}
107
108static int scpi_visa_read_begin(void *priv)
109{
110 (void) priv;
111
112 return SR_OK;
113}
114
115static int scpi_visa_read_data(void *priv, char *buf, int maxlen)
116{
117 struct scpi_visa *vscpi = priv;
118 ViUInt32 count;
119
7ad4e2b8
UH
120 if (viRead(vscpi->vi, (ViBuf) buf, maxlen, &count) != VI_SUCCESS) {
121 sr_err("Read failed.");
1fb2312f
ML
122 return SR_ERR;
123 }
124
125 return count;
126}
127
128static int scpi_visa_read_complete(void *priv)
129{
130 struct scpi_visa *vscpi = priv;
131 ViUInt16 status;
132
133 if (viReadSTB(vscpi->vi, &status) != VI_SUCCESS) {
7ad4e2b8 134 sr_err("Failed to read status.");
1fb2312f
ML
135 return SR_ERR;
136 }
137
138 return !(status & 16);
139}
140
141static int scpi_visa_close(void *priv)
142{
143 struct scpi_visa *vscpi = priv;
144
145 viClose(vscpi->vi);
146 viClose(vscpi->rmgr);
147
148 return SR_OK;
149}
150
151static void scpi_visa_free(void *priv)
152{
153 struct scpi_visa *vscpi = priv;
154
155 g_free(vscpi->resource);
156 g_free(vscpi);
157}
158
159SR_PRIV const struct sr_scpi_dev_inst scpi_visa_dev = {
160 .name = "VISA",
161 .prefix = "visa",
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,
173};