]> sigrok.org Git - libsigrok.git/blame - hardware/fluke-dmm/fluke.c
Initial support for UNI-T DMMs.
[libsigrok.git] / hardware / fluke-dmm / fluke.c
CommitLineData
d38d2ef0
BV
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"
d38d2ef0
BV
23#include "fluke-dmm.h"
24#include <stdlib.h>
25#include <math.h>
26#include <string.h>
27#include <errno.h>
28
29
fe31f8b9
BV
30static 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
d38d2ef0
BV
151static 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], "CEL") || !strcmp(tokens[1], "FAR")) {
185 if (!strcmp(tokens[2], "NORMAL")) {
186 analog->mq = SR_MQ_TEMPERATURE;
187 if (tokens[1][0] == 'C')
fe31f8b9 188 analog->unit = SR_UNIT_CELSIUS;
d38d2ef0
BV
189 else
190 analog->unit = SR_UNIT_FAHRENHEIT;
191 }
192 } if (!strcmp(tokens[1], "OHM")) {
193 if (!strcmp(tokens[3], "NONE")) {
194 analog->mq = SR_MQ_RESISTANCE;
195 analog->unit = SR_UNIT_OHM;
196 if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
197 *analog->data = INFINITY;
198 } else if (strcmp(tokens[2], "NORMAL"))
199 analog->mq = -1;
200 } else if (!strcmp(tokens[3], "OPEN_CIRCUIT")) {
201 analog->mq = SR_MQ_CONTINUITY;
202 analog->unit = SR_UNIT_BOOLEAN;
203 *analog->data = 0.0;
204 } else if (!strcmp(tokens[3], "SHORT_CIRCUIT")) {
205 analog->mq = SR_MQ_CONTINUITY;
206 analog->unit = SR_UNIT_BOOLEAN;
207 *analog->data = 1.0;
208 }
209 } if (!strcmp(tokens[1], "F")
210 && !strcmp(tokens[2], "NORMAL")
211 && !strcmp(tokens[3], "NONE")) {
212 analog->mq = SR_MQ_CAPACITANCE;
213 analog->unit = SR_UNIT_FARAD;
214 } else if (!strcmp(tokens[1], "AAC") || !strcmp(tokens[1], "ADC")) {
215 analog->mq = SR_MQ_CURRENT;
216 analog->unit = SR_UNIT_AMPERE;
d38d2ef0
BV
217 if (!strcmp(tokens[2], "NORMAL")) {
218 if (tokens[1][1] == 'A') {
219 analog->mqflags |= SR_MQFLAG_AC;
220 analog->mqflags |= SR_MQFLAG_RMS;
221 } else
222 analog->mqflags |= SR_MQFLAG_DC;
223 } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
224 *analog->data = NAN;
225 } else
226 analog->mq = -1;
227 } if (!strcmp(tokens[1], "Hz") && !strcmp(tokens[2], "NORMAL")) {
228 analog->mq = SR_MQ_FREQUENCY;
229 analog->unit = SR_UNIT_HERTZ;
230 } if (!strcmp(tokens[1], "PCT") && !strcmp(tokens[2], "NORMAL")) {
231 analog->mq = SR_MQ_DUTY_CYCLE;
232 analog->unit = SR_UNIT_PERCENTAGE;
233 } if (!strcmp(tokens[1], "S") && !strcmp(tokens[2], "NORMAL")) {
234 analog->mq = SR_MQ_PULSE_WIDTH;
235 analog->unit = SR_UNIT_SECOND;
236 } if (!strcmp(tokens[1], "SIE") && !strcmp(tokens[2], "NORMAL")) {
237 analog->mq = SR_MQ_CONDUCTANCE;
238 analog->unit = SR_UNIT_SIEMENS;
239 }
240
d38d2ef0
BV
241 if (analog->mq == -1) {
242 /* Not a valid measurement. */
243 g_free(analog->data);
244 g_free(analog);
245 analog = NULL;
246 }
247
248 return analog;
249}
250
251static void handle_line(const struct sr_dev_inst *sdi)
252{
253 struct dev_context *devc;
254 struct sr_datafeed_packet packet;
255 struct sr_datafeed_analog *analog;
256 char **tokens;
257
258 devc = sdi->priv;
259 sr_spew("fluke-dmm: received line '%s' (%d)", devc->buf, devc->buflen);
260
261 if (devc->buflen == 1) {
262 if (devc->buf[0] != '0') {
263 /* Not just a CMD_ACK from the query command. */
264 sr_dbg("fluke-dmm: got CMD_ACK '%c'", devc->buf[0]);
265 devc->expect_response = FALSE;
266 }
267 devc->buflen = 0;
268 return;
269 }
270
271 analog = NULL;
272 tokens = g_strsplit(devc->buf, ",", 0);
fe31f8b9
BV
273 if (tokens[0] && tokens[1]) {
274 if (devc->profile->model == FLUKE_187) {
275 devc->expect_response = FALSE;
276 analog = handle_qm_v1(sdi, tokens);
277 } else if (devc->profile->model == FLUKE_287) {
d38d2ef0 278 devc->expect_response = FALSE;
fe31f8b9 279 analog = handle_qm_v2(sdi, tokens);
d38d2ef0 280 }
d38d2ef0
BV
281 }
282 g_strfreev(tokens);
283 devc->buflen = 0;
284
285 if (analog) {
286 /* Got a measurement. */
d38d2ef0
BV
287 packet.type = SR_DF_ANALOG;
288 packet.payload = analog;
289 sr_session_send(devc->cb_data, &packet);
290 devc->num_samples++;
291 g_free(analog->data);
292 g_free(analog);
293 }
294
295}
296
297SR_PRIV int fluke_receive_data(int fd, int revents, void *cb_data)
298{
299 const struct sr_dev_inst *sdi;
300 struct dev_context *devc;
301 int len;
302 int64_t now, elapsed;
303
304 if (!(sdi = cb_data))
305 return TRUE;
306
307 if (!(devc = sdi->priv))
308 return TRUE;
309
310 if (revents == G_IO_IN) {
311 /* Serial data arrived. */
312 while(FLUKEDMM_BUFSIZE - devc->buflen - 1 > 0) {
313 len = serial_read(fd, devc->buf + devc->buflen, 1);
314 if (len < 1)
315 break;
316 devc->buflen++;
317 *(devc->buf + devc->buflen) = '\0';
318 if (*(devc->buf + devc->buflen - 1) == '\r') {
319 *(devc->buf + --devc->buflen) = '\0';
320 handle_line(sdi);
321 break;
322 }
323 }
324 }
325
326 if (devc->num_samples >= devc->limit_samples) {
327 sdi->driver->dev_acquisition_stop(sdi, cb_data);
328 return TRUE;
329 }
330
331 now = g_get_monotonic_time() / 1000;
332 elapsed = now - devc->cmd_sent_at;
333 /* Send query command at poll_period interval, or after 1 second
334 * has elapsed. This will make it recover from any out-of-sync
335 * or temporary disconnect issues. */
336 if ((devc->expect_response == FALSE && elapsed > devc->profile->poll_period)
337 || elapsed > 1000) {
338 sr_spew("fluke-dmm: sending QM");
339 if (serial_write(devc->serial->fd, "QM\r", 3) == -1)
340 sr_err("fluke-dmm: unable to send QM: %s", strerror(errno));
341 devc->cmd_sent_at = now;
342 devc->expect_response = TRUE;
343 }
344
345 return TRUE;
346}
347
348