]> sigrok.org Git - libsigrok.git/blob - hardware/fluke-dmm/fluke.c
d960228af2d63d11edc3b88269707630cab92155
[libsigrok.git] / hardware / fluke-dmm / fluke.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 <stdlib.h>
21 #include <math.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <glib.h>
25 #include "libsigrok.h"
26 #include "libsigrok-internal.h"
27 #include "fluke-dmm.h"
28
29 static struct sr_datafeed_analog *handle_qm_18x(const struct sr_dev_inst *sdi,
30                 char **tokens)
31 {
32         struct sr_datafeed_analog *analog;
33         float fvalue;
34         char *e, *u;
35         gboolean is_oor;
36
37         if (strcmp(tokens[0], "QM") || !tokens[1])
38                 return NULL;
39
40         if ((e = strstr(tokens[1], "Out of range"))) {
41                 is_oor = TRUE;
42                 fvalue = -1;
43         } else {
44                 is_oor = FALSE;
45                 fvalue = strtof(tokens[1], &e);
46                 if (fvalue == 0.0 && e == tokens[1]) {
47                         /* Happens all the time, when switching modes. */
48                         sr_dbg("Invalid float.");
49                         return NULL;
50                 }
51         }
52         while(*e && *e == ' ')
53                 e++;
54
55         if (!(analog = g_try_malloc0(sizeof(struct sr_datafeed_analog))))
56                 return NULL;
57         if (!(analog->data = g_try_malloc(sizeof(float))))
58                 return NULL;
59         analog->probes = sdi->probes;
60         analog->num_samples = 1;
61         if (is_oor)
62                 *analog->data = NAN;
63         else
64                 *analog->data = fvalue;
65         analog->mq = -1;
66
67         if ((u = strstr(e, "V DC")) || (u = strstr(e, "V AC"))) {
68                 analog->mq = SR_MQ_VOLTAGE;
69                 analog->unit = SR_UNIT_VOLT;
70                 if (!is_oor && e[0] == 'm')
71                         *analog->data /= 1000;
72                 /* This catches "V AC", "V DC" and "V AC+DC". */
73                 if (strstr(u, "AC"))
74                         analog->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
75                 if (strstr(u, "DC"))
76                         analog->mqflags |= SR_MQFLAG_DC;
77         } else if ((u = strstr(e, "dBV")) || (u = strstr(e, "dBm"))) {
78                 analog->mq = SR_MQ_VOLTAGE;
79                 if (u[2] == 'm')
80                         analog->unit = SR_UNIT_DECIBEL_MW;
81                 else
82                         analog->unit = SR_UNIT_DECIBEL_VOLT;
83                 analog->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
84         } else if ((u = strstr(e, "Ohms"))) {
85                 analog->mq = SR_MQ_RESISTANCE;
86                 analog->unit = SR_UNIT_OHM;
87                 if (is_oor)
88                         *analog->data = INFINITY;
89                 else if (e[0] == 'k')
90                         *analog->data *= 1000;
91                 else if (e[0] == 'M')
92                         *analog->data *= 1000000;
93         } else if (!strcmp(e, "nS")) {
94                 analog->mq = SR_MQ_CONDUCTANCE;
95                 analog->unit = SR_UNIT_SIEMENS;
96                 *analog->data /= 1e+9;
97         } else if ((u = strstr(e, "Farads"))) {
98                 analog->mq = SR_MQ_CAPACITANCE;
99                 analog->unit = SR_UNIT_FARAD;
100                 if (!is_oor) {
101                         if (e[0] == 'm')
102                                 *analog->data /= 1e+3;
103                         else if (e[0] == 'u')
104                                 *analog->data /= 1e+6;
105                         else if (e[0] == 'n')
106                                 *analog->data /= 1e+9;
107                 }
108         } else if ((u = strstr(e, "Deg C")) || (u = strstr(e, "Deg F"))) {
109                 analog->mq = SR_MQ_TEMPERATURE;
110                 if (u[4] == 'C')
111                         analog->unit = SR_UNIT_CELSIUS;
112                 else
113                         analog->unit = SR_UNIT_FAHRENHEIT;
114         } else if ((u = strstr(e, "A AC")) || (u = strstr(e, "A DC"))) {
115                 analog->mq = SR_MQ_CURRENT;
116                 analog->unit = SR_UNIT_AMPERE;
117                 /* This catches "A AC", "A DC" and "A AC+DC". */
118                 if (strstr(u, "AC"))
119                         analog->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
120                 if (strstr(u, "DC"))
121                         analog->mqflags |= SR_MQFLAG_DC;
122                 if (!is_oor) {
123                         if (e[0] == 'm')
124                                 *analog->data /= 1e+3;
125                         else if (e[0] == 'u')
126                                 *analog->data /= 1e+6;
127                 }
128         } else if ((u = strstr(e, "Hz"))) {
129                 analog->mq = SR_MQ_FREQUENCY;
130                 analog->unit = SR_UNIT_HERTZ;
131                 if (e[0] == 'k')
132                         *analog->data *= 1e+3;
133         } else if (!strcmp(e, "%")) {
134                 analog->mq = SR_MQ_DUTY_CYCLE;
135                 analog->unit = SR_UNIT_PERCENTAGE;
136         } else if ((u = strstr(e, "ms"))) {
137                 analog->mq = SR_MQ_PULSE_WIDTH;
138                 analog->unit = SR_UNIT_SECOND;
139                 *analog->data /= 1e+3;
140         }
141
142         if (analog->mq == -1) {
143                 /* Not a valid measurement. */
144                 g_free(analog->data);
145                 g_free(analog);
146                 analog = NULL;
147         }
148
149         return analog;
150 }
151
152 static struct sr_datafeed_analog *handle_qm_28x(const struct sr_dev_inst *sdi,
153                 char **tokens)
154 {
155         struct sr_datafeed_analog *analog;
156         float fvalue;
157         char *eptr;
158
159         if (!tokens[1])
160                 return NULL;
161
162         fvalue = strtof(tokens[0], &eptr);
163         if (fvalue == 0.0 && eptr == tokens[0]) {
164                 sr_err("Invalid float.");
165                 return NULL;
166         }
167
168         if (!(analog = g_try_malloc0(sizeof(struct sr_datafeed_analog))))
169                 return NULL;
170         if (!(analog->data = g_try_malloc(sizeof(float))))
171                 return NULL;
172         analog->probes = sdi->probes;
173         analog->num_samples = 1;
174         *analog->data = fvalue;
175         analog->mq = -1;
176
177         if (!strcmp(tokens[1], "VAC") || !strcmp(tokens[1], "VDC")) {
178                 analog->mq = SR_MQ_VOLTAGE;
179                 analog->unit = SR_UNIT_VOLT;
180                 if (!strcmp(tokens[2], "NORMAL")) {
181                         if (tokens[1][1] == 'A') {
182                                 analog->mqflags |= SR_MQFLAG_AC;
183                                 analog->mqflags |= SR_MQFLAG_RMS;
184                         } else
185                                 analog->mqflags |= SR_MQFLAG_DC;
186                 } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
187                         *analog->data = NAN;
188                 } else
189                         analog->mq = -1;
190         } else if (!strcmp(tokens[1], "dBV") || !strcmp(tokens[1], "dBm")) {
191                 analog->mq = SR_MQ_VOLTAGE;
192                 if (tokens[1][2] == 'm')
193                         analog->unit = SR_UNIT_DECIBEL_MW;
194                 else
195                         analog->unit = SR_UNIT_DECIBEL_VOLT;
196                 analog->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
197         } else if (!strcmp(tokens[1], "CEL") || !strcmp(tokens[1], "FAR")) {
198                 if (!strcmp(tokens[2], "NORMAL")) {
199                         analog->mq = SR_MQ_TEMPERATURE;
200                         if (tokens[1][0] == 'C')
201                                 analog->unit = SR_UNIT_CELSIUS;
202                         else
203                                 analog->unit = SR_UNIT_FAHRENHEIT;
204                 }
205         } else if (!strcmp(tokens[1], "OHM")) {
206                 if (!strcmp(tokens[3], "NONE")) {
207                         analog->mq = SR_MQ_RESISTANCE;
208                         analog->unit = SR_UNIT_OHM;
209                         if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
210                                 *analog->data = INFINITY;
211                         } else if (strcmp(tokens[2], "NORMAL"))
212                                 analog->mq = -1;
213                 } else if (!strcmp(tokens[3], "OPEN_CIRCUIT")) {
214                         analog->mq = SR_MQ_CONTINUITY;
215                         analog->unit = SR_UNIT_BOOLEAN;
216                         *analog->data = 0.0;
217                 } else if (!strcmp(tokens[3], "SHORT_CIRCUIT")) {
218                         analog->mq = SR_MQ_CONTINUITY;
219                         analog->unit = SR_UNIT_BOOLEAN;
220                         *analog->data = 1.0;
221                 }
222         } else if (!strcmp(tokens[1], "F")
223                         && !strcmp(tokens[2], "NORMAL")
224                         && !strcmp(tokens[3], "NONE")) {
225                 analog->mq = SR_MQ_CAPACITANCE;
226                 analog->unit = SR_UNIT_FARAD;
227         } else if (!strcmp(tokens[1], "AAC") || !strcmp(tokens[1], "ADC")) {
228                 analog->mq = SR_MQ_CURRENT;
229                 analog->unit = SR_UNIT_AMPERE;
230                 if (!strcmp(tokens[2], "NORMAL")) {
231                         if (tokens[1][1] == 'A') {
232                                 analog->mqflags |= SR_MQFLAG_AC;
233                                 analog->mqflags |= SR_MQFLAG_RMS;
234                         } else
235                                 analog->mqflags |= SR_MQFLAG_DC;
236                 } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
237                         *analog->data = NAN;
238                 } else
239                         analog->mq = -1;
240         } if (!strcmp(tokens[1], "Hz") && !strcmp(tokens[2], "NORMAL")) {
241                 analog->mq = SR_MQ_FREQUENCY;
242                 analog->unit = SR_UNIT_HERTZ;
243         } else if (!strcmp(tokens[1], "PCT") && !strcmp(tokens[2], "NORMAL")) {
244                 analog->mq = SR_MQ_DUTY_CYCLE;
245                 analog->unit = SR_UNIT_PERCENTAGE;
246         } else if (!strcmp(tokens[1], "S") && !strcmp(tokens[2], "NORMAL")) {
247                 analog->mq = SR_MQ_PULSE_WIDTH;
248                 analog->unit = SR_UNIT_SECOND;
249         } else if (!strcmp(tokens[1], "SIE") && !strcmp(tokens[2], "NORMAL")) {
250                 analog->mq = SR_MQ_CONDUCTANCE;
251                 analog->unit = SR_UNIT_SIEMENS;
252         }
253
254         if (analog->mq == -1) {
255                 /* Not a valid measurement. */
256                 g_free(analog->data);
257                 g_free(analog);
258                 analog = NULL;
259         }
260
261         return analog;
262 }
263
264 static void handle_qm_19x_meta(const struct sr_dev_inst *sdi, char **tokens)
265 {
266         struct dev_context *devc;
267         int meas_type, meas_unit, meas_char, i;
268
269         /* Make sure we have 7 valid tokens. */
270         for (i = 0; tokens[i] && i < 7; i++);
271         if (i != 7)
272                 return;
273
274         if (strcmp(tokens[1], "1"))
275                 /* Invalid measurement. */
276                 return;
277
278         if (strcmp(tokens[2], "3"))
279                 /* Only interested in input from the meter mode source. */
280                 return;
281
282         devc = sdi->priv;
283
284         /* Measurement type 11 == absolute, 19 = relative */
285         meas_type = strtol(tokens[0], NULL, 10);
286         if (meas_type != 11 && meas_type != 19)
287                 /* Device is in some mode we don't support. */
288                 return;
289
290         /* We might get metadata for absolute and relative mode (if the device
291          * is in relative mode). In that case, relative takes precedence. */
292         if (meas_type == 11 && devc->meas_type == 19)
293                 return;
294
295         meas_unit = strtol(tokens[3], NULL, 10);
296         if (meas_unit == 0)
297                 /* Device is turned off. Really. */
298                 return;
299         meas_char = strtol(tokens[4], NULL, 10);
300
301         devc->mq = devc->unit = -1;
302         devc->mqflags = 0;
303         switch (meas_unit) {
304         case 1:
305                 devc->mq = SR_MQ_VOLTAGE;
306                 devc->unit = SR_UNIT_VOLT;
307                 if (meas_char == 1)
308                         devc->mqflags |= SR_MQFLAG_DC;
309                 else if (meas_char == 2)
310                         devc->mqflags |= SR_MQFLAG_AC;
311                 else if (meas_char == 3)
312                         devc->mqflags |= SR_MQFLAG_DC | SR_MQFLAG_AC;
313                 else if (meas_char == 15)
314                         devc->mqflags |= SR_MQFLAG_DIODE;
315                 break;
316         case 2:
317                 devc->mq = SR_MQ_CURRENT;
318                 devc->unit = SR_UNIT_AMPERE;
319                 if (meas_char == 1)
320                         devc->mqflags |= SR_MQFLAG_DC;
321                 else if (meas_char == 2)
322                         devc->mqflags |= SR_MQFLAG_AC;
323                 else if (meas_char == 3)
324                         devc->mqflags |= SR_MQFLAG_DC | SR_MQFLAG_AC;
325                 break;
326         case 3:
327                 if (meas_char == 1) {
328                         devc->mq = SR_MQ_RESISTANCE;
329                         devc->unit = SR_UNIT_OHM;
330                 } else if (meas_char == 16) {
331                         devc->mq = SR_MQ_CONTINUITY;
332                         devc->unit = SR_UNIT_BOOLEAN;
333                 }
334                 break;
335         case 12:
336                 devc->mq = SR_MQ_TEMPERATURE;
337                 devc->unit = SR_UNIT_CELSIUS;
338                 break;
339         case 13:
340                 devc->mq = SR_MQ_TEMPERATURE;
341                 devc->unit = SR_UNIT_FAHRENHEIT;
342                 break;
343         default:
344                 sr_dbg("unknown unit: %d", meas_unit);
345         }
346         if (devc->mq == -1 && devc->unit == -1)
347                 return;
348
349         /* If we got here, we know how to interpret the measurement. */
350         devc->meas_type = meas_type;
351         if (meas_type == 11)
352                 /* Absolute meter reading. */
353                 devc->is_relative = FALSE;
354         else if (!strcmp(tokens[0], "19"))
355                 /* Relative meter reading. */
356                 devc->is_relative = TRUE;
357
358 }
359
360 static void handle_qm_19x_data(const struct sr_dev_inst *sdi, char **tokens)
361 {
362         struct dev_context *devc;
363         struct sr_datafeed_packet packet;
364         struct sr_datafeed_analog analog;
365         float fvalue;
366         char *eptr;
367
368         if (!strcmp(tokens[0], "9.9E+37")) {
369                 /* An invalid measurement shows up on the display as "OL", but
370                  * comes through like this. Since comparing 38-digit floats
371                  * is rather problematic, we'll cut through this here. */
372                 fvalue = NAN;
373         } else {
374                 fvalue = strtof(tokens[0], &eptr);
375                 if (fvalue == 0.0 && eptr == tokens[0]) {
376                         sr_err("Invalid float '%s'.", tokens[0]);
377                         return;
378                 }
379         }
380
381         devc = sdi->priv;
382         if (devc->mq == -1 || devc->unit == -1)
383                 /* Don't have valid metadata yet. */
384                 return;
385
386
387         if (devc->mq == SR_MQ_RESISTANCE && isnan(fvalue))
388                 fvalue = INFINITY;
389         else if (devc->mq == SR_MQ_CONTINUITY) {
390                 if (isnan(fvalue))
391                         fvalue = 0.0;
392                 else
393                         fvalue = 1.0;
394         }
395
396         analog.probes = sdi->probes;
397         analog.num_samples = 1;
398         analog.data = &fvalue;
399         analog.mq = devc->mq;
400         analog.unit = devc->unit;
401         analog.mqflags = 0;
402         packet.type = SR_DF_ANALOG;
403         packet.payload = &analog;
404         sr_session_send(devc->cb_data, &packet);
405         devc->num_samples++;
406
407 }
408
409 static void handle_line(const struct sr_dev_inst *sdi)
410 {
411         struct dev_context *devc;
412         struct sr_serial_dev_inst *serial;
413         struct sr_datafeed_packet packet;
414         struct sr_datafeed_analog *analog;
415         int num_tokens, n, i;
416         char cmd[16], **tokens;
417
418         devc = sdi->priv;
419         serial = sdi->conn;
420         sr_spew("Received line '%s' (%d).", devc->buf, devc->buflen);
421
422         if (devc->buflen == 1) {
423                 if (devc->buf[0] != '0') {
424                         /* Not just a CMD_ACK from the query command. */
425                         sr_dbg("Got CMD_ACK '%c'.", devc->buf[0]);
426                         devc->expect_response = FALSE;
427                 }
428                 devc->buflen = 0;
429                 return;
430         }
431
432         analog = NULL;
433         tokens = g_strsplit(devc->buf, ",", 0);
434         if (tokens[0]) {
435                 if (devc->profile->model == FLUKE_187) {
436                         devc->expect_response = FALSE;
437                         analog = handle_qm_18x(sdi, tokens);
438                 } else if (devc->profile->model == FLUKE_287) {
439                         devc->expect_response = FALSE;
440                         analog = handle_qm_28x(sdi, tokens);
441                 } else if (devc->profile->model == FLUKE_190) {
442                         devc->expect_response = FALSE;
443                         for (num_tokens = 0; tokens[num_tokens]; num_tokens++);
444                         if (num_tokens >= 7) {
445                                 /* Response to QM: this is a comma-separated list of
446                                  * fields with metadata about the measurement. This
447                                  * format can return multiple sets of metadata,
448                                  * split into sets of 7 tokens each. */
449                                 devc->meas_type = 0;
450                                 for (i = 0; i < num_tokens; i += 7)
451                                         handle_qm_19x_meta(sdi, tokens + i);
452                                 if (devc->meas_type) {
453                                         /* Slip the request in now, before the main
454                                          * timer loop asks for metadata again. */
455                                         n = sprintf(cmd, "QM %d\r", devc->meas_type);
456                                         if (serial_write(serial, cmd, n) == -1)
457                                                 sr_err("Unable to send QM (measurement): %s.",
458                                                                 strerror(errno));
459                                 }
460                         } else {
461                                 /* Response to QM <n> measurement request. */
462                                 handle_qm_19x_data(sdi, tokens);
463                         }
464                 }
465         }
466         g_strfreev(tokens);
467         devc->buflen = 0;
468
469         if (analog) {
470                 /* Got a measurement. */
471                 packet.type = SR_DF_ANALOG;
472                 packet.payload = analog;
473                 sr_session_send(devc->cb_data, &packet);
474                 devc->num_samples++;
475                 g_free(analog->data);
476                 g_free(analog);
477         }
478
479 }
480
481 SR_PRIV int fluke_receive_data(int fd, int revents, void *cb_data)
482 {
483         struct sr_dev_inst *sdi;
484         struct dev_context *devc;
485         struct sr_serial_dev_inst *serial;
486         int len;
487         int64_t now, elapsed;
488
489         (void)fd;
490
491         if (!(sdi = cb_data))
492                 return TRUE;
493
494         if (!(devc = sdi->priv))
495                 return TRUE;
496
497         serial = sdi->conn;
498         if (revents == G_IO_IN) {
499                 /* Serial data arrived. */
500                 while(FLUKEDMM_BUFSIZE - devc->buflen - 1 > 0) {
501                         len = serial_read(serial, devc->buf + devc->buflen, 1);
502                         if (len < 1)
503                                 break;
504                         devc->buflen++;
505                         *(devc->buf + devc->buflen) = '\0';
506                         if (*(devc->buf + devc->buflen - 1) == '\r') {
507                                 *(devc->buf + --devc->buflen) = '\0';
508                                 handle_line(sdi);
509                                 break;
510                         }
511                 }
512         }
513
514         if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
515                 sdi->driver->dev_acquisition_stop(sdi, cb_data);
516                 return TRUE;
517         }
518
519         now = g_get_monotonic_time() / 1000;
520         elapsed = now - devc->cmd_sent_at;
521         /* Send query command at poll_period interval, or after 1 second
522          * has elapsed. This will make it easier to recover from any
523          * out-of-sync or temporary disconnect issues. */
524         if ((devc->expect_response == FALSE && elapsed > devc->profile->poll_period)
525                         || elapsed > devc->profile->timeout) {
526                 if (serial_write(serial, "QM\r", 3) == -1)
527                         sr_err("Unable to send QM: %s.", strerror(errno));
528                 devc->cmd_sent_at = now;
529                 devc->expect_response = TRUE;
530         }
531
532         return TRUE;
533 }