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