]>
Commit | Line | Data |
---|---|---|
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 | ||
28 | struct scpi_visa { | |
29 | char *resource; | |
30 | ViSession rmgr; | |
31 | ViSession vi; | |
32 | }; | |
33 | ||
34 | static int scpi_visa_dev_inst_new(void *priv, struct drv_context *drvc, | |
35 | const char *resource, char **params, const char *serialcomm) | |
36 | { | |
37 | struct scpi_visa *vscpi = priv; | |
38 | ||
39 | (void)drvc; | |
40 | (void)resource; | |
41 | (void)serialcomm; | |
42 | ||
43 | if (!params || !params[1]) { | |
44 | sr_err("Invalid parameters."); | |
45 | return SR_ERR_BUG; | |
46 | } | |
47 | ||
48 | vscpi->resource = g_strdup(params[1]); | |
49 | ||
50 | return SR_OK; | |
51 | } | |
52 | ||
53 | static int scpi_visa_open(void *priv) | |
54 | { | |
55 | struct scpi_visa *vscpi = priv; | |
56 | ||
57 | if (viOpenDefaultRM(&vscpi->rmgr) != VI_SUCCESS) { | |
58 | sr_err("Cannot open default resource manager."); | |
59 | return SR_ERR; | |
60 | } | |
61 | ||
62 | if (viOpen(vscpi->rmgr, vscpi->resource, VI_NO_LOCK, 0, &vscpi->vi) != VI_SUCCESS) { | |
63 | sr_err("Cannot open resource."); | |
64 | return SR_ERR; | |
65 | } | |
66 | ||
67 | return SR_OK; | |
68 | } | |
69 | ||
70 | static int scpi_visa_source_add(struct sr_session *session, void *priv, | |
71 | int events, int timeout, sr_receive_data_callback cb, void *cb_data) | |
72 | { | |
73 | (void) priv; | |
74 | ||
75 | /* Hook up a dummy handler to receive data from the device. */ | |
76 | return sr_session_source_add(session, -1, events, timeout, cb, cb_data); | |
77 | } | |
78 | ||
79 | static int scpi_visa_source_remove(struct sr_session *session, void *priv) | |
80 | { | |
81 | (void) priv; | |
82 | ||
83 | return sr_session_source_remove(session, -1); | |
84 | } | |
85 | ||
86 | static int scpi_visa_send(void *priv, const char *command) | |
87 | { | |
88 | struct scpi_visa *vscpi = priv; | |
89 | gchar *terminated_command; | |
90 | ViUInt32 written = 0; | |
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 | ||
109 | static int scpi_visa_read_begin(void *priv) | |
110 | { | |
111 | (void) priv; | |
112 | ||
113 | return SR_OK; | |
114 | } | |
115 | ||
116 | static 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 | sr_err("Read failed."); | |
123 | return SR_ERR; | |
124 | } | |
125 | ||
126 | return count; | |
127 | } | |
128 | ||
129 | static int scpi_visa_read_complete(void *priv) | |
130 | { | |
131 | struct scpi_visa *vscpi = priv; | |
132 | ViUInt16 status; | |
133 | ||
134 | if (viReadSTB(vscpi->vi, &status) != VI_SUCCESS) { | |
135 | sr_err("Failed to read status."); | |
136 | return SR_ERR; | |
137 | } | |
138 | ||
139 | return !(status & 16); | |
140 | } | |
141 | ||
142 | static int scpi_visa_close(void *priv) | |
143 | { | |
144 | struct scpi_visa *vscpi = priv; | |
145 | ||
146 | viClose(vscpi->vi); | |
147 | viClose(vscpi->rmgr); | |
148 | ||
149 | return SR_OK; | |
150 | } | |
151 | ||
152 | static void scpi_visa_free(void *priv) | |
153 | { | |
154 | struct scpi_visa *vscpi = priv; | |
155 | ||
156 | g_free(vscpi->resource); | |
157 | g_free(vscpi); | |
158 | } | |
159 | ||
160 | SR_PRIV const struct sr_scpi_dev_inst scpi_visa_dev = { | |
161 | .name = "VISA", | |
162 | .prefix = "visa", | |
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, | |
174 | }; |