]> sigrok.org Git - libsigrok.git/blame - hardware/common/scpi_visa.c
Add librevisa SCPI backend.
[libsigrok.git] / hardware / common / 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
20#include "libsigrok.h"
21#include "libsigrok-internal.h"
22
23#include <visa.h>
24#include <string.h>
25
26#define LOG_PREFIX "scpi_visa"
27
28struct scpi_visa {
29 char *resource;
30 ViSession rmgr;
31 ViSession vi;
32};
33
34static int scpi_visa_dev_inst_new(void *priv, const char *resource,
35 char **params, const char *serialcomm)
36{
37 struct scpi_visa *vscpi = priv;
38
39 (void)resource;
40 (void)serialcomm;
41
42 if (!params || !params[1]) {
43 sr_err("Invalid parameters.");
44 return SR_ERR;
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
61 if (viOpen(vscpi->rmgr, vscpi->resource, VI_NO_LOCK, 0, &vscpi->vi) != VI_SUCCESS)
62 {
63 sr_err("Cannot open resource.");
64 return SR_ERR;
65 }
66
67 return SR_OK;
68}
69
70static int scpi_visa_source_add(void *priv, int events, int timeout,
71 sr_receive_data_callback_t cb, void *cb_data)
72{
73 (void) priv;
74
75 /* Hook up a dummy handler to receive data from the device. */
76 return sr_source_add(-1, events, timeout, cb, cb_data);
77}
78
79static int scpi_visa_source_remove(void *priv)
80{
81 (void) priv;
82
83 return sr_source_remove(-1);;
84}
85
86static int scpi_visa_send(void *priv, const char *command)
87{
88 struct scpi_visa *vscpi = priv;
89 gchar *terminated_command;
90 ViUInt32 written;
91 int len;
92
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);
99 return SR_ERR;
100 }
101
102 g_free(terminated_command);
103
104 sr_spew("Successfully sent SCPI command: '%s'.", command);
105
106 return SR_OK;
107}
108
109static int scpi_visa_read_begin(void *priv)
110{
111 (void) priv;
112
113 return SR_OK;
114}
115
116static int scpi_visa_read_data(void *priv, char *buf, int maxlen)
117{
118 struct scpi_visa *vscpi = priv;
119 ViUInt32 count;
120
121 if (viRead(vscpi->vi, (ViBuf) buf, maxlen, &count) != VI_SUCCESS)
122 {
123 sr_err("Read failed");
124 return SR_ERR;
125 }
126
127 return count;
128}
129
130static int scpi_visa_read_complete(void *priv)
131{
132 struct scpi_visa *vscpi = priv;
133 ViUInt16 status;
134
135 if (viReadSTB(vscpi->vi, &status) != VI_SUCCESS) {
136 sr_err("Failed to read status");
137 return SR_ERR;
138 }
139
140 return !(status & 16);
141}
142
143static int scpi_visa_close(void *priv)
144{
145 struct scpi_visa *vscpi = priv;
146
147 viClose(vscpi->vi);
148 viClose(vscpi->rmgr);
149
150 return SR_OK;
151}
152
153static void scpi_visa_free(void *priv)
154{
155 struct scpi_visa *vscpi = priv;
156
157 g_free(vscpi->resource);
158 g_free(vscpi);
159}
160
161SR_PRIV const struct sr_scpi_dev_inst scpi_visa_dev = {
162 .name = "VISA",
163 .prefix = "visa",
164 .priv_size = sizeof(struct scpi_visa),
165 .dev_inst_new = scpi_visa_dev_inst_new,
166 .open = scpi_visa_open,
167 .source_add = scpi_visa_source_add,
168 .source_remove = scpi_visa_source_remove,
169 .send = scpi_visa_send,
170 .read_begin = scpi_visa_read_begin,
171 .read_data = scpi_visa_read_data,
172 .read_complete = scpi_visa_read_complete,
173 .close = scpi_visa_close,
174 .free = scpi_visa_free,
175};