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/>.
21 #include "libsigrok.h"
22 #include "libsigrok-internal.h"
23 #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(serial, devc->buf + devc->buflen, 1);
113 *(devc->buf + devc->buflen) = '\0';
114 if (*(devc->buf + devc->buflen - 1) == '\n') {
124 if (devc->limit_samples && devc->num_samples >= devc->limit_samples)
125 sdi->driver->dev_acquisition_stop(sdi, cb_data);
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))
140 strncat(buf, "\r\n", 32);
142 strncat(buf, "\n\r\n", 32);
143 if (serial_write(serial, buf, strlen(buf)) == -1) {
144 sr_err("Failed to send: %s.", strerror(errno));
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_u125x(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 /* Peak hold mode. */
211 devc->cur_mqflags |= SR_MQFLAG_MAX;
213 devc->cur_mqflags &= ~SR_MQFLAG_MAX;
215 /* Triggered hold mode. */
217 devc->cur_mqflags |= SR_MQFLAG_HOLD;
219 devc->cur_mqflags &= ~SR_MQFLAG_HOLD;
226 static int send_fetc(const struct sr_dev_inst *sdi)
228 return agdmm_send(sdi, "FETC?");
231 static int recv_fetc(const struct sr_dev_inst *sdi, GMatchInfo *match)
233 struct dev_context *devc;
234 struct sr_datafeed_packet packet;
235 struct sr_datafeed_analog analog;
239 sr_spew("FETC reply '%s'.", g_match_info_get_string(match));
242 if (devc->cur_mq == -1)
243 /* Haven't seen configuration yet, so can't know what
244 * the fetched float means. Not really an error, we'll
245 * get metadata soon enough. */
248 if (!strcmp(g_match_info_get_string(match), "+9.90000000E+37")) {
249 /* An invalid measurement shows up on the display as "O.L", but
250 * comes through like this. Since comparing 38-digit floats
251 * is rather problematic, we'll cut through this here. */
254 mstr = g_match_info_fetch(match, 1);
255 fvalue = strtof(mstr, &eptr);
257 if (fvalue == 0.0 && eptr == mstr) {
258 sr_err("Invalid float.");
261 if (devc->cur_divider > 0)
262 fvalue /= devc->cur_divider;
265 memset(&analog, 0, sizeof(struct sr_datafeed_analog));
266 analog.mq = devc->cur_mq;
267 analog.unit = devc->cur_unit;
268 analog.mqflags = devc->cur_mqflags;
269 analog.probes = sdi->probes;
270 analog.num_samples = 1;
271 analog.data = &fvalue;
272 packet.type = SR_DF_ANALOG;
273 packet.payload = &analog;
274 sr_session_send(devc->cb_data, &packet);
281 static int send_conf(const struct sr_dev_inst *sdi)
283 return agdmm_send(sdi, "CONF?");
286 static int recv_conf_u123x(const struct sr_dev_inst *sdi, GMatchInfo *match)
288 struct dev_context *devc;
291 sr_spew("CONF? response '%s'.", g_match_info_get_string(match));
293 mstr = g_match_info_fetch(match, 1);
294 if (!strcmp(mstr, "V")) {
295 devc->cur_mq = SR_MQ_VOLTAGE;
296 devc->cur_unit = SR_UNIT_VOLT;
297 devc->cur_mqflags = 0;
298 devc->cur_divider = 0;
299 } else if(!strcmp(mstr, "MV")) {
300 if (devc->mode_tempaux) {
301 devc->cur_mq = SR_MQ_TEMPERATURE;
302 /* No way to detect whether Fahrenheit or Celcius
303 * is used, so we'll just default to Celcius. */
304 devc->cur_unit = SR_UNIT_CELSIUS;
305 devc->cur_mqflags = 0;
306 devc->cur_divider = 0;
308 devc->cur_mq = SR_MQ_VOLTAGE;
309 devc->cur_unit = SR_UNIT_VOLT;
310 devc->cur_mqflags = 0;
311 devc->cur_divider = 1000;
313 } else if(!strcmp(mstr, "A")) {
314 devc->cur_mq = SR_MQ_CURRENT;
315 devc->cur_unit = SR_UNIT_AMPERE;
316 devc->cur_mqflags = 0;
317 devc->cur_divider = 0;
318 } else if(!strcmp(mstr, "UA")) {
319 devc->cur_mq = SR_MQ_CURRENT;
320 devc->cur_unit = SR_UNIT_AMPERE;
321 devc->cur_mqflags = 0;
322 devc->cur_divider = 1000000;
323 } else if(!strcmp(mstr, "FREQ")) {
324 devc->cur_mq = SR_MQ_FREQUENCY;
325 devc->cur_unit = SR_UNIT_HERTZ;
326 devc->cur_mqflags = 0;
327 devc->cur_divider = 0;
328 } else if(!strcmp(mstr, "RES")) {
329 if (devc->mode_continuity) {
330 devc->cur_mq = SR_MQ_CONTINUITY;
331 devc->cur_unit = SR_UNIT_BOOLEAN;
333 devc->cur_mq = SR_MQ_RESISTANCE;
334 devc->cur_unit = SR_UNIT_OHM;
336 devc->cur_mqflags = 0;
337 devc->cur_divider = 0;
338 } else if(!strcmp(mstr, "CAP")) {
339 devc->cur_mq = SR_MQ_CAPACITANCE;
340 devc->cur_unit = SR_UNIT_FARAD;
341 devc->cur_mqflags = 0;
342 devc->cur_divider = 0;
344 sr_dbg("Unknown first argument.");
347 if (g_match_info_get_match_count(match) == 4) {
348 mstr = g_match_info_fetch(match, 3);
349 /* Third value, if present, is always AC or DC. */
350 if (!strcmp(mstr, "AC"))
351 devc->cur_mqflags |= SR_MQFLAG_AC;
352 else if (!strcmp(mstr, "DC"))
353 devc->cur_mqflags |= SR_MQFLAG_DC;
355 sr_dbg("Unknown third argument.");
358 devc->cur_mqflags &= ~(SR_MQFLAG_AC | SR_MQFLAG_DC);
363 static int recv_conf_u125x(const struct sr_dev_inst *sdi, GMatchInfo *match)
365 struct dev_context *devc;
368 sr_spew("CONF? response '%s'.", g_match_info_get_string(match));
370 mstr = g_match_info_fetch(match, 1);
371 if (!strncmp(mstr, "VOLT", 4)) {
372 devc->cur_mq = SR_MQ_VOLTAGE;
373 devc->cur_unit = SR_UNIT_VOLT;
374 devc->cur_mqflags = 0;
375 devc->cur_divider = 0;
376 if (mstr[4] == ':') {
377 if (!strcmp(mstr + 4, "AC"))
378 devc->cur_mqflags |= SR_MQFLAG_AC;
379 else if (!strcmp(mstr + 4, "DC"))
380 devc->cur_mqflags |= SR_MQFLAG_DC;
382 /* "ACDC" appears as well, no idea what it means. */
383 devc->cur_mqflags &= ~(SR_MQFLAG_AC | SR_MQFLAG_DC);
385 devc->cur_mqflags &= ~(SR_MQFLAG_AC | SR_MQFLAG_DC);
386 } else if(!strcmp(mstr, "CURR")) {
387 devc->cur_mq = SR_MQ_CURRENT;
388 devc->cur_unit = SR_UNIT_AMPERE;
389 devc->cur_mqflags = 0;
390 devc->cur_divider = 0;
391 } else if(!strcmp(mstr, "RES")) {
392 if (devc->mode_continuity) {
393 devc->cur_mq = SR_MQ_CONTINUITY;
394 devc->cur_unit = SR_UNIT_BOOLEAN;
396 devc->cur_mq = SR_MQ_RESISTANCE;
397 devc->cur_unit = SR_UNIT_OHM;
399 devc->cur_mqflags = 0;
400 devc->cur_divider = 0;
402 sr_dbg("Unknown first argument.");
408 /* At least the 123x and 125x appear to have this. */
409 static int recv_conf(const struct sr_dev_inst *sdi, GMatchInfo *match)
411 struct dev_context *devc;
414 sr_spew("CONF? response '%s'.", g_match_info_get_string(match));
416 mstr = g_match_info_fetch(match, 1);
417 if(!strcmp(mstr, "DIOD")) {
418 devc->cur_mq = SR_MQ_VOLTAGE;
419 devc->cur_unit = SR_UNIT_VOLT;
420 devc->cur_mqflags = SR_MQFLAG_DIODE;
421 devc->cur_divider = 0;
423 sr_dbg("Unknown single argument.");
429 /* This comes in whenever the rotary switch is changed to a new position.
430 * We could use it to determine the major measurement mode, but we already
431 * have the output of CONF? for that, which is more detailed. However
432 * we do need to catch this here, or it'll show up in some other output. */
433 static int recv_switch(const struct sr_dev_inst *sdi, GMatchInfo *match)
437 sr_spew("Switch '%s'.", g_match_info_get_string(match));
442 SR_PRIV const struct agdmm_job agdmm_jobs_u123x[] = {
449 SR_PRIV const struct agdmm_recv agdmm_recvs_u123x[] = {
450 { "^\"(\\d\\d.{18}\\d)\"$", recv_stat_u123x },
451 { "^\\*([0-9])$", recv_switch },
452 { "^([-+][0-9]\\.[0-9]{8}E[-+][0-9]{2})$", recv_fetc },
453 { "^\"(V|MV|A|UA|FREQ),(\\d),(AC|DC)\"$", recv_conf_u123x },
454 { "^\"(RES|CAP),(\\d)\"$", recv_conf_u123x},
455 { "^\"(DIOD)\"$", recv_conf },
459 SR_PRIV const struct agdmm_job agdmm_jobs_u125x[] = {
466 SR_PRIV const struct agdmm_recv agdmm_recvs_u125x[] = {
467 { "^\"(\\d\\d.{18}\\d)\"$", recv_stat_u125x },
468 { "^\\*([0-9])$", recv_switch },
469 { "^([-+][0-9]\\.[0-9]{8}E[-+][0-9]{2})$", recv_fetc },
470 { "^(VOLT|CURR|RES|CAP) ([-+][0-9\\.E\\-+]+),([-+][0-9\\.E\\-+]+)$", recv_conf_u125x },
471 { "^(VOLT:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9\\.E\\-+]+)$", recv_conf_u125x },
472 { "^\"(DIOD)\"$", recv_conf },