]>
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" | |
e93cdf42 BV |
23 | #include "agilent-dmm.h" |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
26 | #include <errno.h> | |
27 | #include <math.h> | |
28 | ||
e93cdf42 BV |
29 | static void dispatch(const struct sr_dev_inst *sdi) |
30 | { | |
31 | struct dev_context *devc; | |
32 | const struct agdmm_job *jobs; | |
33 | int64_t now; | |
34 | int i; | |
35 | ||
36 | devc = sdi->priv; | |
37 | jobs = devc->profile->jobs; | |
38 | now = g_get_monotonic_time() / 1000; | |
39 | for (i = 0; (&jobs[i])->interval; i++) { | |
40 | if (now - devc->jobqueue[i] > (&jobs[i])->interval) { | |
38d326e8 | 41 | sr_spew("Running job %d.", i); |
e93cdf42 BV |
42 | (&jobs[i])->send(sdi); |
43 | devc->jobqueue[i] = now; | |
44 | } | |
45 | } | |
e93cdf42 BV |
46 | } |
47 | ||
48 | static void receive_line(const struct sr_dev_inst *sdi) | |
49 | { | |
50 | struct dev_context *devc; | |
51 | const struct agdmm_recv *recvs, *recv; | |
52 | GRegex *reg; | |
53 | GMatchInfo *match; | |
54 | int i; | |
55 | ||
56 | devc = sdi->priv; | |
57 | ||
58 | /* Strip CRLF */ | |
59 | while (devc->buflen) { | |
60 | if (*(devc->buf + devc->buflen - 1) == '\r' | |
61 | || *(devc->buf + devc->buflen - 1) == '\n') | |
62 | *(devc->buf + --devc->buflen) = '\0'; | |
63 | else | |
64 | break; | |
65 | } | |
38d326e8 | 66 | sr_spew("Received '%s'.", devc->buf); |
e93cdf42 BV |
67 | |
68 | recv = NULL; | |
69 | recvs = devc->profile->recvs; | |
70 | for (i = 0; (&recvs[i])->recv_regex; i++) { | |
71 | reg = g_regex_new((&recvs[i])->recv_regex, 0, 0, NULL); | |
72 | if (g_regex_match(reg, (char *)devc->buf, 0, &match)) { | |
73 | recv = &recvs[i]; | |
74 | break; | |
75 | } | |
76 | g_match_info_unref(match); | |
77 | g_regex_unref(reg); | |
78 | } | |
79 | if (recv) { | |
80 | recv->recv(sdi, match); | |
81 | g_match_info_unref(match); | |
82 | g_regex_unref(reg); | |
f2e86bbf | 83 | } else |
38d326e8 | 84 | sr_dbg("Unknown line '%s'.", devc->buf); |
e93cdf42 BV |
85 | |
86 | /* Done with this. */ | |
87 | devc->buflen = 0; | |
e93cdf42 BV |
88 | } |
89 | ||
90 | SR_PRIV int agdmm_receive_data(int fd, int revents, void *cb_data) | |
91 | { | |
92 | const struct sr_dev_inst *sdi; | |
93 | struct dev_context *devc; | |
94 | int len; | |
95 | ||
96 | if (!(sdi = cb_data)) | |
97 | return TRUE; | |
98 | ||
99 | if (!(devc = sdi->priv)) | |
100 | return TRUE; | |
101 | ||
102 | if (revents == G_IO_IN) { | |
103 | /* Serial data arrived. */ | |
f2e86bbf BV |
104 | while(AGDMM_BUFSIZE - devc->buflen - 1 > 0) { |
105 | len = serial_read(fd, devc->buf + devc->buflen, 1); | |
106 | if (len < 1) | |
107 | break; | |
108 | devc->buflen += len; | |
109 | *(devc->buf + devc->buflen) = '\0'; | |
110 | if (*(devc->buf + devc->buflen - 1) == '\n') { | |
111 | /* End of line */ | |
112 | receive_line(sdi); | |
113 | break; | |
e93cdf42 BV |
114 | } |
115 | } | |
116 | } | |
117 | ||
118 | dispatch(sdi); | |
119 | ||
120 | if (devc->num_samples >= devc->limit_samples) | |
121 | sdi->driver->dev_acquisition_stop(sdi, cb_data); | |
122 | ||
123 | return TRUE; | |
124 | } | |
125 | ||
126 | static int agdmm_send(const struct sr_dev_inst *sdi, const char *cmd) | |
127 | { | |
128 | struct dev_context *devc; | |
129 | char buf[32]; | |
130 | ||
131 | devc = sdi->priv; | |
38d326e8 | 132 | sr_spew("Sending '%s'.", cmd); |
e93cdf42 BV |
133 | strncpy(buf, cmd, 28); |
134 | if (!strncmp(buf, "*IDN?", 5)) | |
135 | strncat(buf, "\r\n", 32); | |
136 | else | |
137 | strncat(buf, "\n\r\n", 32); | |
138 | if (serial_write(devc->serial->fd, buf, strlen(buf)) == -1) { | |
38d326e8 | 139 | sr_err("Failed to send: %s.", strerror(errno)); |
e93cdf42 BV |
140 | return SR_ERR; |
141 | } | |
142 | ||
143 | return SR_OK; | |
144 | } | |
145 | ||
a4394fb3 | 146 | static int send_stat(const struct sr_dev_inst *sdi) |
e93cdf42 | 147 | { |
e93cdf42 BV |
148 | return agdmm_send(sdi, "STAT?"); |
149 | } | |
150 | ||
8c0152f2 | 151 | static int recv_stat_u123x(const struct sr_dev_inst *sdi, GMatchInfo *match) |
e93cdf42 | 152 | { |
e6b021f3 BV |
153 | struct dev_context *devc; |
154 | char *s; | |
155 | ||
156 | devc = sdi->priv; | |
157 | s = g_match_info_fetch(match, 1); | |
38d326e8 | 158 | sr_spew("STAT response '%s'.", s); |
e6b021f3 BV |
159 | |
160 | /* Max, Min or Avg mode -- no way to tell which, so we'll | |
161 | * set both flags to denote it's not a normal measurement. */ | |
162 | if (s[0] == '1') | |
163 | devc->cur_mqflags |= SR_MQFLAG_MAX | SR_MQFLAG_MIN; | |
164 | else | |
165 | devc->cur_mqflags &= ~(SR_MQFLAG_MAX | SR_MQFLAG_MIN); | |
e93cdf42 | 166 | |
e6b021f3 BV |
167 | if (s[1] == '1') |
168 | devc->cur_mqflags |= SR_MQFLAG_RELATIVE; | |
169 | else | |
170 | devc->cur_mqflags &= ~SR_MQFLAG_RELATIVE; | |
171 | ||
172 | /* Triggered or auto hold modes. */ | |
173 | if (s[2] == '1' || s[3] == '1') | |
174 | devc->cur_mqflags |= SR_MQFLAG_HOLD; | |
175 | else | |
176 | devc->cur_mqflags &= ~SR_MQFLAG_HOLD; | |
177 | ||
178 | /* Temp/aux mode. */ | |
179 | if (s[7] == '1') | |
180 | devc->mode_tempaux = TRUE; | |
181 | else | |
182 | devc->mode_tempaux = FALSE; | |
183 | ||
38d326e8 | 184 | /* Continuity mode. */ |
e6b021f3 BV |
185 | if (s[16] == '1') |
186 | devc->mode_continuity = TRUE; | |
187 | else | |
188 | devc->mode_continuity = FALSE; | |
189 | ||
190 | g_free(s); | |
e93cdf42 BV |
191 | |
192 | return SR_OK; | |
193 | } | |
194 | ||
8c0152f2 BV |
195 | static int recv_stat_u125x(const struct sr_dev_inst *sdi, GMatchInfo *match) |
196 | { | |
197 | struct dev_context *devc; | |
198 | char *s; | |
199 | ||
200 | devc = sdi->priv; | |
201 | s = g_match_info_fetch(match, 1); | |
38d326e8 | 202 | sr_spew("STAT response '%s'.", s); |
8c0152f2 BV |
203 | |
204 | /* Peak hold mode. */ | |
205 | if (s[4] == '1') | |
206 | devc->cur_mqflags |= SR_MQFLAG_MAX; | |
207 | else | |
208 | devc->cur_mqflags &= ~SR_MQFLAG_MAX; | |
209 | ||
210 | /* Triggered hold mode. */ | |
211 | if (s[7] == '1') | |
212 | devc->cur_mqflags |= SR_MQFLAG_HOLD; | |
213 | else | |
214 | devc->cur_mqflags &= ~SR_MQFLAG_HOLD; | |
215 | ||
216 | g_free(s); | |
217 | ||
218 | return SR_OK; | |
219 | } | |
220 | ||
a4394fb3 | 221 | static int send_fetc(const struct sr_dev_inst *sdi) |
e93cdf42 | 222 | { |
e93cdf42 BV |
223 | return agdmm_send(sdi, "FETC?"); |
224 | } | |
225 | ||
a4394fb3 | 226 | static int recv_fetc(const struct sr_dev_inst *sdi, GMatchInfo *match) |
e93cdf42 BV |
227 | { |
228 | struct dev_context *devc; | |
229 | struct sr_datafeed_packet packet; | |
230 | struct sr_datafeed_analog analog; | |
231 | float fvalue; | |
232 | char *mstr, *eptr; | |
233 | ||
38d326e8 | 234 | sr_spew("FETC reply '%s'.", g_match_info_get_string(match)); |
e93cdf42 BV |
235 | devc = sdi->priv; |
236 | ||
237 | if (devc->cur_mq == -1) | |
238 | /* Haven't seen configuration yet, so can't know what | |
239 | * the fetched float means. Not really an error, we'll | |
240 | * get metadata soon enough. */ | |
241 | return SR_OK; | |
242 | ||
243 | if (!strcmp(g_match_info_get_string(match), "+9.90000000E+37")) { | |
244 | /* An invalid measurement shows up on the display as "O.L, but | |
245 | * comes through like this. Since comparing 38-digit floats | |
246 | * is rather problematic, we'll cut through this here. */ | |
247 | fvalue = NAN; | |
248 | } else { | |
249 | mstr = g_match_info_fetch(match, 1); | |
250 | fvalue = strtof(mstr, &eptr); | |
251 | g_free(mstr); | |
252 | if (fvalue == 0.0 && eptr == mstr) { | |
38d326e8 | 253 | sr_err("Invalid float."); |
e93cdf42 BV |
254 | return SR_ERR; |
255 | } | |
256 | if (devc->cur_divider > 0) | |
257 | fvalue /= devc->cur_divider; | |
258 | } | |
259 | ||
260 | memset(&analog, 0, sizeof(struct sr_datafeed_analog)); | |
261 | analog.mq = devc->cur_mq; | |
e6b021f3 BV |
262 | analog.unit = devc->cur_unit; |
263 | analog.mqflags = devc->cur_mqflags; | |
e93cdf42 BV |
264 | analog.num_samples = 1; |
265 | analog.data = &fvalue; | |
266 | packet.type = SR_DF_ANALOG; | |
267 | packet.payload = &analog; | |
268 | sr_session_send(devc->cb_data, &packet); | |
269 | ||
270 | devc->num_samples++; | |
271 | ||
272 | return SR_OK; | |
273 | } | |
274 | ||
a4394fb3 | 275 | static int send_conf(const struct sr_dev_inst *sdi) |
e93cdf42 | 276 | { |
e93cdf42 BV |
277 | return agdmm_send(sdi, "CONF?"); |
278 | } | |
279 | ||
a4394fb3 | 280 | static int recv_conf_u123x(const struct sr_dev_inst *sdi, GMatchInfo *match) |
e93cdf42 BV |
281 | { |
282 | struct dev_context *devc; | |
283 | char *mstr; | |
284 | ||
38d326e8 | 285 | sr_spew("CONF? response '%s'.", g_match_info_get_string(match)); |
e93cdf42 BV |
286 | devc = sdi->priv; |
287 | mstr = g_match_info_fetch(match, 1); | |
288 | if (!strcmp(mstr, "V")) { | |
289 | devc->cur_mq = SR_MQ_VOLTAGE; | |
e6b021f3 BV |
290 | devc->cur_unit = SR_UNIT_VOLT; |
291 | devc->cur_mqflags = 0; | |
e93cdf42 BV |
292 | devc->cur_divider = 0; |
293 | } else if(!strcmp(mstr, "MV")) { | |
e6b021f3 BV |
294 | if (devc->mode_tempaux) { |
295 | devc->cur_mq = SR_MQ_TEMPERATURE; | |
296 | /* No way to detect whether Fahrenheit or Celcius | |
297 | * is used, so we'll just default to Celcius. */ | |
298 | devc->cur_unit = SR_UNIT_CELSIUS; | |
299 | devc->cur_mqflags = 0; | |
300 | devc->cur_divider = 0; | |
301 | } else { | |
302 | devc->cur_mq = SR_MQ_VOLTAGE; | |
303 | devc->cur_unit = SR_UNIT_VOLT; | |
304 | devc->cur_mqflags = 0; | |
305 | devc->cur_divider = 1000; | |
306 | } | |
e93cdf42 BV |
307 | } else if(!strcmp(mstr, "A")) { |
308 | devc->cur_mq = SR_MQ_CURRENT; | |
e6b021f3 BV |
309 | devc->cur_unit = SR_UNIT_AMPERE; |
310 | devc->cur_mqflags = 0; | |
e93cdf42 BV |
311 | devc->cur_divider = 0; |
312 | } else if(!strcmp(mstr, "UA")) { | |
313 | devc->cur_mq = SR_MQ_CURRENT; | |
e6b021f3 BV |
314 | devc->cur_unit = SR_UNIT_AMPERE; |
315 | devc->cur_mqflags = 0; | |
e93cdf42 BV |
316 | devc->cur_divider = 1000000; |
317 | } else if(!strcmp(mstr, "FREQ")) { | |
318 | devc->cur_mq = SR_MQ_FREQUENCY; | |
e6b021f3 BV |
319 | devc->cur_unit = SR_UNIT_HERTZ; |
320 | devc->cur_mqflags = 0; | |
e93cdf42 BV |
321 | devc->cur_divider = 0; |
322 | } else if(!strcmp(mstr, "RES")) { | |
e6b021f3 BV |
323 | if (devc->mode_continuity) { |
324 | devc->cur_mq = SR_MQ_CONTINUITY; | |
325 | devc->cur_unit = SR_UNIT_BOOLEAN; | |
326 | } else { | |
327 | devc->cur_mq = SR_MQ_RESISTANCE; | |
328 | devc->cur_unit = SR_UNIT_OHM; | |
329 | } | |
330 | devc->cur_mqflags = 0; | |
e93cdf42 BV |
331 | devc->cur_divider = 0; |
332 | } else if(!strcmp(mstr, "CAP")) { | |
333 | devc->cur_mq = SR_MQ_CAPACITANCE; | |
e6b021f3 BV |
334 | devc->cur_unit = SR_UNIT_FARAD; |
335 | devc->cur_mqflags = 0; | |
e93cdf42 | 336 | devc->cur_divider = 0; |
e93cdf42 | 337 | } else |
38d326e8 | 338 | sr_dbg("Unknown first argument."); |
e93cdf42 BV |
339 | g_free(mstr); |
340 | ||
e066c32a BV |
341 | if (g_match_info_get_match_count(match) == 4) { |
342 | mstr = g_match_info_fetch(match, 3); | |
e93cdf42 BV |
343 | /* Third value, if present, is always AC or DC. */ |
344 | if (!strcmp(mstr, "AC")) | |
e6b021f3 | 345 | devc->cur_mqflags |= SR_MQFLAG_AC; |
e93cdf42 | 346 | else if (!strcmp(mstr, "DC")) |
e6b021f3 | 347 | devc->cur_mqflags |= SR_MQFLAG_DC; |
e93cdf42 | 348 | else |
38d326e8 | 349 | sr_dbg("Unknown third argument."); |
e93cdf42 | 350 | g_free(mstr); |
e6b021f3 BV |
351 | } else |
352 | devc->cur_mqflags &= ~(SR_MQFLAG_AC | SR_MQFLAG_DC); | |
e93cdf42 BV |
353 | |
354 | return SR_OK; | |
355 | } | |
356 | ||
8c0152f2 BV |
357 | static int recv_conf_u125x(const struct sr_dev_inst *sdi, GMatchInfo *match) |
358 | { | |
359 | struct dev_context *devc; | |
360 | char *mstr; | |
361 | ||
38d326e8 | 362 | sr_spew("CONF? response '%s'.", g_match_info_get_string(match)); |
8c0152f2 BV |
363 | devc = sdi->priv; |
364 | mstr = g_match_info_fetch(match, 1); | |
365 | if (!strncmp(mstr, "VOLT", 4)) { | |
366 | devc->cur_mq = SR_MQ_VOLTAGE; | |
367 | devc->cur_unit = SR_UNIT_VOLT; | |
368 | devc->cur_mqflags = 0; | |
369 | devc->cur_divider = 0; | |
370 | if (mstr[4] == ':') { | |
371 | if (!strcmp(mstr + 4, "AC")) | |
372 | devc->cur_mqflags |= SR_MQFLAG_AC; | |
373 | else if (!strcmp(mstr + 4, "DC")) | |
374 | devc->cur_mqflags |= SR_MQFLAG_DC; | |
375 | else | |
376 | /* "ACDC" appears as well, no idea what it means. */ | |
377 | devc->cur_mqflags &= ~(SR_MQFLAG_AC | SR_MQFLAG_DC); | |
378 | } else | |
379 | devc->cur_mqflags &= ~(SR_MQFLAG_AC | SR_MQFLAG_DC); | |
380 | } else if(!strcmp(mstr, "CURR")) { | |
381 | devc->cur_mq = SR_MQ_CURRENT; | |
382 | devc->cur_unit = SR_UNIT_AMPERE; | |
383 | devc->cur_mqflags = 0; | |
384 | devc->cur_divider = 0; | |
385 | } else if(!strcmp(mstr, "RES")) { | |
386 | if (devc->mode_continuity) { | |
387 | devc->cur_mq = SR_MQ_CONTINUITY; | |
388 | devc->cur_unit = SR_UNIT_BOOLEAN; | |
389 | } else { | |
390 | devc->cur_mq = SR_MQ_RESISTANCE; | |
391 | devc->cur_unit = SR_UNIT_OHM; | |
392 | } | |
393 | devc->cur_mqflags = 0; | |
394 | devc->cur_divider = 0; | |
38d326e8 UH |
395 | } else |
396 | sr_dbg("Unknown first argument."); | |
8c0152f2 BV |
397 | g_free(mstr); |
398 | ||
8c0152f2 BV |
399 | return SR_OK; |
400 | } | |
401 | ||
a4394fb3 BV |
402 | /* At least the 123x and 125x appear to have this. */ |
403 | static int recv_conf(const struct sr_dev_inst *sdi, GMatchInfo *match) | |
404 | { | |
405 | struct dev_context *devc; | |
406 | char *mstr; | |
407 | ||
38d326e8 | 408 | sr_spew("CONF? response '%s'.", g_match_info_get_string(match)); |
a4394fb3 BV |
409 | devc = sdi->priv; |
410 | mstr = g_match_info_fetch(match, 1); | |
411 | if(!strcmp(mstr, "DIOD")) { | |
412 | devc->cur_mq = SR_MQ_VOLTAGE; | |
413 | devc->cur_unit = SR_UNIT_VOLT; | |
414 | devc->cur_mqflags = SR_MQFLAG_DIODE; | |
415 | devc->cur_divider = 0; | |
8c0152f2 | 416 | } else |
38d326e8 | 417 | sr_dbg("Unknown single argument."); |
a4394fb3 BV |
418 | g_free(mstr); |
419 | ||
420 | return SR_OK; | |
421 | } | |
422 | ||
81599cc5 BV |
423 | /* This comes in whenever the rotary switch is changed to a new position. |
424 | * We could use it to determine the major measurement mode, but we already | |
425 | * have the output of CONF? for that, which is more detailed. However | |
426 | * we do need to catch this here, or it'll show up in some other output. */ | |
a4394fb3 | 427 | static int recv_switch(const struct sr_dev_inst *sdi, GMatchInfo *match) |
e93cdf42 | 428 | { |
e93cdf42 BV |
429 | (void)sdi; |
430 | ||
38d326e8 | 431 | sr_spew("Switch '%s'.", g_match_info_get_string(match)); |
e93cdf42 BV |
432 | |
433 | return SR_OK; | |
434 | } | |
435 | ||
8c0152f2 | 436 | SR_PRIV const struct agdmm_job agdmm_jobs_u123x[] = { |
a4394fb3 BV |
437 | { 143, send_stat }, |
438 | { 1000, send_conf }, | |
439 | { 143, send_fetc }, | |
e93cdf42 BV |
440 | { 0, NULL } |
441 | }; | |
442 | ||
8c0152f2 BV |
443 | SR_PRIV const struct agdmm_recv agdmm_recvs_u123x[] = { |
444 | { "^\"(\\d\\d.{18}\\d)\"$", recv_stat_u123x }, | |
a4394fb3 BV |
445 | { "^\\*([0-9])$", recv_switch }, |
446 | { "^([-+][0-9]\\.[0-9]{8}E[-+][0-9]{2})$", recv_fetc }, | |
447 | { "^\"(V|MV|A|UA|FREQ),(\\d),(AC|DC)\"$", recv_conf_u123x }, | |
448 | { "^\"(RES|CAP),(\\d)\"$", recv_conf_u123x}, | |
449 | { "^\"(DIOD)\"$", recv_conf }, | |
e93cdf42 BV |
450 | { NULL, NULL } |
451 | }; | |
452 | ||
8c0152f2 BV |
453 | SR_PRIV const struct agdmm_job agdmm_jobs_u125x[] = { |
454 | { 143, send_stat }, | |
455 | { 1000, send_conf }, | |
456 | { 143, send_fetc }, | |
457 | { 0, NULL } | |
458 | }; | |
459 | ||
460 | SR_PRIV const struct agdmm_recv agdmm_recvs_u125x[] = { | |
461 | { "^\"(\\d\\d.{18}\\d)\"$", recv_stat_u125x }, | |
462 | { "^\\*([0-9])$", recv_switch }, | |
463 | { "^([-+][0-9]\\.[0-9]{8}E[-+][0-9]{2})$", recv_fetc }, | |
464 | { "^(VOLT|CURR|RES|CAP) ([-+][0-9\\.E\\-+]+),([-+][0-9\\.E\\-+]+)$", recv_conf_u125x }, | |
465 | { "^(VOLT:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9\\.E\\-+]+)$", recv_conf_u125x }, | |
466 | { "^\"(DIOD)\"$", recv_conf }, | |
467 | { NULL, NULL } | |
468 | }; |