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