]> sigrok.org Git - libsigrok.git/blob - src/scpi/scpi_visa.c
Build: Set local include directories in Makefile.am
[libsigrok.git] / src / scpi / scpi_visa.c
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 <visa.h>
21 #include <string.h>
22 #include <libsigrok/libsigrok.h>
23 #include "libsigrok-internal.h"
24
25 #define LOG_PREFIX "scpi_visa"
26
27 struct scpi_visa {
28         char *resource;
29         ViSession rmgr;
30         ViSession vi;
31 };
32
33 static int scpi_visa_dev_inst_new(void *priv, struct drv_context *drvc,
34                 const char *resource, char **params, const char *serialcomm)
35 {
36         struct scpi_visa *vscpi = priv;
37
38         (void)drvc;
39         (void)resource;
40         (void)serialcomm;
41
42         if (!params || !params[1]) {
43                 sr_err("Invalid parameters.");
44                 return SR_ERR_BUG;
45         }
46
47         vscpi->resource = g_strdup(params[1]);
48
49         return SR_OK;
50 }
51
52 static 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                 sr_err("Cannot open resource.");
63                 return SR_ERR;
64         }
65
66         return SR_OK;
67 }
68
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)
71 {
72         (void) priv;
73
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);
76 }
77
78 static int scpi_visa_source_remove(struct sr_session *session, void *priv)
79 {
80         (void) priv;
81
82         return sr_session_source_remove(session, -1);
83 }
84
85 static int scpi_visa_send(void *priv, const char *command)
86 {
87         struct scpi_visa *vscpi = priv;
88         gchar *terminated_command;
89         ViUInt32 written = 0;
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
108 static int scpi_visa_read_begin(void *priv)
109 {
110         (void) priv;
111
112         return SR_OK;
113 }
114
115 static int scpi_visa_read_data(void *priv, char *buf, int maxlen)
116 {
117         struct scpi_visa *vscpi = priv;
118         ViUInt32 count;
119
120         if (viRead(vscpi->vi, (ViBuf) buf, maxlen, &count) != VI_SUCCESS) {
121                 sr_err("Read failed.");
122                 return SR_ERR;
123         }
124
125         return count;
126 }
127
128 static 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) {
134                 sr_err("Failed to read status.");
135                 return SR_ERR;
136         }
137
138         return !(status & 16);
139 }
140
141 static 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
151 static 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
159 SR_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 };