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