]>
Commit | Line | Data |
---|---|---|
e93cdf42 BV |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2012 Bert Vermeulen <bert@biot.com> | |
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 <glib.h> | |
21 | #include "libsigrok.h" | |
22 | #include "libsigrok-internal.h" | |
23 | #include "config.h" | |
24 | #include "agilent-dmm.h" | |
25 | #include <stdlib.h> | |
26 | #include <string.h> | |
27 | #include <errno.h> | |
28 | #include <math.h> | |
29 | ||
30 | ||
31 | static void dispatch(const struct sr_dev_inst *sdi) | |
32 | { | |
33 | struct dev_context *devc; | |
34 | const struct agdmm_job *jobs; | |
35 | int64_t now; | |
36 | int i; | |
37 | ||
38 | devc = sdi->priv; | |
39 | jobs = devc->profile->jobs; | |
40 | now = g_get_monotonic_time() / 1000; | |
41 | for (i = 0; (&jobs[i])->interval; i++) { | |
42 | if (now - devc->jobqueue[i] > (&jobs[i])->interval) { | |
43 | sr_spew("agilent-dmm: running job %d", i); | |
44 | (&jobs[i])->send(sdi); | |
45 | devc->jobqueue[i] = now; | |
46 | } | |
47 | } | |
48 | ||
49 | } | |
50 | ||
51 | static void receive_line(const struct sr_dev_inst *sdi) | |
52 | { | |
53 | struct dev_context *devc; | |
54 | const struct agdmm_recv *recvs, *recv; | |
55 | GRegex *reg; | |
56 | GMatchInfo *match; | |
57 | int i; | |
58 | ||
59 | devc = sdi->priv; | |
60 | ||
61 | /* Strip CRLF */ | |
62 | while (devc->buflen) { | |
63 | if (*(devc->buf + devc->buflen - 1) == '\r' | |
64 | || *(devc->buf + devc->buflen - 1) == '\n') | |
65 | *(devc->buf + --devc->buflen) = '\0'; | |
66 | else | |
67 | break; | |
68 | } | |
69 | sr_spew("agilent-dmm: received '%s'", devc->buf); | |
70 | ||
71 | recv = NULL; | |
72 | recvs = devc->profile->recvs; | |
73 | for (i = 0; (&recvs[i])->recv_regex; i++) { | |
74 | reg = g_regex_new((&recvs[i])->recv_regex, 0, 0, NULL); | |
75 | if (g_regex_match(reg, (char *)devc->buf, 0, &match)) { | |
76 | recv = &recvs[i]; | |
77 | break; | |
78 | } | |
79 | g_match_info_unref(match); | |
80 | g_regex_unref(reg); | |
81 | } | |
82 | if (recv) { | |
83 | recv->recv(sdi, match); | |
84 | g_match_info_unref(match); | |
85 | g_regex_unref(reg); | |
86 | } | |
87 | ||
88 | /* Done with this. */ | |
89 | devc->buflen = 0; | |
90 | ||
91 | } | |
92 | ||
93 | SR_PRIV int agdmm_receive_data(int fd, int revents, void *cb_data) | |
94 | { | |
95 | const struct sr_dev_inst *sdi; | |
96 | struct dev_context *devc; | |
97 | int len; | |
98 | ||
99 | if (!(sdi = cb_data)) | |
100 | return TRUE; | |
101 | ||
102 | if (!(devc = sdi->priv)) | |
103 | return TRUE; | |
104 | ||
105 | if (revents == G_IO_IN) { | |
106 | /* Serial data arrived. */ | |
107 | len = AGDMM_BUFSIZE - devc->buflen - 1; | |
108 | if (len > 0) { | |
109 | len = serial_read(fd, devc->buf + devc->buflen, len); | |
110 | if (len > 0) { | |
111 | devc->buflen += len; | |
112 | *(devc->buf + devc->buflen) = '\0'; | |
113 | if (devc->buflen > 0 && *(devc->buf + devc->buflen - 1) == '\n') | |
114 | /* End of line */ | |
115 | receive_line(sdi); | |
116 | } | |
117 | } | |
118 | } | |
119 | ||
120 | dispatch(sdi); | |
121 | ||
122 | if (devc->num_samples >= devc->limit_samples) | |
123 | sdi->driver->dev_acquisition_stop(sdi, cb_data); | |
124 | ||
125 | return TRUE; | |
126 | } | |
127 | ||
128 | static int agdmm_send(const struct sr_dev_inst *sdi, const char *cmd) | |
129 | { | |
130 | struct dev_context *devc; | |
131 | char buf[32]; | |
132 | ||
133 | devc = sdi->priv; | |
134 | sr_spew("agilent-dmm: sending '%s'", cmd); | |
135 | strncpy(buf, cmd, 28); | |
136 | if (!strncmp(buf, "*IDN?", 5)) | |
137 | strncat(buf, "\r\n", 32); | |
138 | else | |
139 | strncat(buf, "\n\r\n", 32); | |
140 | if (serial_write(devc->serial->fd, buf, strlen(buf)) == -1) { | |
141 | sr_err("agilent-dmm: failed to send: %s", strerror(errno)); | |
142 | return SR_ERR; | |
143 | } | |
144 | ||
145 | return SR_OK; | |
146 | } | |
147 | ||
148 | static int agdmm_ident_send(const struct sr_dev_inst *sdi) | |
149 | { | |
150 | ||
151 | return agdmm_send(sdi, "*IDN?"); | |
152 | } | |
153 | ||
154 | static int agdmm_ident_recv(const struct sr_dev_inst *sdi, GMatchInfo *match) | |
155 | { | |
156 | ||
157 | (void)sdi; | |
158 | ||
159 | sr_spew("got ident '%s'", g_match_info_get_string(match)); | |
160 | ||
161 | return SR_OK; | |
162 | } | |
163 | ||
164 | static int agdmm_stat_send(const struct sr_dev_inst *sdi) | |
165 | { | |
166 | ||
167 | return agdmm_send(sdi, "STAT?"); | |
168 | } | |
169 | ||
170 | static int agdmm_stat_recv(const struct sr_dev_inst *sdi, GMatchInfo *match) | |
171 | { | |
172 | ||
173 | sr_spew("got stat '%s'", g_match_info_get_string(match)); | |
174 | ||
175 | return SR_OK; | |
176 | } | |
177 | ||
178 | SR_PRIV int agdmm_fetc_send(const struct sr_dev_inst *sdi) | |
179 | { | |
180 | ||
181 | return agdmm_send(sdi, "FETC?"); | |
182 | } | |
183 | ||
184 | SR_PRIV int agdmm_fetc_recv(const struct sr_dev_inst *sdi, GMatchInfo *match) | |
185 | { | |
186 | struct dev_context *devc; | |
187 | struct sr_datafeed_packet packet; | |
188 | struct sr_datafeed_analog analog; | |
189 | float fvalue; | |
190 | char *mstr, *eptr; | |
191 | ||
192 | sr_spew("agilent-dmm: FETC reply '%s'", g_match_info_get_string(match)); | |
193 | devc = sdi->priv; | |
194 | ||
195 | if (devc->cur_mq == -1) | |
196 | /* Haven't seen configuration yet, so can't know what | |
197 | * the fetched float means. Not really an error, we'll | |
198 | * get metadata soon enough. */ | |
199 | return SR_OK; | |
200 | ||
201 | if (!strcmp(g_match_info_get_string(match), "+9.90000000E+37")) { | |
202 | /* An invalid measurement shows up on the display as "O.L, but | |
203 | * comes through like this. Since comparing 38-digit floats | |
204 | * is rather problematic, we'll cut through this here. */ | |
205 | fvalue = NAN; | |
206 | } else { | |
207 | mstr = g_match_info_fetch(match, 1); | |
208 | fvalue = strtof(mstr, &eptr); | |
209 | g_free(mstr); | |
210 | if (fvalue == 0.0 && eptr == mstr) { | |
211 | sr_err("agilent-dmm: invalid float"); | |
212 | return SR_ERR; | |
213 | } | |
214 | if (devc->cur_divider > 0) | |
215 | fvalue /= devc->cur_divider; | |
216 | } | |
217 | ||
218 | memset(&analog, 0, sizeof(struct sr_datafeed_analog)); | |
219 | analog.mq = devc->cur_mq; | |
220 | analog.unit = devc->cur_mq_unit; | |
221 | analog.mqflags = devc->cur_mq_flags; | |
222 | analog.num_samples = 1; | |
223 | analog.data = &fvalue; | |
224 | packet.type = SR_DF_ANALOG; | |
225 | packet.payload = &analog; | |
226 | sr_session_send(devc->cb_data, &packet); | |
227 | ||
228 | devc->num_samples++; | |
229 | ||
230 | return SR_OK; | |
231 | } | |
232 | ||
233 | SR_PRIV int agdmm_conf_send(const struct sr_dev_inst *sdi) | |
234 | { | |
235 | ||
236 | return agdmm_send(sdi, "CONF?"); | |
237 | } | |
238 | ||
239 | SR_PRIV int agdmm_conf_recv(const struct sr_dev_inst *sdi, GMatchInfo *match) | |
240 | { | |
241 | struct dev_context *devc; | |
242 | char *mstr; | |
243 | ||
244 | sr_spew("got conf '%s'", g_match_info_get_string(match)); | |
245 | devc = sdi->priv; | |
246 | mstr = g_match_info_fetch(match, 1); | |
247 | if (!strcmp(mstr, "V")) { | |
248 | devc->cur_mq = SR_MQ_VOLTAGE; | |
249 | devc->cur_mq_unit = SR_UNIT_VOLT; | |
250 | devc->cur_mq_flags = 0; | |
251 | devc->cur_divider = 0; | |
252 | } else if(!strcmp(mstr, "MV")) { | |
253 | devc->cur_mq = SR_MQ_VOLTAGE; | |
254 | devc->cur_mq_unit = SR_UNIT_VOLT; | |
255 | devc->cur_mq_flags = 0; | |
256 | devc->cur_divider = 1000; | |
257 | } else if(!strcmp(mstr, "A")) { | |
258 | devc->cur_mq = SR_MQ_CURRENT; | |
259 | devc->cur_mq_unit = SR_UNIT_AMPERE; | |
260 | devc->cur_mq_flags = 0; | |
261 | devc->cur_divider = 0; | |
262 | } else if(!strcmp(mstr, "UA")) { | |
263 | devc->cur_mq = SR_MQ_CURRENT; | |
264 | devc->cur_mq_unit = SR_UNIT_AMPERE; | |
265 | devc->cur_mq_flags = 0; | |
266 | devc->cur_divider = 1000000; | |
267 | } else if(!strcmp(mstr, "FREQ")) { | |
268 | devc->cur_mq = SR_MQ_FREQUENCY; | |
269 | devc->cur_mq_unit = SR_UNIT_HERTZ; | |
270 | devc->cur_mq_flags = 0; | |
271 | devc->cur_divider = 0; | |
272 | } else if(!strcmp(mstr, "RES")) { | |
273 | devc->cur_mq = SR_MQ_RESISTANCE; | |
274 | devc->cur_mq_unit = SR_UNIT_OHM; | |
275 | devc->cur_mq_flags = 0; | |
276 | devc->cur_divider = 0; | |
277 | } else if(!strcmp(mstr, "CAP")) { | |
278 | devc->cur_mq = SR_MQ_CAPACITANCE; | |
279 | devc->cur_mq_unit = SR_UNIT_FARAD; | |
280 | devc->cur_mq_flags = 0; | |
281 | devc->cur_divider = 0; | |
282 | } else if(!strcmp(mstr, "DIOD")) { | |
283 | devc->cur_mq = SR_MQ_VOLTAGE; | |
284 | devc->cur_mq_unit = SR_UNIT_VOLT; | |
285 | devc->cur_mq_flags = SR_MQFLAG_DIODE; | |
286 | devc->cur_divider = 0; | |
287 | } else | |
288 | sr_dbg("agilent-dmm: unknown first argument"); | |
289 | g_free(mstr); | |
290 | ||
291 | if (g_match_info_get_match_count(match) == 3) { | |
292 | mstr = g_match_info_fetch(match, 1); | |
293 | /* Third value, if present, is always AC or DC. */ | |
294 | if (!strcmp(mstr, "AC")) | |
295 | devc->cur_acdc = 1; | |
296 | else if (!strcmp(mstr, "DC")) | |
297 | devc->cur_acdc = 2; | |
298 | else | |
299 | sr_dbg("agilent-dmm: unknown third argument"); | |
300 | g_free(mstr); | |
301 | } | |
302 | ||
303 | return SR_OK; | |
304 | } | |
305 | ||
306 | SR_PRIV int agdmm_switch_recv(const struct sr_dev_inst *sdi, GMatchInfo *match) | |
307 | { | |
308 | ||
309 | (void)sdi; | |
310 | ||
311 | sr_spew("got switch '%s'", g_match_info_get_string(match)); | |
312 | ||
313 | return SR_OK; | |
314 | } | |
315 | ||
316 | ||
317 | SR_PRIV const struct agdmm_job u123x_jobs[] = { | |
318 | { 1000, agdmm_ident_send }, | |
319 | { 143, agdmm_stat_send }, | |
320 | { 1000, agdmm_conf_send }, | |
321 | { 143, agdmm_fetc_send }, | |
322 | { 0, NULL } | |
323 | }; | |
324 | ||
325 | SR_PRIV const struct agdmm_recv u123x_recvs[] = { | |
326 | { "^\"(\\d\\d.{18}\\d)\"$", agdmm_stat_recv }, | |
327 | { "^\\*([0-9])$", agdmm_switch_recv }, | |
328 | { "^([-+][0-9]\\.[0-9]{8}E[-+][0-9]{2})$", agdmm_fetc_recv }, | |
329 | { "^\"(V|MV|A|UA|FREQ),(\\d),(AC|DC)\"$", agdmm_conf_recv }, | |
330 | { "^\"(RES|CAP),(\\d)\"$", agdmm_conf_recv }, | |
331 | { "^\"(DIOD)\"$", agdmm_conf_recv }, | |
332 | { NULL, NULL } | |
333 | }; | |
334 | ||
335 |