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