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_ident_send(const struct sr_dev_inst *sdi)
151 return agdmm_send(sdi, "*IDN?");
154 static int agdmm_ident_recv(const struct sr_dev_inst *sdi, GMatchInfo *match)
159 sr_spew("got ident '%s'", g_match_info_get_string(match));
164 static int agdmm_stat_send(const struct sr_dev_inst *sdi)
167 return agdmm_send(sdi, "STAT?");
170 static int agdmm_stat_recv(const struct sr_dev_inst *sdi, GMatchInfo *match)
173 sr_spew("got stat '%s'", g_match_info_get_string(match));
178 SR_PRIV int agdmm_fetc_send(const struct sr_dev_inst *sdi)
181 return agdmm_send(sdi, "FETC?");
184 SR_PRIV int agdmm_fetc_recv(const struct sr_dev_inst *sdi, GMatchInfo *match)
186 struct dev_context *devc;
187 struct sr_datafeed_packet packet;
188 struct sr_datafeed_analog analog;
192 sr_spew("agilent-dmm: FETC reply '%s'", g_match_info_get_string(match));
195 if (devc->cur_mq == -1)
196 /* Haven't seen configuration yet, so can't know what
197 * the fetched float means. Not really an error, we'll
198 * get metadata soon enough. */
201 if (!strcmp(g_match_info_get_string(match), "+9.90000000E+37")) {
202 /* An invalid measurement shows up on the display as "O.L, but
203 * comes through like this. Since comparing 38-digit floats
204 * is rather problematic, we'll cut through this here. */
207 mstr = g_match_info_fetch(match, 1);
208 fvalue = strtof(mstr, &eptr);
210 if (fvalue == 0.0 && eptr == mstr) {
211 sr_err("agilent-dmm: invalid float");
214 if (devc->cur_divider > 0)
215 fvalue /= devc->cur_divider;
218 memset(&analog, 0, sizeof(struct sr_datafeed_analog));
219 analog.mq = devc->cur_mq;
220 analog.unit = devc->cur_mq_unit;
221 analog.mqflags = devc->cur_mq_flags;
222 analog.num_samples = 1;
223 analog.data = &fvalue;
224 packet.type = SR_DF_ANALOG;
225 packet.payload = &analog;
226 sr_session_send(devc->cb_data, &packet);
233 SR_PRIV int agdmm_conf_send(const struct sr_dev_inst *sdi)
236 return agdmm_send(sdi, "CONF?");
239 SR_PRIV int agdmm_conf_recv(const struct sr_dev_inst *sdi, GMatchInfo *match)
241 struct dev_context *devc;
244 sr_spew("got conf '%s'", g_match_info_get_string(match));
246 mstr = g_match_info_fetch(match, 1);
247 if (!strcmp(mstr, "V")) {
248 devc->cur_mq = SR_MQ_VOLTAGE;
249 devc->cur_mq_unit = SR_UNIT_VOLT;
250 devc->cur_mq_flags = 0;
251 devc->cur_divider = 0;
252 } else if(!strcmp(mstr, "MV")) {
253 devc->cur_mq = SR_MQ_VOLTAGE;
254 devc->cur_mq_unit = SR_UNIT_VOLT;
255 devc->cur_mq_flags = 0;
256 devc->cur_divider = 1000;
257 } else if(!strcmp(mstr, "A")) {
258 devc->cur_mq = SR_MQ_CURRENT;
259 devc->cur_mq_unit = SR_UNIT_AMPERE;
260 devc->cur_mq_flags = 0;
261 devc->cur_divider = 0;
262 } else if(!strcmp(mstr, "UA")) {
263 devc->cur_mq = SR_MQ_CURRENT;
264 devc->cur_mq_unit = SR_UNIT_AMPERE;
265 devc->cur_mq_flags = 0;
266 devc->cur_divider = 1000000;
267 } else if(!strcmp(mstr, "FREQ")) {
268 devc->cur_mq = SR_MQ_FREQUENCY;
269 devc->cur_mq_unit = SR_UNIT_HERTZ;
270 devc->cur_mq_flags = 0;
271 devc->cur_divider = 0;
272 } else if(!strcmp(mstr, "RES")) {
273 devc->cur_mq = SR_MQ_RESISTANCE;
274 devc->cur_mq_unit = SR_UNIT_OHM;
275 devc->cur_mq_flags = 0;
276 devc->cur_divider = 0;
277 } else if(!strcmp(mstr, "CAP")) {
278 devc->cur_mq = SR_MQ_CAPACITANCE;
279 devc->cur_mq_unit = SR_UNIT_FARAD;
280 devc->cur_mq_flags = 0;
281 devc->cur_divider = 0;
282 } else if(!strcmp(mstr, "DIOD")) {
283 devc->cur_mq = SR_MQ_VOLTAGE;
284 devc->cur_mq_unit = SR_UNIT_VOLT;
285 devc->cur_mq_flags = SR_MQFLAG_DIODE;
286 devc->cur_divider = 0;
288 sr_dbg("agilent-dmm: unknown first argument");
291 if (g_match_info_get_match_count(match) == 3) {
292 mstr = g_match_info_fetch(match, 1);
293 /* Third value, if present, is always AC or DC. */
294 if (!strcmp(mstr, "AC"))
296 else if (!strcmp(mstr, "DC"))
299 sr_dbg("agilent-dmm: unknown third argument");
306 SR_PRIV int agdmm_switch_recv(const struct sr_dev_inst *sdi, GMatchInfo *match)
311 sr_spew("got switch '%s'", g_match_info_get_string(match));
317 SR_PRIV const struct agdmm_job u123x_jobs[] = {
318 { 1000, agdmm_ident_send },
319 { 143, agdmm_stat_send },
320 { 1000, agdmm_conf_send },
321 { 143, agdmm_fetc_send },
325 SR_PRIV const struct agdmm_recv u123x_recvs[] = {
326 { "^\"(\\d\\d.{18}\\d)\"$", agdmm_stat_recv },
327 { "^\\*([0-9])$", agdmm_switch_recv },
328 { "^([-+][0-9]\\.[0-9]{8}E[-+][0-9]{2})$", agdmm_fetc_recv },
329 { "^\"(V|MV|A|UA|FREQ),(\\d),(AC|DC)\"$", agdmm_conf_recv },
330 { "^\"(RES|CAP),(\\d)\"$", agdmm_conf_recv },
331 { "^\"(DIOD)\"$", agdmm_conf_recv },