2 * This file is part of the libsigrok project.
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
24 #include "libsigrok.h"
25 #include "libsigrok-internal.h"
26 #include "agilent-dmm.h"
28 static void dispatch(const struct sr_dev_inst *sdi)
30 struct dev_context *devc;
31 const struct agdmm_job *jobs;
36 jobs = devc->profile->jobs;
37 now = g_get_monotonic_time() / 1000;
38 for (i = 0; (&jobs[i])->interval; i++) {
39 if (now - devc->jobqueue[i] > (&jobs[i])->interval) {
40 sr_spew("Running job %d.", i);
41 (&jobs[i])->send(sdi);
42 devc->jobqueue[i] = now;
47 static void receive_line(const struct sr_dev_inst *sdi)
49 struct dev_context *devc;
50 const struct agdmm_recv *recvs, *recv;
58 while (devc->buflen) {
59 if (*(devc->buf + devc->buflen - 1) == '\r'
60 || *(devc->buf + devc->buflen - 1) == '\n')
61 *(devc->buf + --devc->buflen) = '\0';
65 sr_spew("Received '%s'.", devc->buf);
68 recvs = devc->profile->recvs;
69 for (i = 0; (&recvs[i])->recv_regex; i++) {
70 reg = g_regex_new((&recvs[i])->recv_regex, 0, 0, NULL);
71 if (g_regex_match(reg, (char *)devc->buf, 0, &match)) {
75 g_match_info_unref(match);
79 recv->recv(sdi, match);
80 g_match_info_unref(match);
83 sr_dbg("Unknown line '%s'.", devc->buf);
89 SR_PRIV int agdmm_receive_data(int fd, int revents, void *cb_data)
91 struct sr_dev_inst *sdi;
92 struct dev_context *devc;
93 struct sr_serial_dev_inst *serial;
101 if (!(devc = sdi->priv))
105 if (revents == G_IO_IN) {
106 /* Serial data arrived. */
107 while (AGDMM_BUFSIZE - devc->buflen - 1 > 0) {
108 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, 1);
112 *(devc->buf + devc->buflen) = '\0';
113 if (*(devc->buf + devc->buflen - 1) == '\n') {
123 if (devc->limit_samples && devc->num_samples >= devc->limit_samples)
124 sdi->driver->dev_acquisition_stop(sdi, cb_data);
129 static int agdmm_send(const struct sr_dev_inst *sdi, const char *cmd)
131 struct sr_serial_dev_inst *serial;
136 sr_spew("Sending '%s'.", cmd);
137 strncpy(buf, cmd, 28);
138 if (!strncmp(buf, "*IDN?", 5))
141 strcat(buf, "\n\r\n");
142 if (serial_write_blocking(serial, buf, strlen(buf), SERIAL_WRITE_TIMEOUT_MS) < (int)strlen(buf)) {
143 sr_err("Failed to send.");
150 static int send_stat(const struct sr_dev_inst *sdi)
152 return agdmm_send(sdi, "STAT?");
155 static int recv_stat_u123x(const struct sr_dev_inst *sdi, GMatchInfo *match)
157 struct dev_context *devc;
161 s = g_match_info_fetch(match, 1);
162 sr_spew("STAT response '%s'.", s);
164 /* Max, Min or Avg mode -- no way to tell which, so we'll
165 * set both flags to denote it's not a normal measurement. */
167 devc->cur_mqflags |= SR_MQFLAG_MAX | SR_MQFLAG_MIN;
169 devc->cur_mqflags &= ~(SR_MQFLAG_MAX | SR_MQFLAG_MIN);
172 devc->cur_mqflags |= SR_MQFLAG_RELATIVE;
174 devc->cur_mqflags &= ~SR_MQFLAG_RELATIVE;
176 /* Triggered or auto hold modes. */
177 if (s[2] == '1' || s[3] == '1')
178 devc->cur_mqflags |= SR_MQFLAG_HOLD;
180 devc->cur_mqflags &= ~SR_MQFLAG_HOLD;
184 devc->mode_tempaux = TRUE;
186 devc->mode_tempaux = FALSE;
188 /* Continuity mode. */
190 devc->mode_continuity = TRUE;
192 devc->mode_continuity = FALSE;
199 static int recv_stat_u124x(const struct sr_dev_inst *sdi, GMatchInfo *match)
201 struct dev_context *devc;
205 s = g_match_info_fetch(match, 1);
206 sr_spew("STAT response '%s'.", s);
208 /* Max, Min or Avg mode -- no way to tell which, so we'll
209 * set both flags to denote it's not a normal measurement. */
211 devc->cur_mqflags |= SR_MQFLAG_MAX | SR_MQFLAG_MIN;
213 devc->cur_mqflags &= ~(SR_MQFLAG_MAX | SR_MQFLAG_MIN);
216 devc->cur_mqflags |= SR_MQFLAG_RELATIVE;
218 devc->cur_mqflags &= ~SR_MQFLAG_RELATIVE;
222 devc->cur_mqflags |= SR_MQFLAG_HOLD;
224 devc->cur_mqflags &= ~SR_MQFLAG_HOLD;
231 static int recv_stat_u125x(const struct sr_dev_inst *sdi, GMatchInfo *match)
233 struct dev_context *devc;
237 s = g_match_info_fetch(match, 1);
238 sr_spew("STAT response '%s'.", s);
240 /* Peak hold mode. */
242 devc->cur_mqflags |= SR_MQFLAG_MAX;
244 devc->cur_mqflags &= ~SR_MQFLAG_MAX;
246 /* Triggered hold mode. */
248 devc->cur_mqflags |= SR_MQFLAG_HOLD;
250 devc->cur_mqflags &= ~SR_MQFLAG_HOLD;
257 static int send_fetc(const struct sr_dev_inst *sdi)
259 return agdmm_send(sdi, "FETC?");
262 static int recv_fetc(const struct sr_dev_inst *sdi, GMatchInfo *match)
264 struct dev_context *devc;
265 struct sr_datafeed_packet packet;
266 struct sr_datafeed_analog analog;
271 sr_spew("FETC reply '%s'.", g_match_info_get_string(match));
274 if (devc->cur_mq == -1)
275 /* Haven't seen configuration yet, so can't know what
276 * the fetched float means. Not really an error, we'll
277 * get metadata soon enough. */
280 s = g_match_info_get_string(match);
281 if (!strcmp(s, "-9.90000000E+37") || !strcmp(s, "+9.90000000E+37")) {
282 /* An invalid measurement shows up on the display as "O.L", but
283 * comes through like this. Since comparing 38-digit floats
284 * is rather problematic, we'll cut through this here. */
287 mstr = g_match_info_fetch(match, 1);
288 if (sr_atof_ascii(mstr, &fvalue) != SR_OK) {
290 sr_dbg("Invalid float.");
294 if (devc->cur_divider > 0)
295 fvalue /= devc->cur_divider;
298 memset(&analog, 0, sizeof(struct sr_datafeed_analog));
299 analog.mq = devc->cur_mq;
300 analog.unit = devc->cur_unit;
301 analog.mqflags = devc->cur_mqflags;
302 analog.channels = sdi->channels;
303 analog.num_samples = 1;
304 analog.data = &fvalue;
305 packet.type = SR_DF_ANALOG;
306 packet.payload = &analog;
307 sr_session_send(devc->cb_data, &packet);
314 static int send_conf(const struct sr_dev_inst *sdi)
316 return agdmm_send(sdi, "CONF?");
319 static int recv_conf_u123x(const struct sr_dev_inst *sdi, GMatchInfo *match)
321 struct dev_context *devc;
324 sr_spew("CONF? response '%s'.", g_match_info_get_string(match));
326 mstr = g_match_info_fetch(match, 1);
327 if (!strcmp(mstr, "V")) {
328 devc->cur_mq = SR_MQ_VOLTAGE;
329 devc->cur_unit = SR_UNIT_VOLT;
330 devc->cur_mqflags = 0;
331 devc->cur_divider = 0;
332 } else if (!strcmp(mstr, "MV")) {
333 if (devc->mode_tempaux) {
334 devc->cur_mq = SR_MQ_TEMPERATURE;
335 /* No way to detect whether Fahrenheit or Celsius
336 * is used, so we'll just default to Celsius. */
337 devc->cur_unit = SR_UNIT_CELSIUS;
338 devc->cur_mqflags = 0;
339 devc->cur_divider = 0;
341 devc->cur_mq = SR_MQ_VOLTAGE;
342 devc->cur_unit = SR_UNIT_VOLT;
343 devc->cur_mqflags = 0;
344 devc->cur_divider = 1000;
346 } else if (!strcmp(mstr, "A")) {
347 devc->cur_mq = SR_MQ_CURRENT;
348 devc->cur_unit = SR_UNIT_AMPERE;
349 devc->cur_mqflags = 0;
350 devc->cur_divider = 0;
351 } else if (!strcmp(mstr, "UA")) {
352 devc->cur_mq = SR_MQ_CURRENT;
353 devc->cur_unit = SR_UNIT_AMPERE;
354 devc->cur_mqflags = 0;
355 devc->cur_divider = 1000000;
356 } else if (!strcmp(mstr, "FREQ")) {
357 devc->cur_mq = SR_MQ_FREQUENCY;
358 devc->cur_unit = SR_UNIT_HERTZ;
359 devc->cur_mqflags = 0;
360 devc->cur_divider = 0;
361 } else if (!strcmp(mstr, "RES")) {
362 if (devc->mode_continuity) {
363 devc->cur_mq = SR_MQ_CONTINUITY;
364 devc->cur_unit = SR_UNIT_BOOLEAN;
366 devc->cur_mq = SR_MQ_RESISTANCE;
367 devc->cur_unit = SR_UNIT_OHM;
369 devc->cur_mqflags = 0;
370 devc->cur_divider = 0;
371 } else if (!strcmp(mstr, "CAP")) {
372 devc->cur_mq = SR_MQ_CAPACITANCE;
373 devc->cur_unit = SR_UNIT_FARAD;
374 devc->cur_mqflags = 0;
375 devc->cur_divider = 0;
377 sr_dbg("Unknown first argument.");
380 if (g_match_info_get_match_count(match) == 4) {
381 mstr = g_match_info_fetch(match, 3);
382 /* Third value, if present, is always AC or DC. */
383 if (!strcmp(mstr, "AC")) {
384 devc->cur_mqflags |= SR_MQFLAG_AC;
385 if (devc->cur_mq == SR_MQ_VOLTAGE)
386 devc->cur_mqflags |= SR_MQFLAG_RMS;
387 } else if (!strcmp(mstr, "DC")) {
388 devc->cur_mqflags |= SR_MQFLAG_DC;
390 sr_dbg("Unknown first argument '%s'.", mstr);
394 devc->cur_mqflags &= ~(SR_MQFLAG_AC | SR_MQFLAG_DC);
399 static int recv_conf_u124x_5x(const struct sr_dev_inst *sdi, GMatchInfo *match)
401 struct dev_context *devc;
404 sr_spew("CONF? response '%s'.", g_match_info_get_string(match));
406 mstr = g_match_info_fetch(match, 1);
407 if (!strncmp(mstr, "VOLT", 4)) {
408 devc->cur_mq = SR_MQ_VOLTAGE;
409 devc->cur_unit = SR_UNIT_VOLT;
410 devc->cur_mqflags = 0;
411 devc->cur_divider = 0;
412 if (mstr[4] == ':') {
413 if (!strncmp(mstr + 5, "AC", 2)) {
414 devc->cur_mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
415 } else if (!strncmp(mstr + 5, "DC", 2)) {
416 devc->cur_mqflags |= SR_MQFLAG_DC;
417 } else if (!strncmp(mstr + 5, "ACDC", 4)) {
419 devc->cur_mqflags |= SR_MQFLAG_AC | SR_MQFLAG_DC | SR_MQFLAG_RMS;
421 devc->cur_mqflags &= ~(SR_MQFLAG_AC | SR_MQFLAG_DC);
424 devc->cur_mqflags &= ~(SR_MQFLAG_AC | SR_MQFLAG_DC);
425 } else if (!strcmp(mstr, "CURR")) {
426 devc->cur_mq = SR_MQ_CURRENT;
427 devc->cur_unit = SR_UNIT_AMPERE;
428 devc->cur_mqflags = 0;
429 devc->cur_divider = 0;
430 } else if (!strcmp(mstr, "RES")) {
431 devc->cur_mq = SR_MQ_RESISTANCE;
432 devc->cur_unit = SR_UNIT_OHM;
433 devc->cur_mqflags = 0;
434 devc->cur_divider = 0;
435 } else if (!strcmp(mstr, "CAP")) {
436 devc->cur_mq = SR_MQ_CAPACITANCE;
437 devc->cur_unit = SR_UNIT_FARAD;
438 devc->cur_mqflags = 0;
439 devc->cur_divider = 0;
440 } else if (!strcmp(mstr, "FREQ")) {
441 devc->cur_mq = SR_MQ_FREQUENCY;
442 devc->cur_unit = SR_UNIT_HERTZ;
443 devc->cur_mqflags = 0;
444 devc->cur_divider = 0;
445 } else if (!strcmp(mstr, "CONT")) {
446 devc->cur_mq = SR_MQ_CONTINUITY;
447 devc->cur_unit = SR_UNIT_BOOLEAN;
448 devc->cur_mqflags = 0;
449 devc->cur_divider = 0;
450 } else if (!strncmp(mstr, "T1", 2) || !strncmp(mstr, "T2", 2)) {
451 devc->cur_mq = SR_MQ_TEMPERATURE;
452 m2 = g_match_info_fetch(match, 2);
453 if (!strcmp(m2, "FAR"))
454 devc->cur_unit = SR_UNIT_FAHRENHEIT;
456 devc->cur_unit = SR_UNIT_CELSIUS;
458 devc->cur_mqflags = 0;
459 devc->cur_divider = 0;
460 } else if (!strcmp(mstr, "SCOU")) {
462 * Switch counter, not supported. Not sure what values
463 * come from FETC in this mode, or how they would map
466 } else if (!strncmp(mstr, "CPER:", 5)) {
467 devc->cur_mq = SR_MQ_CURRENT;
468 devc->cur_unit = SR_UNIT_PERCENTAGE;
469 devc->cur_mqflags = 0;
470 devc->cur_divider = 0;
472 sr_dbg("Unknown first argument '%s'.", mstr);
479 static int recv_conf(const struct sr_dev_inst *sdi, GMatchInfo *match)
481 struct dev_context *devc;
484 sr_spew("CONF? response '%s'.", g_match_info_get_string(match));
486 mstr = g_match_info_fetch(match, 1);
487 if (!strcmp(mstr, "DIOD")) {
488 devc->cur_mq = SR_MQ_VOLTAGE;
489 devc->cur_unit = SR_UNIT_VOLT;
490 devc->cur_mqflags = SR_MQFLAG_DIODE;
491 devc->cur_divider = 0;
493 sr_dbg("Unknown single argument.");
499 /* This comes in whenever the rotary switch is changed to a new position.
500 * We could use it to determine the major measurement mode, but we already
501 * have the output of CONF? for that, which is more detailed. However
502 * we do need to catch this here, or it'll show up in some other output. */
503 static int recv_switch(const struct sr_dev_inst *sdi, GMatchInfo *match)
507 sr_spew("Switch '%s'.", g_match_info_get_string(match));
512 /* Poll keys/switches and values at 7Hz, mode at 1Hz. */
513 SR_PRIV const struct agdmm_job agdmm_jobs_u12xx[] = {
520 SR_PRIV const struct agdmm_recv agdmm_recvs_u123x[] = {
521 { "^\"(\\d\\d.{18}\\d)\"$", recv_stat_u123x },
522 { "^\\*([0-9])$", recv_switch },
523 { "^([-+][0-9]\\.[0-9]{8}E[-+][0-9]{2})$", recv_fetc },
524 { "^\"(V|MV|A|UA|FREQ),(\\d),(AC|DC)\"$", recv_conf_u123x },
525 { "^\"(RES|CAP),(\\d)\"$", recv_conf_u123x},
526 { "^\"(DIOD)\"$", recv_conf },
530 SR_PRIV const struct agdmm_recv agdmm_recvs_u124x[] = {
531 { "^\"(\\d\\d.{18}\\d)\"$", recv_stat_u124x },
532 { "^\\*([0-9])$", recv_switch },
533 { "^([-+][0-9]\\.[0-9]{8}E[-+][0-9]{2})$", recv_fetc },
534 { "^\"(VOLT|CURR|RES|CAP|FREQ) ([-+][0-9\\.E\\-+]+),([-+][0-9\\.E\\-+]+)\"$", recv_conf_u124x_5x },
535 { "^\"(VOLT:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9\\.E\\-+]+)\"$", recv_conf_u124x_5x },
536 { "^\"(CPER:[40]-20mA) ([-+][0-9\\.E\\-+]+),([-+][0-9\\.E\\-+]+)\"$", recv_conf_u124x_5x },
537 { "^\"(T[0-9]:[A-Z]+) ([A-Z]+)\"$", recv_conf_u124x_5x },
538 { "^\"(DIOD)\"$", recv_conf },
542 SR_PRIV const struct agdmm_recv agdmm_recvs_u125x[] = {
543 { "^\"(\\d\\d.{18}\\d)\"$", recv_stat_u125x },
544 { "^\\*([0-9])$", recv_switch },
545 { "^([-+][0-9]\\.[0-9]{8}E[-+][0-9]{2})$", recv_fetc },
546 { "^\"(VOLT|CURR|RES|CAP|FREQ) ([-+][0-9\\.E\\-+]+),([-+][0-9\\.E\\-+]+)\"$", recv_conf_u124x_5x },
547 { "^\"(VOLT:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9\\.E\\-+]+)\"$", recv_conf_u124x_5x },
548 { "^\"(CPER:[40]-20mA) ([-+][0-9\\.E\\-+]+),([-+][0-9\\.E\\-+]+)\"$", recv_conf_u124x_5x },
549 { "^\"(T[0-9]:[A-Z]+) ([A-Z]+)\"$", recv_conf_u124x_5x },
550 { "^\"(DIOD)\"$", recv_conf },