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