]> sigrok.org Git - libsigrok.git/blob - src/hardware/agilent-dmm/protocol.c
agilent-dmm: add support for reading secondary display and temperature
[libsigrok.git] / src / hardware / agilent-dmm / protocol.c
1 /*
2  * This file is part of the libsigrok 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 <config.h>
21 #include <glib.h>
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include <string.h>
25 #include <math.h>
26 #include <libsigrok/libsigrok.h>
27 #include "libsigrok-internal.h"
28 #include "protocol.h"
29
30 static void dispatch(const struct sr_dev_inst *sdi)
31 {
32         struct dev_context *devc;
33         const struct agdmm_job *jobs;
34         int64_t now;
35         int i;
36
37         devc = sdi->priv;
38         jobs = devc->profile->jobs;
39         now = g_get_monotonic_time() / 1000;
40         for (i = 0; (&jobs[i])->interval; i++) {
41                 if (now - devc->jobqueue[i] > (&jobs[i])->interval) {
42                         sr_spew("Running job %d.", i);
43                         (&jobs[i])->send(sdi);
44                         devc->jobqueue[i] = now;
45                 }
46         }
47 }
48
49 static void receive_line(const struct sr_dev_inst *sdi)
50 {
51         struct dev_context *devc;
52         const struct agdmm_recv *recvs, *recv;
53         GRegex *reg;
54         GMatchInfo *match;
55         int i;
56
57         devc = sdi->priv;
58
59         /* Strip CRLF */
60         while (devc->buflen) {
61                 if (*(devc->buf + devc->buflen - 1) == '\r'
62                                 || *(devc->buf + devc->buflen - 1) == '\n')
63                         *(devc->buf + --devc->buflen) = '\0';
64                 else
65                         break;
66         }
67         sr_spew("Received '%s'.", devc->buf);
68
69         recv = NULL;
70         recvs = devc->profile->recvs;
71         for (i = 0; (&recvs[i])->recv_regex; i++) {
72                 reg = g_regex_new((&recvs[i])->recv_regex, 0, 0, NULL);
73                 if (g_regex_match(reg, (char *)devc->buf, 0, &match)) {
74                         recv = &recvs[i];
75                         break;
76                 }
77                 g_match_info_unref(match);
78                 g_regex_unref(reg);
79         }
80         if (recv) {
81                 recv->recv(sdi, match);
82                 g_match_info_unref(match);
83                 g_regex_unref(reg);
84         } else
85                 sr_dbg("Unknown line '%s'.", devc->buf);
86
87         /* Done with this. */
88         devc->buflen = 0;
89 }
90
91 SR_PRIV int agdmm_receive_data(int fd, int revents, void *cb_data)
92 {
93         struct sr_dev_inst *sdi;
94         struct dev_context *devc;
95         struct sr_serial_dev_inst *serial;
96         int len;
97
98         (void)fd;
99
100         if (!(sdi = cb_data))
101                 return TRUE;
102
103         if (!(devc = sdi->priv))
104                 return TRUE;
105
106         serial = sdi->conn;
107         if (revents == G_IO_IN) {
108                 /* Serial data arrived. */
109                 while (AGDMM_BUFSIZE - devc->buflen - 1 > 0) {
110                         len = serial_read_nonblocking(serial, devc->buf + devc->buflen, 1);
111                         if (len < 1)
112                                 break;
113                         devc->buflen += len;
114                         *(devc->buf + devc->buflen) = '\0';
115                         if (*(devc->buf + devc->buflen - 1) == '\n') {
116                                 /* End of line */
117                                 receive_line(sdi);
118                                 break;
119                         }
120                 }
121         }
122
123         dispatch(sdi);
124
125         if (sr_sw_limits_check(&devc->limits))
126                 sdi->driver->dev_acquisition_stop(sdi);
127
128         return TRUE;
129 }
130
131 static int agdmm_send(const struct sr_dev_inst *sdi, const char *cmd, ...)
132 {
133         struct sr_serial_dev_inst *serial;
134         va_list args;
135         char buf[32];
136
137         serial = sdi->conn;
138
139         va_start(args, cmd);
140         vsnprintf(buf, sizeof(buf)-3, cmd, args);
141         va_end(args);
142         sr_spew("Sending '%s'.", buf);
143         if (!strncmp(buf, "*IDN?", 5))
144                 strcat(buf, "\r\n");
145         else
146                 strcat(buf, "\n\r\n");
147         if (serial_write_blocking(serial, buf, strlen(buf), SERIAL_WRITE_TIMEOUT_MS) < (int)strlen(buf)) {
148                 sr_err("Failed to send.");
149                 return SR_ERR;
150         }
151
152         return SR_OK;
153 }
154
155 static int send_stat(const struct sr_dev_inst *sdi)
156 {
157         return agdmm_send(sdi, "STAT?");
158 }
159
160 static int recv_stat_u123x(const struct sr_dev_inst *sdi, GMatchInfo *match)
161 {
162         struct dev_context *devc;
163         char *s;
164
165         devc = sdi->priv;
166         s = g_match_info_fetch(match, 1);
167         sr_spew("STAT response '%s'.", s);
168
169         /* Max, Min or Avg mode -- no way to tell which, so we'll
170          * set both flags to denote it's not a normal measurement. */
171         if (s[0] == '1')
172                 devc->cur_mqflags[0] |= SR_MQFLAG_MAX | SR_MQFLAG_MIN;
173         else
174                 devc->cur_mqflags[0] &= ~(SR_MQFLAG_MAX | SR_MQFLAG_MIN);
175
176         if (s[1] == '1')
177                 devc->cur_mqflags[0] |= SR_MQFLAG_RELATIVE;
178         else
179                 devc->cur_mqflags[0] &= ~SR_MQFLAG_RELATIVE;
180
181         /* Triggered or auto hold modes. */
182         if (s[2] == '1' || s[3] == '1')
183                 devc->cur_mqflags[0] |= SR_MQFLAG_HOLD;
184         else
185                 devc->cur_mqflags[0] &= ~SR_MQFLAG_HOLD;
186
187         /* Temp/aux mode. */
188         if (s[7] == '1')
189                 devc->mode_tempaux = TRUE;
190         else
191                 devc->mode_tempaux = FALSE;
192
193         /* Continuity mode. */
194         if (s[16] == '1')
195                 devc->mode_continuity = TRUE;
196         else
197                 devc->mode_continuity = FALSE;
198
199         g_free(s);
200
201         return SR_OK;
202 }
203
204 static int recv_stat_u124x(const struct sr_dev_inst *sdi, GMatchInfo *match)
205 {
206         struct dev_context *devc;
207         char *s;
208
209         devc = sdi->priv;
210         s = g_match_info_fetch(match, 1);
211         sr_spew("STAT response '%s'.", s);
212
213         /* Max, Min or Avg mode -- no way to tell which, so we'll
214          * set both flags to denote it's not a normal measurement. */
215         if (s[0] == '1')
216                 devc->cur_mqflags[0] |= SR_MQFLAG_MAX | SR_MQFLAG_MIN;
217         else
218                 devc->cur_mqflags[0] &= ~(SR_MQFLAG_MAX | SR_MQFLAG_MIN);
219
220         if (s[1] == '1')
221                 devc->cur_mqflags[0] |= SR_MQFLAG_RELATIVE;
222         else
223                 devc->cur_mqflags[0] &= ~SR_MQFLAG_RELATIVE;
224
225         /* Hold mode. */
226         if (s[7] == '1')
227                 devc->cur_mqflags[0] |= SR_MQFLAG_HOLD;
228         else
229                 devc->cur_mqflags[0] &= ~SR_MQFLAG_HOLD;
230
231         g_free(s);
232
233         return SR_OK;
234 }
235
236 static int recv_stat_u125x(const struct sr_dev_inst *sdi, GMatchInfo *match)
237 {
238         struct dev_context *devc;
239         char *s;
240
241         devc = sdi->priv;
242         s = g_match_info_fetch(match, 1);
243         sr_spew("STAT response '%s'.", s);
244
245         /* Peak hold mode. */
246         if (s[4] == '1')
247                 devc->cur_mqflags[0] |= SR_MQFLAG_MAX;
248         else
249                 devc->cur_mqflags[0] &= ~SR_MQFLAG_MAX;
250
251         /* Triggered hold mode. */
252         if (s[7] == '1')
253                 devc->cur_mqflags[0] |= SR_MQFLAG_HOLD;
254         else
255                 devc->cur_mqflags[0] &= ~SR_MQFLAG_HOLD;
256
257         g_free(s);
258
259         return SR_OK;
260 }
261
262 static int recv_stat_u128x(const struct sr_dev_inst *sdi, GMatchInfo *match)
263 {
264         struct dev_context *devc;
265         char *s;
266
267         devc = sdi->priv;
268         s = g_match_info_fetch(match, 1);
269         sr_spew("STAT response '%s'.", s);
270
271         /* Max, Min or Avg mode -- no way to tell which, so we'll
272          * set both flags to denote it's not a normal measurement. */
273         if (s[0] == '1')
274                 devc->cur_mqflags[0] |= SR_MQFLAG_MAX | SR_MQFLAG_MIN | SR_MQFLAG_AVG;
275         else
276                 devc->cur_mqflags[0] &= ~(SR_MQFLAG_MAX | SR_MQFLAG_MIN | SR_MQFLAG_AVG);
277
278         /* Peak hold mode. */
279         if (s[4] == '4')
280                 devc->cur_mqflags[0] |= SR_MQFLAG_MAX;
281         else
282                 devc->cur_mqflags[0] &= ~SR_MQFLAG_MAX;
283
284         /* Null function. */
285         if (s[1] == '1')
286                 devc->cur_mqflags[0] |= SR_MQFLAG_RELATIVE;
287         else
288                 devc->cur_mqflags[0] &= ~SR_MQFLAG_RELATIVE;
289
290         /* Triggered or auto hold modes. */
291         if (s[7] == '1' || s[11] == '1')
292                 devc->cur_mqflags[0] |= SR_MQFLAG_HOLD;
293         else
294                 devc->cur_mqflags[0] &= ~SR_MQFLAG_HOLD;
295
296         g_free(s);
297
298         return SR_OK;
299 }
300
301 static int send_fetc(const struct sr_dev_inst *sdi)
302 {
303         struct dev_context *devc;
304         devc = sdi->priv;
305         if (devc->mode_squarewave)
306                 return SR_OK;
307         devc->cur_channel = sr_next_enabled_channel(sdi, devc->cur_channel);
308         if (devc->cur_channel->index > 0)
309                 return agdmm_send(sdi, "FETC? @%d", devc->cur_channel->index + 1);
310         else
311                 return agdmm_send(sdi, "FETC?");
312 }
313
314 static int recv_fetc(const struct sr_dev_inst *sdi, GMatchInfo *match)
315 {
316         struct dev_context *devc;
317         struct sr_datafeed_packet packet;
318         struct sr_datafeed_analog analog;
319         struct sr_analog_encoding encoding;
320         struct sr_analog_meaning meaning;
321         struct sr_analog_spec spec;
322         float fvalue;
323         const char *s;
324         char *mstr;
325         int i;
326
327         sr_spew("FETC reply '%s'.", g_match_info_get_string(match));
328         devc = sdi->priv;
329         i = devc->cur_channel->index;
330
331         if (devc->cur_mq[i] == -1)
332                 /* Haven't seen configuration yet, so can't know what
333                  * the fetched float means. Not really an error, we'll
334                  * get metadata soon enough. */
335                 return SR_OK;
336
337         s = g_match_info_get_string(match);
338         if (!strcmp(s, "-9.90000000E+37") || !strcmp(s, "+9.90000000E+37")) {
339                 /* An invalid measurement shows up on the display as "O.L", but
340                  * comes through like this. Since comparing 38-digit floats
341                  * is rather problematic, we'll cut through this here. */
342                 fvalue = NAN;
343         } else {
344                 mstr = g_match_info_fetch(match, 1);
345                 if (sr_atof_ascii(mstr, &fvalue) != SR_OK) {
346                         g_free(mstr);
347                         sr_dbg("Invalid float.");
348                         return SR_ERR;
349                 }
350                 g_free(mstr);
351                 if (devc->cur_exponent[i] != 0)
352                         fvalue *= powf(10, devc->cur_exponent[i]);
353         }
354
355         sr_analog_init(&analog, &encoding, &meaning, &spec,
356                        devc->cur_digits[i] - devc->cur_exponent[i]);
357         analog.meaning->mq = devc->cur_mq[i];
358         analog.meaning->unit = devc->cur_unit[i];
359         analog.meaning->mqflags = devc->cur_mqflags[i];
360         analog.meaning->channels = g_slist_append(NULL, devc->cur_channel);
361         analog.num_samples = 1;
362         analog.data = &fvalue;
363         encoding.digits = devc->cur_encoding[i] - devc->cur_exponent[i];
364         packet.type = SR_DF_ANALOG;
365         packet.payload = &analog;
366         sr_session_send(sdi, &packet);
367         g_slist_free(analog.meaning->channels);
368
369         sr_sw_limits_update_samples_read(&devc->limits, 1);
370
371         return SR_OK;
372 }
373
374 static int send_conf(const struct sr_dev_inst *sdi)
375 {
376         struct dev_context *devc = sdi->priv;
377
378         devc->cur_conf = sr_next_enabled_channel(sdi, devc->cur_conf);
379
380         /* Do not try to send CONF? for internal temperature channel. */
381         if (devc->cur_conf->index == MAX(devc->profile->nb_channels - 1, 1))
382                 devc->cur_conf = sr_next_enabled_channel(sdi, devc->cur_conf);
383         if (devc->cur_conf->index == MAX(devc->profile->nb_channels - 1, 1))
384                 return SR_ERR_NA;
385
386         if (devc->cur_conf->index > 0)
387                 return agdmm_send(sdi, "CONF? @%d", devc->cur_conf->index + 1);
388         else
389                 return agdmm_send(sdi, "CONF?");
390 }
391
392 static int recv_conf_u123x(const struct sr_dev_inst *sdi, GMatchInfo *match)
393 {
394         struct dev_context *devc;
395         char *mstr, *rstr;
396         int i, resolution;
397
398         sr_spew("CONF? response '%s'.", g_match_info_get_string(match));
399         devc = sdi->priv;
400         i = devc->cur_conf->index;
401
402         rstr = g_match_info_fetch(match, 2);
403         if (rstr)
404                 sr_atoi(rstr, &resolution);
405         g_free(rstr);
406
407         mstr = g_match_info_fetch(match, 1);
408         if (!strcmp(mstr, "V")) {
409                 devc->cur_mq[i] = SR_MQ_VOLTAGE;
410                 devc->cur_unit[i] = SR_UNIT_VOLT;
411                 devc->cur_mqflags[i] = 0;
412                 devc->cur_exponent[i] = 0;
413                 devc->cur_digits[i] = 4 - resolution;
414         } else if (!strcmp(mstr, "MV")) {
415                 if (devc->mode_tempaux) {
416                         devc->cur_mq[i] = SR_MQ_TEMPERATURE;
417                         /* No way to detect whether Fahrenheit or Celsius
418                          * is used, so we'll just default to Celsius. */
419                         devc->cur_unit[i] = SR_UNIT_CELSIUS;
420                         devc->cur_mqflags[i] = 0;
421                         devc->cur_exponent[i] = 0;
422                         devc->cur_digits[i] = 1;
423                 } else {
424                         devc->cur_mq[i] = SR_MQ_VOLTAGE;
425                         devc->cur_unit[i] = SR_UNIT_VOLT;
426                         devc->cur_mqflags[i] = 0;
427                         devc->cur_exponent[i] = -3;
428                         devc->cur_digits[i] = 5 - resolution;
429                 }
430         } else if (!strcmp(mstr, "A")) {
431                 devc->cur_mq[i] = SR_MQ_CURRENT;
432                 devc->cur_unit[i] = SR_UNIT_AMPERE;
433                 devc->cur_mqflags[i] = 0;
434                 devc->cur_exponent[i] = 0;
435                 devc->cur_digits[i] = 3 - resolution;
436         } else if (!strcmp(mstr, "UA")) {
437                 devc->cur_mq[i] = SR_MQ_CURRENT;
438                 devc->cur_unit[i] = SR_UNIT_AMPERE;
439                 devc->cur_mqflags[i] = 0;
440                 devc->cur_exponent[i] = -6;
441                 devc->cur_digits[i] = 8 - resolution;
442         } else if (!strcmp(mstr, "FREQ")) {
443                 devc->cur_mq[i] = SR_MQ_FREQUENCY;
444                 devc->cur_unit[i] = SR_UNIT_HERTZ;
445                 devc->cur_mqflags[i] = 0;
446                 devc->cur_exponent[i] = 0;
447                 devc->cur_digits[i] = 2 - resolution;
448         } else if (!strcmp(mstr, "RES")) {
449                 if (devc->mode_continuity) {
450                         devc->cur_mq[i] = SR_MQ_CONTINUITY;
451                         devc->cur_unit[i] = SR_UNIT_BOOLEAN;
452                 } else {
453                         devc->cur_mq[i] = SR_MQ_RESISTANCE;
454                         devc->cur_unit[i] = SR_UNIT_OHM;
455                 }
456                 devc->cur_mqflags[i] = 0;
457                 devc->cur_exponent[i] = 0;
458                 devc->cur_digits[i] = 1 - resolution;
459         } else if (!strcmp(mstr, "DIOD")) {
460                 devc->cur_mq[i] = SR_MQ_VOLTAGE;
461                 devc->cur_unit[i] = SR_UNIT_VOLT;
462                 devc->cur_mqflags[i] = SR_MQFLAG_DIODE;
463                 devc->cur_exponent[i] = 0;
464                 devc->cur_digits[i] = 3;
465         } else if (!strcmp(mstr, "CAP")) {
466                 devc->cur_mq[i] = SR_MQ_CAPACITANCE;
467                 devc->cur_unit[i] = SR_UNIT_FARAD;
468                 devc->cur_mqflags[i] = 0;
469                 devc->cur_exponent[i] = 0;
470                 devc->cur_digits[i] = 9 - resolution;
471         } else
472                 sr_dbg("Unknown first argument.");
473         g_free(mstr);
474
475         /* This is based on guess, supposing similarity with other models. */
476         devc->cur_encoding[i] = devc->cur_digits[i] + 1;
477
478         if (g_match_info_get_match_count(match) == 4) {
479                 mstr = g_match_info_fetch(match, 3);
480                 /* Third value, if present, is always AC or DC. */
481                 if (!strcmp(mstr, "AC")) {
482                         devc->cur_mqflags[i] |= SR_MQFLAG_AC;
483                         if (devc->cur_mq[i] == SR_MQ_VOLTAGE)
484                                 devc->cur_mqflags[i] |= SR_MQFLAG_RMS;
485                 } else if (!strcmp(mstr, "DC")) {
486                         devc->cur_mqflags[i] |= SR_MQFLAG_DC;
487                 } else {
488                 sr_dbg("Unknown first argument '%s'.", mstr);
489                 }
490                 g_free(mstr);
491         } else
492                 devc->cur_mqflags[i] &= ~(SR_MQFLAG_AC | SR_MQFLAG_DC);
493
494         return SR_OK;
495 }
496
497 static int recv_conf_u124x_5x(const struct sr_dev_inst *sdi, GMatchInfo *match)
498 {
499         struct dev_context *devc;
500         char *mstr, *rstr, *m2;
501         int i, resolution;
502
503         sr_spew("CONF? response '%s'.", g_match_info_get_string(match));
504         devc = sdi->priv;
505         i = devc->cur_conf->index;
506
507         devc->mode_squarewave = 0;
508
509         rstr = g_match_info_fetch(match, 4);
510         if (rstr && sr_atoi(rstr, &resolution) == SR_OK) {
511                 devc->cur_digits[i] = -resolution;
512                 devc->cur_encoding[i] = -resolution + 1;
513         }
514         g_free(rstr);
515
516         mstr = g_match_info_fetch(match, 1);
517         if (!strncmp(mstr, "VOLT", 4)) {
518                 devc->cur_mq[i] = SR_MQ_VOLTAGE;
519                 devc->cur_unit[i] = SR_UNIT_VOLT;
520                 devc->cur_mqflags[i] = 0;
521                 devc->cur_exponent[i] = 0;
522                 if (mstr[4] == ':') {
523                         if (!strncmp(mstr + 5, "ACDC", 4)) {
524                                 /* AC + DC offset */
525                                 devc->cur_mqflags[i] |= SR_MQFLAG_AC | SR_MQFLAG_DC | SR_MQFLAG_RMS;
526                         } else if (!strncmp(mstr + 5, "AC", 2)) {
527                                 devc->cur_mqflags[i] |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
528                         } else if (!strncmp(mstr + 5, "DC", 2)) {
529                                 devc->cur_mqflags[i] |= SR_MQFLAG_DC;
530                         }
531                 } else
532                         devc->cur_mqflags[i] |= SR_MQFLAG_DC;
533         } else if (!strncmp(mstr, "CURR", 4)) {
534                 devc->cur_mq[i] = SR_MQ_CURRENT;
535                 devc->cur_unit[i] = SR_UNIT_AMPERE;
536                 devc->cur_mqflags[i] = 0;
537                 devc->cur_exponent[i] = 0;
538                 if (mstr[4] == ':') {
539                         if (!strncmp(mstr + 5, "ACDC", 4)) {
540                                 /* AC + DC offset */
541                                 devc->cur_mqflags[i] |= SR_MQFLAG_AC | SR_MQFLAG_DC | SR_MQFLAG_RMS;
542                         } else if (!strncmp(mstr + 5, "AC", 2)) {
543                                 devc->cur_mqflags[i] |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
544                         } else if (!strncmp(mstr + 5, "DC", 2)) {
545                                 devc->cur_mqflags[i] |= SR_MQFLAG_DC;
546                         }
547                 } else
548                         devc->cur_mqflags[i] |= SR_MQFLAG_DC;
549         } else if (!strcmp(mstr, "RES")) {
550                 devc->cur_mq[i] = SR_MQ_RESISTANCE;
551                 devc->cur_unit[i] = SR_UNIT_OHM;
552                 devc->cur_mqflags[i] = 0;
553                 devc->cur_exponent[i] = 0;
554         } else if (!strcmp(mstr, "COND")) {
555                 devc->cur_mq[i] = SR_MQ_CONDUCTANCE;
556                 devc->cur_unit[i] = SR_UNIT_SIEMENS;
557                 devc->cur_mqflags[i] = 0;
558                 devc->cur_exponent[i] = 0;
559         } else if (!strcmp(mstr, "CAP")) {
560                 devc->cur_mq[i] = SR_MQ_CAPACITANCE;
561                 devc->cur_unit[i] = SR_UNIT_FARAD;
562                 devc->cur_mqflags[i] = 0;
563                 devc->cur_exponent[i] = 0;
564         } else if (!strncmp(mstr, "FREQ", 4) || !strncmp(mstr, "FC1", 3)) {
565                 devc->cur_mq[i] = SR_MQ_FREQUENCY;
566                 devc->cur_unit[i] = SR_UNIT_HERTZ;
567                 devc->cur_mqflags[i] = 0;
568                 devc->cur_exponent[i] = 0;
569         } else if (!strcmp(mstr, "CONT")) {
570                 devc->cur_mq[i] = SR_MQ_CONTINUITY;
571                 devc->cur_unit[i] = SR_UNIT_BOOLEAN;
572                 devc->cur_mqflags[i] = 0;
573                 devc->cur_exponent[i] = 0;
574         } else if (!strcmp(mstr, "DIOD")) {
575                 devc->cur_mq[i] = SR_MQ_VOLTAGE;
576                 devc->cur_unit[i] = SR_UNIT_VOLT;
577                 devc->cur_mqflags[i] = SR_MQFLAG_DIODE;
578                 devc->cur_exponent[i] = 0;
579                 devc->cur_digits[i] = 4;
580                 devc->cur_encoding[i] = 5;
581         } else if (!strncmp(mstr, "T1", 2) || !strncmp(mstr, "T2", 2) ||
582                    !strncmp(mstr, "TEMP", 2)) {
583                 devc->cur_mq[i] = SR_MQ_TEMPERATURE;
584                 m2 = g_match_info_fetch(match, 2);
585                 if (!m2)
586                         /*
587                          * TEMP without param is for secondary display (channel P2)
588                          * and is identical to channel P3, so discard it.
589                          */
590                         devc->cur_mq[i] = -1;
591                 else if (!strcmp(m2, "FAR"))
592                         devc->cur_unit[i] = SR_UNIT_FAHRENHEIT;
593                 else
594                         devc->cur_unit[i] = SR_UNIT_CELSIUS;
595                 g_free(m2);
596                 devc->cur_mqflags[i] = 0;
597                 devc->cur_exponent[i] = 0;
598                 devc->cur_digits[i] = 1;
599                 devc->cur_encoding[i] = 2;
600         } else if (!strcmp(mstr, "SCOU")) {
601                 /*
602                  * Switch counter, not supported. Not sure what values
603                  * come from FETC in this mode, or how they would map
604                  * into libsigrok.
605                  */
606         } else if (!strncmp(mstr, "CPER:", 5)) {
607                 devc->cur_mq[i] = SR_MQ_CURRENT;
608                 devc->cur_unit[i] = SR_UNIT_PERCENTAGE;
609                 devc->cur_mqflags[i] = 0;
610                 devc->cur_exponent[i] = 0;
611                 devc->cur_digits[i] = 2;
612                 devc->cur_encoding[i] = 3;
613         } else if (!strcmp(mstr, "SQU")) {
614                 /*
615                  * Square wave output, not supported. FETC just return
616                  * an error in this mode, so don't even call it.
617                  */
618                 devc->mode_squarewave = 1;
619         } else {
620                 sr_dbg("Unknown first argument '%s'.", mstr);
621         }
622         g_free(mstr);
623
624         return SR_OK;
625 }
626
627 /* This comes in whenever the rotary switch is changed to a new position.
628  * We could use it to determine the major measurement mode, but we already
629  * have the output of CONF? for that, which is more detailed. However
630  * we do need to catch this here, or it'll show up in some other output. */
631 static int recv_switch(const struct sr_dev_inst *sdi, GMatchInfo *match)
632 {
633         (void)sdi;
634
635         sr_spew("Switch '%s'.", g_match_info_get_string(match));
636
637         return SR_OK;
638 }
639
640 /* Poll keys/switches and values at 7Hz, mode at 1Hz. */
641 SR_PRIV const struct agdmm_job agdmm_jobs_u12xx[] = {
642         { 143, send_stat },
643         { 1000, send_conf },
644         { 143, send_fetc },
645         ALL_ZERO
646 };
647
648 SR_PRIV const struct agdmm_recv agdmm_recvs_u123x[] = {
649         { "^\"(\\d\\d.{18}\\d)\"$", recv_stat_u123x },
650         { "^\\*([0-9])$", recv_switch },
651         { "^([-+][0-9]\\.[0-9]{8}E[-+][0-9]{2})$", recv_fetc },
652         { "^\"(V|MV|A|UA|FREQ),(\\d),(AC|DC)\"$", recv_conf_u123x },
653         { "^\"(RES|CAP),(\\d)\"$", recv_conf_u123x},
654         { "^\"(DIOD)\"$", recv_conf_u123x },
655         ALL_ZERO
656 };
657
658 SR_PRIV const struct agdmm_recv agdmm_recvs_u124x[] = {
659         { "^\"(\\d\\d.{18}\\d)\"$", recv_stat_u124x },
660         { "^\\*([0-9])$", recv_switch },
661         { "^([-+][0-9]\\.[0-9]{8}E[-+][0-9]{2})$", recv_fetc },
662         { "^\"(VOLT|CURR|RES|CAP|FREQ) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x },
663         { "^\"(VOLT:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x },
664         { "^\"(CURR:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x },
665         { "^\"(CPER:[40]-20mA) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x },
666         { "^\"(T[0-9]:[A-Z]+) ([A-Z]+)\"$", recv_conf_u124x_5x },
667         { "^\"(DIOD)\"$", recv_conf_u124x_5x },
668         ALL_ZERO
669 };
670
671 SR_PRIV const struct agdmm_recv agdmm_recvs_u125x[] = {
672         { "^\"(\\d\\d.{18}\\d)\"$", recv_stat_u125x },
673         { "^\\*([0-9])$", recv_switch },
674         { "^([-+][0-9]\\.[0-9]{8}E[-+][0-9]{2})$", recv_fetc },
675         { "^\"(VOLT|CURR|RES|CAP|FREQ) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x },
676         { "^\"(VOLT:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x },
677         { "^\"(CURR:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x },
678         { "^\"(CPER:[40]-20mA) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x },
679         { "^\"(T[0-9]:[A-Z]+) ([A-Z]+)\"$", recv_conf_u124x_5x },
680         { "^\"(DIOD)\"$", recv_conf_u124x_5x },
681         ALL_ZERO
682 };
683
684 SR_PRIV const struct agdmm_recv agdmm_recvs_u128x[] = {
685         { "^\"(\\d\\d.{18}\\d)\"$", recv_stat_u128x },
686         { "^\\*([0-9])$", recv_switch },
687         { "^([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))$", recv_fetc },
688         { "^\"(VOLT|CURR|RES|COND|CAP|FREQ|FC1|FC100) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x },
689         { "^\"(VOLT:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x },
690         { "^\"(CURR:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x },
691         { "^\"(FREQ:[ACD]+) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x },
692         { "^\"(CPER:[40]-20mA) ([-+][0-9\\.E\\-+]+),([-+][0-9]\\.[0-9]{8}E([-+][0-9]{2}))\"$", recv_conf_u124x_5x },
693         { "^\"(TEMP:[A-Z]+) ([A-Z]+)\"$", recv_conf_u124x_5x },
694         { "^\"(DIOD|SQU|TEMP)\"$", recv_conf_u124x_5x },
695         ALL_ZERO
696 };