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