2 * This file is part of the sigrok 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"
24 #include "agilent-dmm.h"
31 static void dispatch(const struct sr_dev_inst *sdi)
33 struct dev_context *devc;
34 const struct agdmm_job *jobs;
39 jobs = devc->profile->jobs;
40 now = g_get_monotonic_time() / 1000;
41 for (i = 0; (&jobs[i])->interval; i++) {
42 if (now - devc->jobqueue[i] > (&jobs[i])->interval) {
43 sr_spew("agilent-dmm: running job %d", i);
44 (&jobs[i])->send(sdi);
45 devc->jobqueue[i] = now;
51 static void receive_line(const struct sr_dev_inst *sdi)
53 struct dev_context *devc;
54 const struct agdmm_recv *recvs, *recv;
62 while (devc->buflen) {
63 if (*(devc->buf + devc->buflen - 1) == '\r'
64 || *(devc->buf + devc->buflen - 1) == '\n')
65 *(devc->buf + --devc->buflen) = '\0';
69 sr_spew("agilent-dmm: received '%s'", devc->buf);
72 recvs = devc->profile->recvs;
73 for (i = 0; (&recvs[i])->recv_regex; i++) {
74 reg = g_regex_new((&recvs[i])->recv_regex, 0, 0, NULL);
75 if (g_regex_match(reg, (char *)devc->buf, 0, &match)) {
79 g_match_info_unref(match);
83 recv->recv(sdi, match);
84 g_match_info_unref(match);
93 SR_PRIV int agdmm_receive_data(int fd, int revents, void *cb_data)
95 const struct sr_dev_inst *sdi;
96 struct dev_context *devc;
102 if (!(devc = sdi->priv))
105 if (revents == G_IO_IN) {
106 /* Serial data arrived. */
107 len = AGDMM_BUFSIZE - devc->buflen - 1;
109 len = serial_read(fd, devc->buf + devc->buflen, len);
112 *(devc->buf + devc->buflen) = '\0';
113 if (devc->buflen > 0 && *(devc->buf + devc->buflen - 1) == '\n')
122 if (devc->num_samples >= devc->limit_samples)
123 sdi->driver->dev_acquisition_stop(sdi, cb_data);
128 static int agdmm_send(const struct sr_dev_inst *sdi, const char *cmd)
130 struct dev_context *devc;
134 sr_spew("agilent-dmm: sending '%s'", cmd);
135 strncpy(buf, cmd, 28);
136 if (!strncmp(buf, "*IDN?", 5))
137 strncat(buf, "\r\n", 32);
139 strncat(buf, "\n\r\n", 32);
140 if (serial_write(devc->serial->fd, buf, strlen(buf)) == -1) {
141 sr_err("agilent-dmm: failed to send: %s", strerror(errno));
148 static int agdmm_stat_send(const struct sr_dev_inst *sdi)
151 return agdmm_send(sdi, "STAT?");
154 static int agdmm_stat_recv(const struct sr_dev_inst *sdi, GMatchInfo *match)
156 struct dev_context *devc;
160 s = g_match_info_fetch(match, 1);
161 sr_spew("agilent-dmm: STAT response '%s'", s);
163 /* Max, Min or Avg mode -- no way to tell which, so we'll
164 * set both flags to denote it's not a normal measurement. */
166 devc->cur_mqflags |= SR_MQFLAG_MAX | SR_MQFLAG_MIN;
168 devc->cur_mqflags &= ~(SR_MQFLAG_MAX | SR_MQFLAG_MIN);
171 devc->cur_mqflags |= SR_MQFLAG_RELATIVE;
173 devc->cur_mqflags &= ~SR_MQFLAG_RELATIVE;
175 /* Triggered or auto hold modes. */
176 if (s[2] == '1' || s[3] == '1')
177 devc->cur_mqflags |= SR_MQFLAG_HOLD;
179 devc->cur_mqflags &= ~SR_MQFLAG_HOLD;
183 devc->mode_tempaux = TRUE;
185 devc->mode_tempaux = FALSE;
187 /* Continuity mode. */
189 devc->mode_continuity = TRUE;
191 devc->mode_continuity = FALSE;
198 SR_PRIV int agdmm_fetc_send(const struct sr_dev_inst *sdi)
201 return agdmm_send(sdi, "FETC?");
204 SR_PRIV int agdmm_fetc_recv(const struct sr_dev_inst *sdi, GMatchInfo *match)
206 struct dev_context *devc;
207 struct sr_datafeed_packet packet;
208 struct sr_datafeed_analog analog;
212 sr_spew("agilent-dmm: FETC reply '%s'", g_match_info_get_string(match));
215 if (devc->cur_mq == -1)
216 /* Haven't seen configuration yet, so can't know what
217 * the fetched float means. Not really an error, we'll
218 * get metadata soon enough. */
221 if (!strcmp(g_match_info_get_string(match), "+9.90000000E+37")) {
222 /* An invalid measurement shows up on the display as "O.L, but
223 * comes through like this. Since comparing 38-digit floats
224 * is rather problematic, we'll cut through this here. */
227 mstr = g_match_info_fetch(match, 1);
228 fvalue = strtof(mstr, &eptr);
230 if (fvalue == 0.0 && eptr == mstr) {
231 sr_err("agilent-dmm: invalid float");
234 if (devc->cur_divider > 0)
235 fvalue /= devc->cur_divider;
238 memset(&analog, 0, sizeof(struct sr_datafeed_analog));
239 analog.mq = devc->cur_mq;
240 analog.unit = devc->cur_unit;
241 analog.mqflags = devc->cur_mqflags;
242 analog.num_samples = 1;
243 analog.data = &fvalue;
244 packet.type = SR_DF_ANALOG;
245 packet.payload = &analog;
246 sr_session_send(devc->cb_data, &packet);
253 SR_PRIV int agdmm_conf_send(const struct sr_dev_inst *sdi)
256 return agdmm_send(sdi, "CONF?");
259 SR_PRIV int agdmm_conf_recv(const struct sr_dev_inst *sdi, GMatchInfo *match)
261 struct dev_context *devc;
264 sr_spew("agilent-dmm: CONF? response '%s'", g_match_info_get_string(match));
266 mstr = g_match_info_fetch(match, 1);
267 if (!strcmp(mstr, "V")) {
268 devc->cur_mq = SR_MQ_VOLTAGE;
269 devc->cur_unit = SR_UNIT_VOLT;
270 devc->cur_mqflags = 0;
271 devc->cur_divider = 0;
272 } else if(!strcmp(mstr, "MV")) {
273 if (devc->mode_tempaux) {
274 devc->cur_mq = SR_MQ_TEMPERATURE;
275 /* No way to detect whether Fahrenheit or Celcius
276 * is used, so we'll just default to Celcius. */
277 devc->cur_unit = SR_UNIT_CELSIUS;
278 devc->cur_mqflags = 0;
279 devc->cur_divider = 0;
281 devc->cur_mq = SR_MQ_VOLTAGE;
282 devc->cur_unit = SR_UNIT_VOLT;
283 devc->cur_mqflags = 0;
284 devc->cur_divider = 1000;
286 } else if(!strcmp(mstr, "A")) {
287 devc->cur_mq = SR_MQ_CURRENT;
288 devc->cur_unit = SR_UNIT_AMPERE;
289 devc->cur_mqflags = 0;
290 devc->cur_divider = 0;
291 } else if(!strcmp(mstr, "UA")) {
292 devc->cur_mq = SR_MQ_CURRENT;
293 devc->cur_unit = SR_UNIT_AMPERE;
294 devc->cur_mqflags = 0;
295 devc->cur_divider = 1000000;
296 } else if(!strcmp(mstr, "FREQ")) {
297 devc->cur_mq = SR_MQ_FREQUENCY;
298 devc->cur_unit = SR_UNIT_HERTZ;
299 devc->cur_mqflags = 0;
300 devc->cur_divider = 0;
301 } else if(!strcmp(mstr, "RES")) {
302 if (devc->mode_continuity) {
303 devc->cur_mq = SR_MQ_CONTINUITY;
304 devc->cur_unit = SR_UNIT_BOOLEAN;
306 devc->cur_mq = SR_MQ_RESISTANCE;
307 devc->cur_unit = SR_UNIT_OHM;
309 devc->cur_mqflags = 0;
310 devc->cur_divider = 0;
311 } else if(!strcmp(mstr, "CAP")) {
312 devc->cur_mq = SR_MQ_CAPACITANCE;
313 devc->cur_unit = SR_UNIT_FARAD;
314 devc->cur_mqflags = 0;
315 devc->cur_divider = 0;
316 } else if(!strcmp(mstr, "DIOD")) {
317 devc->cur_mq = SR_MQ_VOLTAGE;
318 devc->cur_unit = SR_UNIT_VOLT;
319 devc->cur_mqflags = SR_MQFLAG_DIODE;
320 devc->cur_divider = 0;
322 sr_dbg("agilent-dmm: unknown first argument");
325 if (g_match_info_get_match_count(match) == 3) {
326 mstr = g_match_info_fetch(match, 1);
327 /* Third value, if present, is always AC or DC. */
328 if (!strcmp(mstr, "AC"))
329 devc->cur_mqflags |= SR_MQFLAG_AC;
330 else if (!strcmp(mstr, "DC"))
331 devc->cur_mqflags |= SR_MQFLAG_DC;
333 sr_dbg("agilent-dmm: unknown third argument");
336 devc->cur_mqflags &= ~(SR_MQFLAG_AC | SR_MQFLAG_DC);
341 /* This comes in whenever the rotary switch is changed to a new position.
342 * We could use it to determine the major measurement mode, but we already
343 * have the output of CONF? for that, which is more detailed. However
344 * we do need to catch this here, or it'll show up in some other output. */
345 SR_PRIV int agdmm_switch_recv(const struct sr_dev_inst *sdi, GMatchInfo *match)
350 sr_spew("agilent-dmm: switch '%s'", g_match_info_get_string(match));
356 SR_PRIV const struct agdmm_job u123x_jobs[] = {
357 { 143, agdmm_stat_send },
358 { 1000, agdmm_conf_send },
359 { 143, agdmm_fetc_send },
363 SR_PRIV const struct agdmm_recv u123x_recvs[] = {
364 { "^\"(\\d\\d.{18}\\d)\"$", agdmm_stat_recv },
365 { "^\\*([0-9])$", agdmm_switch_recv },
366 { "^([-+][0-9]\\.[0-9]{8}E[-+][0-9]{2})$", agdmm_fetc_recv },
367 { "^\"(V|MV|A|UA|FREQ),(\\d),(AC|DC)\"$", agdmm_conf_recv },
368 { "^\"(RES|CAP),(\\d)\"$", agdmm_conf_recv },
369 { "^\"(DIOD)\"$", agdmm_conf_recv },