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/>.
25 #include <libsigrok/libsigrok.h>
26 #include "libsigrok-internal.h"
27 #include "agilent-dmm.h"
29 static void dispatch(const struct sr_dev_inst *sdi)
31 struct dev_context *devc;
32 const struct agdmm_job *jobs;
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) {
41 sr_spew("Running job %d.", i);
42 (&jobs[i])->send(sdi);
43 devc->jobqueue[i] = now;
48 static void receive_line(const struct sr_dev_inst *sdi)
50 struct dev_context *devc;
51 const struct agdmm_recv *recvs, *recv;
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';
66 sr_spew("Received '%s'.", devc->buf);
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)) {
76 g_match_info_unref(match);
80 recv->recv(sdi, match);
81 g_match_info_unref(match);
84 sr_dbg("Unknown line '%s'.", devc->buf);
90 SR_PRIV int agdmm_receive_data(int fd, int revents, void *cb_data)
92 struct sr_dev_inst *sdi;
93 struct dev_context *devc;
94 struct sr_serial_dev_inst *serial;
102 if (!(devc = sdi->priv))
106 if (revents == G_IO_IN) {
107 /* Serial data arrived. */
108 while (AGDMM_BUFSIZE - devc->buflen - 1 > 0) {
109 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, 1);
113 *(devc->buf + devc->buflen) = '\0';
114 if (*(devc->buf + devc->buflen - 1) == '\n') {
124 if (sr_sw_limits_check(&devc->limits))
125 sdi->driver->dev_acquisition_stop(sdi);
130 static int agdmm_send(const struct sr_dev_inst *sdi, const char *cmd)
132 struct sr_serial_dev_inst *serial;
137 sr_spew("Sending '%s'.", cmd);
138 strncpy(buf, cmd, 28);
139 if (!strncmp(buf, "*IDN?", 5))
142 strcat(buf, "\n\r\n");
143 if (serial_write_blocking(serial, buf, strlen(buf), SERIAL_WRITE_TIMEOUT_MS) < (int)strlen(buf)) {
144 sr_err("Failed to send.");
151 static int send_stat(const struct sr_dev_inst *sdi)
153 return agdmm_send(sdi, "STAT?");
156 static int recv_stat_u123x(const struct sr_dev_inst *sdi, GMatchInfo *match)
158 struct dev_context *devc;
162 s = g_match_info_fetch(match, 1);
163 sr_spew("STAT response '%s'.", s);
165 /* Max, Min or Avg mode -- no way to tell which, so we'll
166 * set both flags to denote it's not a normal measurement. */
168 devc->cur_mqflags |= SR_MQFLAG_MAX | SR_MQFLAG_MIN;
170 devc->cur_mqflags &= ~(SR_MQFLAG_MAX | SR_MQFLAG_MIN);
173 devc->cur_mqflags |= SR_MQFLAG_RELATIVE;
175 devc->cur_mqflags &= ~SR_MQFLAG_RELATIVE;
177 /* Triggered or auto hold modes. */
178 if (s[2] == '1' || s[3] == '1')
179 devc->cur_mqflags |= SR_MQFLAG_HOLD;
181 devc->cur_mqflags &= ~SR_MQFLAG_HOLD;
185 devc->mode_tempaux = TRUE;
187 devc->mode_tempaux = FALSE;
189 /* Continuity mode. */
191 devc->mode_continuity = TRUE;
193 devc->mode_continuity = FALSE;
200 static int recv_stat_u124x(const struct sr_dev_inst *sdi, GMatchInfo *match)
202 struct dev_context *devc;
206 s = g_match_info_fetch(match, 1);
207 sr_spew("STAT response '%s'.", s);
209 /* Max, Min or Avg mode -- no way to tell which, so we'll
210 * set both flags to denote it's not a normal measurement. */
212 devc->cur_mqflags |= SR_MQFLAG_MAX | SR_MQFLAG_MIN;
214 devc->cur_mqflags &= ~(SR_MQFLAG_MAX | SR_MQFLAG_MIN);
217 devc->cur_mqflags |= SR_MQFLAG_RELATIVE;
219 devc->cur_mqflags &= ~SR_MQFLAG_RELATIVE;
223 devc->cur_mqflags |= SR_MQFLAG_HOLD;
225 devc->cur_mqflags &= ~SR_MQFLAG_HOLD;
232 static int recv_stat_u125x(const struct sr_dev_inst *sdi, GMatchInfo *match)
234 struct dev_context *devc;
238 s = g_match_info_fetch(match, 1);
239 sr_spew("STAT response '%s'.", s);
241 /* Peak hold mode. */
243 devc->cur_mqflags |= SR_MQFLAG_MAX;
245 devc->cur_mqflags &= ~SR_MQFLAG_MAX;
247 /* Triggered hold mode. */
249 devc->cur_mqflags |= SR_MQFLAG_HOLD;
251 devc->cur_mqflags &= ~SR_MQFLAG_HOLD;
258 static int send_fetc(const struct sr_dev_inst *sdi)
260 return agdmm_send(sdi, "FETC?");
263 static int recv_fetc(const struct sr_dev_inst *sdi, GMatchInfo *match)
265 struct dev_context *devc;
266 struct sr_datafeed_packet packet;
267 struct sr_datafeed_analog analog;
268 struct sr_analog_encoding encoding;
269 struct sr_analog_meaning meaning;
270 struct sr_analog_spec spec;
275 sr_spew("FETC reply '%s'.", g_match_info_get_string(match));
278 if (devc->cur_mq == -1)
279 /* Haven't seen configuration yet, so can't know what
280 * the fetched float means. Not really an error, we'll
281 * get metadata soon enough. */
284 s = g_match_info_get_string(match);
285 if (!strcmp(s, "-9.90000000E+37") || !strcmp(s, "+9.90000000E+37")) {
286 /* An invalid measurement shows up on the display as "O.L", but
287 * comes through like this. Since comparing 38-digit floats
288 * is rather problematic, we'll cut through this here. */
291 mstr = g_match_info_fetch(match, 1);
292 if (sr_atof_ascii(mstr, &fvalue) != SR_OK) {
294 sr_dbg("Invalid float.");
298 if (devc->cur_exponent != 0)
299 fvalue *= powf(10, devc->cur_exponent);
302 sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
303 analog.meaning->mq = devc->cur_mq;
304 analog.meaning->unit = devc->cur_unit;
305 analog.meaning->mqflags = devc->cur_mqflags;
306 analog.meaning->channels = sdi->channels;
307 analog.num_samples = 1;
308 analog.data = &fvalue;
309 packet.type = SR_DF_ANALOG;
310 packet.payload = &analog;
311 sr_session_send(sdi, &packet);
313 sr_sw_limits_update_samples_read(&devc->limits, 1);
318 static int send_conf(const struct sr_dev_inst *sdi)
320 return agdmm_send(sdi, "CONF?");
323 static int recv_conf_u123x(const struct sr_dev_inst *sdi, GMatchInfo *match)
325 struct dev_context *devc;
328 sr_spew("CONF? response '%s'.", g_match_info_get_string(match));
330 mstr = g_match_info_fetch(match, 1);
331 if (!strcmp(mstr, "V")) {
332 devc->cur_mq = SR_MQ_VOLTAGE;
333 devc->cur_unit = SR_UNIT_VOLT;
334 devc->cur_mqflags = 0;
335 devc->cur_exponent = 0;
336 } else if (!strcmp(mstr, "MV")) {
337 if (devc->mode_tempaux) {
338 devc->cur_mq = SR_MQ_TEMPERATURE;
339 /* No way to detect whether Fahrenheit or Celsius
340 * is used, so we'll just default to Celsius. */
341 devc->cur_unit = SR_UNIT_CELSIUS;
342 devc->cur_mqflags = 0;
343 devc->cur_exponent = 0;
345 devc->cur_mq = SR_MQ_VOLTAGE;
346 devc->cur_unit = SR_UNIT_VOLT;
347 devc->cur_mqflags = 0;
348 devc->cur_exponent = -3;
350 } else if (!strcmp(mstr, "A")) {
351 devc->cur_mq = SR_MQ_CURRENT;
352 devc->cur_unit = SR_UNIT_AMPERE;
353 devc->cur_mqflags = 0;
354 devc->cur_exponent = 0;
355 } else if (!strcmp(mstr, "UA")) {
356 devc->cur_mq = SR_MQ_CURRENT;
357 devc->cur_unit = SR_UNIT_AMPERE;
358 devc->cur_mqflags = 0;
359 devc->cur_exponent = -6;
360 } else if (!strcmp(mstr, "FREQ")) {
361 devc->cur_mq = SR_MQ_FREQUENCY;
362 devc->cur_unit = SR_UNIT_HERTZ;
363 devc->cur_mqflags = 0;
364 devc->cur_exponent = 0;
365 } else if (!strcmp(mstr, "RES")) {
366 if (devc->mode_continuity) {
367 devc->cur_mq = SR_MQ_CONTINUITY;
368 devc->cur_unit = SR_UNIT_BOOLEAN;
370 devc->cur_mq = SR_MQ_RESISTANCE;
371 devc->cur_unit = SR_UNIT_OHM;
373 devc->cur_mqflags = 0;
374 devc->cur_exponent = 0;
375 } else if (!strcmp(mstr, "DIOD")) {
376 devc->cur_mq = SR_MQ_VOLTAGE;
377 devc->cur_unit = SR_UNIT_VOLT;
378 devc->cur_mqflags = SR_MQFLAG_DIODE;
379 devc->cur_exponent = 0;
380 } else if (!strcmp(mstr, "CAP")) {
381 devc->cur_mq = SR_MQ_CAPACITANCE;
382 devc->cur_unit = SR_UNIT_FARAD;
383 devc->cur_mqflags = 0;
384 devc->cur_exponent = 0;
386 sr_dbg("Unknown first argument.");
389 if (g_match_info_get_match_count(match) == 4) {
390 mstr = g_match_info_fetch(match, 3);
391 /* Third value, if present, is always AC or DC. */
392 if (!strcmp(mstr, "AC")) {
393 devc->cur_mqflags |= SR_MQFLAG_AC;
394 if (devc->cur_mq == SR_MQ_VOLTAGE)
395 devc->cur_mqflags |= SR_MQFLAG_RMS;
396 } else if (!strcmp(mstr, "DC")) {
397 devc->cur_mqflags |= SR_MQFLAG_DC;
399 sr_dbg("Unknown first argument '%s'.", mstr);
403 devc->cur_mqflags &= ~(SR_MQFLAG_AC | SR_MQFLAG_DC);
408 static int recv_conf_u124x_5x(const struct sr_dev_inst *sdi, GMatchInfo *match)
410 struct dev_context *devc;
413 sr_spew("CONF? response '%s'.", g_match_info_get_string(match));
415 mstr = g_match_info_fetch(match, 1);
416 if (!strncmp(mstr, "VOLT", 4)) {
417 devc->cur_mq = SR_MQ_VOLTAGE;
418 devc->cur_unit = SR_UNIT_VOLT;
419 devc->cur_mqflags = 0;
420 devc->cur_exponent = 0;
421 if (mstr[4] == ':') {
422 if (!strncmp(mstr + 5, "AC", 2)) {
423 devc->cur_mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
424 } else if (!strncmp(mstr + 5, "DC", 2)) {
425 devc->cur_mqflags |= SR_MQFLAG_DC;
426 } else if (!strncmp(mstr + 5, "ACDC", 4)) {
428 devc->cur_mqflags |= SR_MQFLAG_AC | SR_MQFLAG_DC | SR_MQFLAG_RMS;
430 devc->cur_mqflags &= ~(SR_MQFLAG_AC | SR_MQFLAG_DC);
433 devc->cur_mqflags &= ~(SR_MQFLAG_AC | SR_MQFLAG_DC);
434 } else if (!strcmp(mstr, "CURR")) {
435 devc->cur_mq = SR_MQ_CURRENT;
436 devc->cur_unit = SR_UNIT_AMPERE;
437 devc->cur_mqflags = 0;
438 devc->cur_exponent = 0;
439 } else if (!strcmp(mstr, "RES")) {
440 devc->cur_mq = SR_MQ_RESISTANCE;
441 devc->cur_unit = SR_UNIT_OHM;
442 devc->cur_mqflags = 0;
443 devc->cur_exponent = 0;
444 } else if (!strcmp(mstr, "CAP")) {
445 devc->cur_mq = SR_MQ_CAPACITANCE;
446 devc->cur_unit = SR_UNIT_FARAD;
447 devc->cur_mqflags = 0;
448 devc->cur_exponent = 0;
449 } else if (!strcmp(mstr, "FREQ")) {
450 devc->cur_mq = SR_MQ_FREQUENCY;
451 devc->cur_unit = SR_UNIT_HERTZ;
452 devc->cur_mqflags = 0;
453 devc->cur_exponent = 0;
454 } else if (!strcmp(mstr, "CONT")) {
455 devc->cur_mq = SR_MQ_CONTINUITY;
456 devc->cur_unit = SR_UNIT_BOOLEAN;
457 devc->cur_mqflags = 0;
458 devc->cur_exponent = 0;
459 } else if (!strcmp(mstr, "DIOD")) {
460 devc->cur_mq = SR_MQ_VOLTAGE;
461 devc->cur_unit = SR_UNIT_VOLT;
462 devc->cur_mqflags = SR_MQFLAG_DIODE;
463 devc->cur_exponent = 0;
464 } else if (!strncmp(mstr, "T1", 2) || !strncmp(mstr, "T2", 2)) {
465 devc->cur_mq = SR_MQ_TEMPERATURE;
466 m2 = g_match_info_fetch(match, 2);
467 if (!strcmp(m2, "FAR"))
468 devc->cur_unit = SR_UNIT_FAHRENHEIT;
470 devc->cur_unit = SR_UNIT_CELSIUS;
472 devc->cur_mqflags = 0;
473 devc->cur_exponent = 0;
474 } else if (!strcmp(mstr, "SCOU")) {
476 * Switch counter, not supported. Not sure what values
477 * come from FETC in this mode, or how they would map
480 } else if (!strncmp(mstr, "CPER:", 5)) {
481 devc->cur_mq = SR_MQ_CURRENT;
482 devc->cur_unit = SR_UNIT_PERCENTAGE;
483 devc->cur_mqflags = 0;
484 devc->cur_exponent = 0;
486 sr_dbg("Unknown first argument '%s'.", mstr);
493 /* This comes in whenever the rotary switch is changed to a new position.
494 * We could use it to determine the major measurement mode, but we already
495 * have the output of CONF? for that, which is more detailed. However
496 * we do need to catch this here, or it'll show up in some other output. */
497 static int recv_switch(const struct sr_dev_inst *sdi, GMatchInfo *match)
501 sr_spew("Switch '%s'.", g_match_info_get_string(match));
506 /* Poll keys/switches and values at 7Hz, mode at 1Hz. */
507 SR_PRIV const struct agdmm_job agdmm_jobs_u12xx[] = {
514 SR_PRIV const struct agdmm_recv agdmm_recvs_u123x[] = {
515 { "^\"(\\d\\d.{18}\\d)\"$", recv_stat_u123x },
516 { "^\\*([0-9])$", recv_switch },
517 { "^([-+][0-9]\\.[0-9]{8}E[-+][0-9]{2})$", recv_fetc },
518 { "^\"(V|MV|A|UA|FREQ),(\\d),(AC|DC)\"$", recv_conf_u123x },
519 { "^\"(RES|CAP),(\\d)\"$", recv_conf_u123x},
520 { "^\"(DIOD)\"$", recv_conf_u123x },
524 SR_PRIV const struct agdmm_recv agdmm_recvs_u124x[] = {
525 { "^\"(\\d\\d.{18}\\d)\"$", recv_stat_u124x },
526 { "^\\*([0-9])$", recv_switch },
527 { "^([-+][0-9]\\.[0-9]{8}E[-+][0-9]{2})$", recv_fetc },
528 { "^\"(VOLT|CURR|RES|CAP|FREQ) ([-+][0-9\\.E\\-+]+),([-+][0-9\\.E\\-+]+)\"$", recv_conf_u124x_5x },
529 { "^\"(VOLT:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9\\.E\\-+]+)\"$", recv_conf_u124x_5x },
530 { "^\"(CPER:[40]-20mA) ([-+][0-9\\.E\\-+]+),([-+][0-9\\.E\\-+]+)\"$", recv_conf_u124x_5x },
531 { "^\"(T[0-9]:[A-Z]+) ([A-Z]+)\"$", recv_conf_u124x_5x },
532 { "^\"(DIOD)\"$", recv_conf_u124x_5x },
536 SR_PRIV const struct agdmm_recv agdmm_recvs_u125x[] = {
537 { "^\"(\\d\\d.{18}\\d)\"$", recv_stat_u125x },
538 { "^\\*([0-9])$", recv_switch },
539 { "^([-+][0-9]\\.[0-9]{8}E[-+][0-9]{2})$", recv_fetc },
540 { "^\"(VOLT|CURR|RES|CAP|FREQ) ([-+][0-9\\.E\\-+]+),([-+][0-9\\.E\\-+]+)\"$", recv_conf_u124x_5x },
541 { "^\"(VOLT:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9\\.E\\-+]+)\"$", recv_conf_u124x_5x },
542 { "^\"(CPER:[40]-20mA) ([-+][0-9\\.E\\-+]+),([-+][0-9\\.E\\-+]+)\"$", recv_conf_u124x_5x },
543 { "^\"(T[0-9]:[A-Z]+) ([A-Z]+)\"$", recv_conf_u124x_5x },
544 { "^\"(DIOD)\"$", recv_conf_u124x_5x },