]> sigrok.org Git - libsigrok.git/blame - hardware/agilent-dmm/sched.c
agilent-dmm: remove IDN check at 1Hz, we just don't need it.
[libsigrok.git] / hardware / agilent-dmm / sched.c
CommitLineData
e93cdf42
BV
1/*
2 * This file is part of the sigrok 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 <glib.h>
21#include "libsigrok.h"
22#include "libsigrok-internal.h"
23#include "config.h"
24#include "agilent-dmm.h"
25#include <stdlib.h>
26#include <string.h>
27#include <errno.h>
28#include <math.h>
29
30
31static void dispatch(const struct sr_dev_inst *sdi)
32{
33 struct dev_context *devc;
34 const struct agdmm_job *jobs;
35 int64_t now;
36 int i;
37
38 devc = sdi->priv;
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;
46 }
47 }
48
49}
50
51static void receive_line(const struct sr_dev_inst *sdi)
52{
53 struct dev_context *devc;
54 const struct agdmm_recv *recvs, *recv;
55 GRegex *reg;
56 GMatchInfo *match;
57 int i;
58
59 devc = sdi->priv;
60
61 /* Strip CRLF */
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';
66 else
67 break;
68 }
69 sr_spew("agilent-dmm: received '%s'", devc->buf);
70
71 recv = NULL;
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)) {
76 recv = &recvs[i];
77 break;
78 }
79 g_match_info_unref(match);
80 g_regex_unref(reg);
81 }
82 if (recv) {
83 recv->recv(sdi, match);
84 g_match_info_unref(match);
85 g_regex_unref(reg);
86 }
87
88 /* Done with this. */
89 devc->buflen = 0;
90
91}
92
93SR_PRIV int agdmm_receive_data(int fd, int revents, void *cb_data)
94{
95 const struct sr_dev_inst *sdi;
96 struct dev_context *devc;
97 int len;
98
99 if (!(sdi = cb_data))
100 return TRUE;
101
102 if (!(devc = sdi->priv))
103 return TRUE;
104
105 if (revents == G_IO_IN) {
106 /* Serial data arrived. */
107 len = AGDMM_BUFSIZE - devc->buflen - 1;
108 if (len > 0) {
109 len = serial_read(fd, devc->buf + devc->buflen, len);
110 if (len > 0) {
111 devc->buflen += len;
112 *(devc->buf + devc->buflen) = '\0';
113 if (devc->buflen > 0 && *(devc->buf + devc->buflen - 1) == '\n')
114 /* End of line */
115 receive_line(sdi);
116 }
117 }
118 }
119
120 dispatch(sdi);
121
122 if (devc->num_samples >= devc->limit_samples)
123 sdi->driver->dev_acquisition_stop(sdi, cb_data);
124
125 return TRUE;
126}
127
128static int agdmm_send(const struct sr_dev_inst *sdi, const char *cmd)
129{
130 struct dev_context *devc;
131 char buf[32];
132
133 devc = sdi->priv;
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);
138 else
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));
142 return SR_ERR;
143 }
144
145 return SR_OK;
146}
147
e93cdf42
BV
148static int agdmm_stat_send(const struct sr_dev_inst *sdi)
149{
150
151 return agdmm_send(sdi, "STAT?");
152}
153
154static int agdmm_stat_recv(const struct sr_dev_inst *sdi, GMatchInfo *match)
155{
e6b021f3
BV
156 struct dev_context *devc;
157 char *s;
158
159 devc = sdi->priv;
160 s = g_match_info_fetch(match, 1);
81599cc5 161 sr_spew("agilent-dmm: STAT response '%s'", s);
e6b021f3
BV
162
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. */
165 if (s[0] == '1')
166 devc->cur_mqflags |= SR_MQFLAG_MAX | SR_MQFLAG_MIN;
167 else
168 devc->cur_mqflags &= ~(SR_MQFLAG_MAX | SR_MQFLAG_MIN);
e93cdf42 169
e6b021f3
BV
170 if (s[1] == '1')
171 devc->cur_mqflags |= SR_MQFLAG_RELATIVE;
172 else
173 devc->cur_mqflags &= ~SR_MQFLAG_RELATIVE;
174
175 /* Triggered or auto hold modes. */
176 if (s[2] == '1' || s[3] == '1')
177 devc->cur_mqflags |= SR_MQFLAG_HOLD;
178 else
179 devc->cur_mqflags &= ~SR_MQFLAG_HOLD;
180
181 /* Temp/aux mode. */
182 if (s[7] == '1')
183 devc->mode_tempaux = TRUE;
184 else
185 devc->mode_tempaux = FALSE;
186
187 /* Continuity mode. */
188 if (s[16] == '1')
189 devc->mode_continuity = TRUE;
190 else
191 devc->mode_continuity = FALSE;
192
193 g_free(s);
e93cdf42
BV
194
195 return SR_OK;
196}
197
198SR_PRIV int agdmm_fetc_send(const struct sr_dev_inst *sdi)
199{
200
201 return agdmm_send(sdi, "FETC?");
202}
203
204SR_PRIV int agdmm_fetc_recv(const struct sr_dev_inst *sdi, GMatchInfo *match)
205{
206 struct dev_context *devc;
207 struct sr_datafeed_packet packet;
208 struct sr_datafeed_analog analog;
209 float fvalue;
210 char *mstr, *eptr;
211
212 sr_spew("agilent-dmm: FETC reply '%s'", g_match_info_get_string(match));
213 devc = sdi->priv;
214
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. */
219 return SR_OK;
220
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. */
225 fvalue = NAN;
226 } else {
227 mstr = g_match_info_fetch(match, 1);
228 fvalue = strtof(mstr, &eptr);
229 g_free(mstr);
230 if (fvalue == 0.0 && eptr == mstr) {
231 sr_err("agilent-dmm: invalid float");
232 return SR_ERR;
233 }
234 if (devc->cur_divider > 0)
235 fvalue /= devc->cur_divider;
236 }
237
238 memset(&analog, 0, sizeof(struct sr_datafeed_analog));
239 analog.mq = devc->cur_mq;
e6b021f3
BV
240 analog.unit = devc->cur_unit;
241 analog.mqflags = devc->cur_mqflags;
e93cdf42
BV
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);
247
248 devc->num_samples++;
249
250 return SR_OK;
251}
252
253SR_PRIV int agdmm_conf_send(const struct sr_dev_inst *sdi)
254{
255
256 return agdmm_send(sdi, "CONF?");
257}
258
259SR_PRIV int agdmm_conf_recv(const struct sr_dev_inst *sdi, GMatchInfo *match)
260{
261 struct dev_context *devc;
262 char *mstr;
263
81599cc5 264 sr_spew("agilent-dmm: CONF? response '%s'", g_match_info_get_string(match));
e93cdf42
BV
265 devc = sdi->priv;
266 mstr = g_match_info_fetch(match, 1);
267 if (!strcmp(mstr, "V")) {
268 devc->cur_mq = SR_MQ_VOLTAGE;
e6b021f3
BV
269 devc->cur_unit = SR_UNIT_VOLT;
270 devc->cur_mqflags = 0;
e93cdf42
BV
271 devc->cur_divider = 0;
272 } else if(!strcmp(mstr, "MV")) {
e6b021f3
BV
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;
280 } else {
281 devc->cur_mq = SR_MQ_VOLTAGE;
282 devc->cur_unit = SR_UNIT_VOLT;
283 devc->cur_mqflags = 0;
284 devc->cur_divider = 1000;
285 }
e93cdf42
BV
286 } else if(!strcmp(mstr, "A")) {
287 devc->cur_mq = SR_MQ_CURRENT;
e6b021f3
BV
288 devc->cur_unit = SR_UNIT_AMPERE;
289 devc->cur_mqflags = 0;
e93cdf42
BV
290 devc->cur_divider = 0;
291 } else if(!strcmp(mstr, "UA")) {
292 devc->cur_mq = SR_MQ_CURRENT;
e6b021f3
BV
293 devc->cur_unit = SR_UNIT_AMPERE;
294 devc->cur_mqflags = 0;
e93cdf42
BV
295 devc->cur_divider = 1000000;
296 } else if(!strcmp(mstr, "FREQ")) {
297 devc->cur_mq = SR_MQ_FREQUENCY;
e6b021f3
BV
298 devc->cur_unit = SR_UNIT_HERTZ;
299 devc->cur_mqflags = 0;
e93cdf42
BV
300 devc->cur_divider = 0;
301 } else if(!strcmp(mstr, "RES")) {
e6b021f3
BV
302 if (devc->mode_continuity) {
303 devc->cur_mq = SR_MQ_CONTINUITY;
304 devc->cur_unit = SR_UNIT_BOOLEAN;
305 } else {
306 devc->cur_mq = SR_MQ_RESISTANCE;
307 devc->cur_unit = SR_UNIT_OHM;
308 }
309 devc->cur_mqflags = 0;
e93cdf42
BV
310 devc->cur_divider = 0;
311 } else if(!strcmp(mstr, "CAP")) {
312 devc->cur_mq = SR_MQ_CAPACITANCE;
e6b021f3
BV
313 devc->cur_unit = SR_UNIT_FARAD;
314 devc->cur_mqflags = 0;
e93cdf42
BV
315 devc->cur_divider = 0;
316 } else if(!strcmp(mstr, "DIOD")) {
317 devc->cur_mq = SR_MQ_VOLTAGE;
e6b021f3
BV
318 devc->cur_unit = SR_UNIT_VOLT;
319 devc->cur_mqflags = SR_MQFLAG_DIODE;
e93cdf42
BV
320 devc->cur_divider = 0;
321 } else
322 sr_dbg("agilent-dmm: unknown first argument");
323 g_free(mstr);
324
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"))
e6b021f3 329 devc->cur_mqflags |= SR_MQFLAG_AC;
e93cdf42 330 else if (!strcmp(mstr, "DC"))
e6b021f3 331 devc->cur_mqflags |= SR_MQFLAG_DC;
e93cdf42
BV
332 else
333 sr_dbg("agilent-dmm: unknown third argument");
334 g_free(mstr);
e6b021f3
BV
335 } else
336 devc->cur_mqflags &= ~(SR_MQFLAG_AC | SR_MQFLAG_DC);
e93cdf42
BV
337
338 return SR_OK;
339}
340
81599cc5
BV
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. */
e93cdf42
BV
345SR_PRIV int agdmm_switch_recv(const struct sr_dev_inst *sdi, GMatchInfo *match)
346{
347
348 (void)sdi;
349
81599cc5 350 sr_spew("agilent-dmm: switch '%s'", g_match_info_get_string(match));
e93cdf42
BV
351
352 return SR_OK;
353}
354
355
356SR_PRIV const struct agdmm_job u123x_jobs[] = {
e93cdf42
BV
357 { 143, agdmm_stat_send },
358 { 1000, agdmm_conf_send },
359 { 143, agdmm_fetc_send },
360 { 0, NULL }
361};
362
363SR_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 },
370 { NULL, NULL }
371};
372
373