]> sigrok.org Git - libsigrok.git/blame - src/scpi/scpi_visa.c
scpi: Pass SCPI device instance to open and close callbacks.
[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
6ec6c43b 20#include <config.h>
1fb2312f
ML
21#include <visa.h>
22#include <string.h>
c1aae900 23#include <libsigrok/libsigrok.h>
515ab088 24#include "libsigrok-internal.h"
5a1afc09 25#include "scpi.h"
1fb2312f
ML
26
27#define LOG_PREFIX "scpi_visa"
28
29struct scpi_visa {
30 char *resource;
31 ViSession rmgr;
32 ViSession vi;
33};
34
d8b6b28b
DE
35static int scpi_visa_dev_inst_new(void *priv, struct drv_context *drvc,
36 const char *resource, char **params, const char *serialcomm)
1fb2312f
ML
37{
38 struct scpi_visa *vscpi = priv;
39
d8b6b28b 40 (void)drvc;
1fb2312f
ML
41 (void)resource;
42 (void)serialcomm;
43
44 if (!params || !params[1]) {
45 sr_err("Invalid parameters.");
7ad4e2b8 46 return SR_ERR_BUG;
1fb2312f
ML
47 }
48
49 vscpi->resource = g_strdup(params[1]);
50
51 return SR_OK;
52}
53
04229f7b 54static int scpi_visa_open(struct sr_scpi_dev_inst *scpi)
1fb2312f 55{
04229f7b 56 struct scpi_visa *vscpi = scpi->priv;
1fb2312f
ML
57
58 if (viOpenDefaultRM(&vscpi->rmgr) != VI_SUCCESS) {
59 sr_err("Cannot open default resource manager.");
60 return SR_ERR;
61 }
62
7ad4e2b8 63 if (viOpen(vscpi->rmgr, vscpi->resource, VI_NO_LOCK, 0, &vscpi->vi) != VI_SUCCESS) {
1fb2312f
ML
64 sr_err("Cannot open resource.");
65 return SR_ERR;
66 }
67
68 return SR_OK;
69}
70
102f1239
BV
71static int scpi_visa_source_add(struct sr_session *session, void *priv,
72 int events, int timeout, sr_receive_data_callback cb, void *cb_data)
1fb2312f
ML
73{
74 (void) priv;
75
76 /* Hook up a dummy handler to receive data from the device. */
102f1239 77 return sr_session_source_add(session, -1, events, timeout, cb, cb_data);
1fb2312f
ML
78}
79
102f1239 80static int scpi_visa_source_remove(struct sr_session *session, void *priv)
1fb2312f
ML
81{
82 (void) priv;
83
102f1239 84 return sr_session_source_remove(session, -1);
1fb2312f
ML
85}
86
87static int scpi_visa_send(void *priv, const char *command)
88{
89 struct scpi_visa *vscpi = priv;
90 gchar *terminated_command;
dc05dd60 91 ViUInt32 written = 0;
1fb2312f
ML
92 int len;
93
94 terminated_command = g_strconcat(command, "\n", NULL);
95 len = strlen(terminated_command);
96 if (viWrite(vscpi->vi, (ViBuf) (terminated_command + written), len,
97 &written) != VI_SUCCESS) {
98 sr_err("Error while sending SCPI command: '%s'.", command);
99 g_free(terminated_command);
100 return SR_ERR;
101 }
102
103 g_free(terminated_command);
104
105 sr_spew("Successfully sent SCPI command: '%s'.", command);
106
107 return SR_OK;
108}
109
110static int scpi_visa_read_begin(void *priv)
111{
112 (void) priv;
113
114 return SR_OK;
115}
116
117static int scpi_visa_read_data(void *priv, char *buf, int maxlen)
118{
119 struct scpi_visa *vscpi = priv;
120 ViUInt32 count;
121
7ad4e2b8
UH
122 if (viRead(vscpi->vi, (ViBuf) buf, maxlen, &count) != VI_SUCCESS) {
123 sr_err("Read failed.");
1fb2312f
ML
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) {
7ad4e2b8 136 sr_err("Failed to read status.");
1fb2312f
ML
137 return SR_ERR;
138 }
139
140 return !(status & 16);
141}
142
04229f7b 143static int scpi_visa_close(struct sr_scpi_dev_inst *scpi)
1fb2312f 144{
04229f7b 145 struct scpi_visa *vscpi = scpi->priv;
1fb2312f
ML
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};