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