]> sigrok.org Git - libsigrok.git/blob - hardware/fluke-dmm/fluke.c
Fluke 287/289: support for dBu/dBV measurement units
[libsigrok.git] / hardware / fluke-dmm / fluke.c
1 /*
2  * This file is part of the sigrok 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 <glib.h>
21 #include "libsigrok.h"
22 #include "libsigrok-internal.h"
23 #include "fluke-dmm.h"
24 #include <stdlib.h>
25 #include <math.h>
26 #include <string.h>
27 #include <errno.h>
28
29
30 static struct sr_datafeed_analog *handle_qm_v1(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         (void)sdi;
39         if (strcmp(tokens[0], "QM"))
40                 return NULL;
41
42         if ((e = strstr(tokens[1], "Out of range"))) {
43                 is_oor = TRUE;
44                 fvalue = -1;
45         } else {
46                 is_oor = FALSE;
47                 fvalue = strtof(tokens[1], &e);
48                 if (fvalue == 0.0 && e == tokens[1]) {
49                         /* Happens all the time, when switching modes. */
50                         sr_dbg("fluke-dmm: invalid float");
51                         return NULL;
52                 }
53         }
54         while(*e && *e == ' ')
55                 e++;
56
57         analog = g_try_malloc0(sizeof(struct sr_datafeed_analog));
58         analog->num_samples = 1;
59         analog->data = g_try_malloc(sizeof(float));
60         if (is_oor)
61                 *analog->data = NAN;
62         else
63                 *analog->data = fvalue;
64         analog->mq = -1;
65
66         if ((u = strstr(e, "V DC")) || (u = strstr(e, "V AC"))) {
67                 analog->mq = SR_MQ_VOLTAGE;
68                 analog->unit = SR_UNIT_VOLT;
69                 if (!is_oor && e[0] == 'm')
70                         *analog->data /= 1000;
71                 /* This catches "V AC", "V DC" and "V AC+DC". */
72                 if (strstr(u, "AC"))
73                         analog->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
74                 if (strstr(u, "DC"))
75                         analog->mqflags |= SR_MQFLAG_DC;
76         } else if ((u = strstr(e, "dBV")) || (u = strstr(e, "dBm"))) {
77                 analog->mq = SR_MQ_VOLTAGE;
78                 if (u[2] == 'm')
79                         analog->unit = SR_UNIT_DECIBEL_MW;
80                 else
81                         analog->unit = SR_UNIT_DECIBEL_VOLT;
82                 analog->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
83         } else if ((u = strstr(e, "Ohms"))) {
84                 analog->mq = SR_MQ_RESISTANCE;
85                 analog->unit = SR_UNIT_OHM;
86                 if (is_oor)
87                         *analog->data = INFINITY;
88                 else if (e[0] == 'k')
89                         *analog->data *= 1000;
90                 else if (e[0] == 'M')
91                         *analog->data *= 1000000;
92         } else if (!strcmp(e, "nS")) {
93                 analog->mq = SR_MQ_CONDUCTANCE;
94                 analog->unit = SR_UNIT_SIEMENS;
95                 *analog->data /= 1e+9;
96         } else if ((u = strstr(e, "Farads"))) {
97                 analog->mq = SR_MQ_CAPACITANCE;
98                 analog->unit = SR_UNIT_FARAD;
99                 if (!is_oor) {
100                         if (e[0] == 'm')
101                                 *analog->data /= 1e+3;
102                         else if (e[0] == 'u')
103                                 *analog->data /= 1e+6;
104                         else if (e[0] == 'n')
105                                 *analog->data /= 1e+9;
106                 }
107         } else if ((u = strstr(e, "Deg C")) || (u = strstr(e, "Deg F"))) {
108                 analog->mq = SR_MQ_TEMPERATURE;
109                 if (u[4] == 'C')
110                         analog->unit = SR_UNIT_CELSIUS;
111                 else
112                         analog->unit = SR_UNIT_FAHRENHEIT;
113         } else if ((u = strstr(e, "A AC")) || (u = strstr(e, "A DC"))) {
114                 analog->mq = SR_MQ_CURRENT;
115                 analog->unit = SR_UNIT_AMPERE;
116                 /* This catches "A AC", "A DC" and "A AC+DC". */
117                 if (strstr(u, "AC"))
118                         analog->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
119                 if (strstr(u, "DC"))
120                         analog->mqflags |= SR_MQFLAG_DC;
121                 if (!is_oor) {
122                         if (e[0] == 'm')
123                                 *analog->data /= 1e+3;
124                         else if (e[0] == 'u')
125                                 *analog->data /= 1e+6;
126                 }
127         } else if ((u = strstr(e, "Hz"))) {
128                 analog->mq = SR_MQ_FREQUENCY;
129                 analog->unit = SR_UNIT_HERTZ;
130                 if (e[0] == 'k')
131                         *analog->data *= 1e+3;
132         } else if (!strcmp(e, "%")) {
133                 analog->mq = SR_MQ_DUTY_CYCLE;
134                 analog->unit = SR_UNIT_PERCENTAGE;
135         } else if ((u = strstr(e, "ms"))) {
136                 analog->mq = SR_MQ_PULSE_WIDTH;
137                 analog->unit = SR_UNIT_SECOND;
138                 *analog->data /= 1e+3;
139         }
140
141         if (analog->mq == -1) {
142                 /* Not a valid measurement. */
143                 g_free(analog->data);
144                 g_free(analog);
145                 analog = NULL;
146         }
147
148         return analog;
149 }
150
151 static struct sr_datafeed_analog *handle_qm_v2(const struct sr_dev_inst *sdi,
152                 char **tokens)
153 {
154         struct sr_datafeed_analog *analog;
155         float fvalue;
156         char *eptr;
157
158         (void)sdi;
159         fvalue = strtof(tokens[0], &eptr);
160         if (fvalue == 0.0 && eptr == tokens[0]) {
161                 sr_err("fluke-dmm: invalid float");
162                 return NULL;
163         }
164
165         analog = g_try_malloc0(sizeof(struct sr_datafeed_analog));
166         analog->num_samples = 1;
167         analog->data = g_try_malloc(sizeof(float));
168         *analog->data = fvalue;
169         analog->mq = -1;
170
171         if (!strcmp(tokens[1], "VAC") || !strcmp(tokens[1], "VDC")) {
172                 analog->mq = SR_MQ_VOLTAGE;
173                 analog->unit = SR_UNIT_VOLT;
174                 if (!strcmp(tokens[2], "NORMAL")) {
175                         if (tokens[1][1] == 'A') {
176                                 analog->mqflags |= SR_MQFLAG_AC;
177                                 analog->mqflags |= SR_MQFLAG_RMS;
178                         } else
179                                 analog->mqflags |= SR_MQFLAG_DC;
180                 } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
181                         *analog->data = NAN;
182                 } else
183                         analog->mq = -1;
184         } if (!strcmp(tokens[1], "dBV") || !strcmp(tokens[1], "dBm")) {
185                 analog->mq = SR_MQ_VOLTAGE;
186                 if (tokens[1][2] == 'm')
187                         analog->unit = SR_UNIT_DECIBEL_MW;
188                 else
189                         analog->unit = SR_UNIT_DECIBEL_VOLT;
190                 analog->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
191         } if (!strcmp(tokens[1], "CEL") || !strcmp(tokens[1], "FAR")) {
192                 if (!strcmp(tokens[2], "NORMAL")) {
193                         analog->mq = SR_MQ_TEMPERATURE;
194                         if (tokens[1][0] == 'C')
195                                 analog->unit = SR_UNIT_CELSIUS;
196                         else
197                                 analog->unit = SR_UNIT_FAHRENHEIT;
198                 }
199         } if (!strcmp(tokens[1], "OHM")) {
200                 if (!strcmp(tokens[3], "NONE")) {
201                         analog->mq = SR_MQ_RESISTANCE;
202                         analog->unit = SR_UNIT_OHM;
203                         if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
204                                 *analog->data = INFINITY;
205                         } else if (strcmp(tokens[2], "NORMAL"))
206                                 analog->mq = -1;
207                 } else if (!strcmp(tokens[3], "OPEN_CIRCUIT")) {
208                         analog->mq = SR_MQ_CONTINUITY;
209                         analog->unit = SR_UNIT_BOOLEAN;
210                         *analog->data = 0.0;
211                 } else if (!strcmp(tokens[3], "SHORT_CIRCUIT")) {
212                         analog->mq = SR_MQ_CONTINUITY;
213                         analog->unit = SR_UNIT_BOOLEAN;
214                         *analog->data = 1.0;
215                 }
216         } if (!strcmp(tokens[1], "F")
217                         && !strcmp(tokens[2], "NORMAL")
218                         && !strcmp(tokens[3], "NONE")) {
219                 analog->mq = SR_MQ_CAPACITANCE;
220                 analog->unit = SR_UNIT_FARAD;
221         } else if (!strcmp(tokens[1], "AAC") || !strcmp(tokens[1], "ADC")) {
222                 analog->mq = SR_MQ_CURRENT;
223                 analog->unit = SR_UNIT_AMPERE;
224                 if (!strcmp(tokens[2], "NORMAL")) {
225                         if (tokens[1][1] == 'A') {
226                                 analog->mqflags |= SR_MQFLAG_AC;
227                                 analog->mqflags |= SR_MQFLAG_RMS;
228                         } else
229                                 analog->mqflags |= SR_MQFLAG_DC;
230                 } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
231                         *analog->data = NAN;
232                 } else
233                         analog->mq = -1;
234         } if (!strcmp(tokens[1], "Hz") && !strcmp(tokens[2], "NORMAL")) {
235                 analog->mq = SR_MQ_FREQUENCY;
236                 analog->unit = SR_UNIT_HERTZ;
237         } if (!strcmp(tokens[1], "PCT") && !strcmp(tokens[2], "NORMAL")) {
238                 analog->mq = SR_MQ_DUTY_CYCLE;
239                 analog->unit = SR_UNIT_PERCENTAGE;
240         } if (!strcmp(tokens[1], "S") && !strcmp(tokens[2], "NORMAL")) {
241                 analog->mq = SR_MQ_PULSE_WIDTH;
242                 analog->unit = SR_UNIT_SECOND;
243         } if (!strcmp(tokens[1], "SIE") && !strcmp(tokens[2], "NORMAL")) {
244                 analog->mq = SR_MQ_CONDUCTANCE;
245                 analog->unit = SR_UNIT_SIEMENS;
246         }
247
248         if (analog->mq == -1) {
249                 /* Not a valid measurement. */
250                 g_free(analog->data);
251                 g_free(analog);
252                 analog = NULL;
253         }
254
255         return analog;
256 }
257
258 static void handle_line(const struct sr_dev_inst *sdi)
259 {
260         struct dev_context *devc;
261         struct sr_datafeed_packet packet;
262         struct sr_datafeed_analog *analog;
263         char **tokens;
264
265         devc = sdi->priv;
266         sr_spew("fluke-dmm: received line '%s' (%d)", devc->buf, devc->buflen);
267
268         if (devc->buflen == 1) {
269                 if (devc->buf[0] != '0') {
270                         /* Not just a CMD_ACK from the query command. */
271                         sr_dbg("fluke-dmm: got CMD_ACK '%c'", devc->buf[0]);
272                         devc->expect_response = FALSE;
273                 }
274                 devc->buflen = 0;
275                 return;
276         }
277
278         analog = NULL;
279         tokens = g_strsplit(devc->buf, ",", 0);
280         if (tokens[0] && tokens[1]) {
281                 if (devc->profile->model == FLUKE_187) {
282                         devc->expect_response = FALSE;
283                         analog = handle_qm_v1(sdi, tokens);
284                 } else if (devc->profile->model == FLUKE_287) {
285                         devc->expect_response = FALSE;
286                         analog = handle_qm_v2(sdi, tokens);
287                 }
288         }
289         g_strfreev(tokens);
290         devc->buflen = 0;
291
292         if (analog) {
293                 /* Got a measurement. */
294                 packet.type = SR_DF_ANALOG;
295                 packet.payload = analog;
296                 sr_session_send(devc->cb_data, &packet);
297                 devc->num_samples++;
298                 g_free(analog->data);
299                 g_free(analog);
300         }
301
302 }
303
304 SR_PRIV int fluke_receive_data(int fd, int revents, void *cb_data)
305 {
306         const struct sr_dev_inst *sdi;
307         struct dev_context *devc;
308         int len;
309         int64_t now, elapsed;
310
311         if (!(sdi = cb_data))
312                 return TRUE;
313
314         if (!(devc = sdi->priv))
315                 return TRUE;
316
317         if (revents == G_IO_IN) {
318                 /* Serial data arrived. */
319                 while(FLUKEDMM_BUFSIZE - devc->buflen - 1 > 0) {
320                         len = serial_read(fd, devc->buf + devc->buflen, 1);
321                         if (len < 1)
322                                 break;
323                         devc->buflen++;
324                         *(devc->buf + devc->buflen) = '\0';
325                         if (*(devc->buf + devc->buflen - 1) == '\r') {
326                                 *(devc->buf + --devc->buflen) = '\0';
327                                 handle_line(sdi);
328                                 break;
329                         }
330                 }
331         }
332
333         if (devc->num_samples >= devc->limit_samples) {
334                 sdi->driver->dev_acquisition_stop(sdi, cb_data);
335                 return TRUE;
336         }
337
338         now = g_get_monotonic_time() / 1000;
339         elapsed = now - devc->cmd_sent_at;
340         /* Send query command at poll_period interval, or after 1 second
341          * has elapsed. This will make it recover from any out-of-sync
342          * or temporary disconnect issues. */
343         if ((devc->expect_response == FALSE && elapsed > devc->profile->poll_period)
344                         || elapsed > 1000) {
345                 sr_spew("fluke-dmm: sending QM");
346                 if (serial_write(devc->serial->fd, "QM\r", 3) == -1)
347                         sr_err("fluke-dmm: unable to send QM: %s", strerror(errno));
348                 devc->cmd_sent_at = now;
349                 devc->expect_response = TRUE;
350         }
351
352         return TRUE;
353 }
354
355