2 * This file is part of the sigrok project.
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
25 #include "libsigrok.h"
26 #include "libsigrok-internal.h"
27 #include "fluke-dmm.h"
30 static struct sr_datafeed_analog *handle_qm_18x(const struct sr_dev_inst *sdi,
33 struct sr_datafeed_analog *analog;
40 if (strcmp(tokens[0], "QM") || !tokens[1])
43 if ((e = strstr(tokens[1], "Out of range"))) {
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.");
55 while(*e && *e == ' ')
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));
66 *analog->data = fvalue;
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". */
76 analog->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
78 analog->mqflags |= SR_MQFLAG_DC;
79 } else if ((u = strstr(e, "dBV")) || (u = strstr(e, "dBm"))) {
80 analog->mq = SR_MQ_VOLTAGE;
82 analog->unit = SR_UNIT_DECIBEL_MW;
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;
90 *analog->data = INFINITY;
92 *analog->data *= 1000;
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;
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;
110 } else if ((u = strstr(e, "Deg C")) || (u = strstr(e, "Deg F"))) {
111 analog->mq = SR_MQ_TEMPERATURE;
113 analog->unit = SR_UNIT_CELSIUS;
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". */
121 analog->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
123 analog->mqflags |= SR_MQFLAG_DC;
126 *analog->data /= 1e+3;
127 else if (e[0] == 'u')
128 *analog->data /= 1e+6;
130 } else if ((u = strstr(e, "Hz"))) {
131 analog->mq = SR_MQ_FREQUENCY;
132 analog->unit = SR_UNIT_HERTZ;
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;
144 if (analog->mq == -1) {
145 /* Not a valid measurement. */
146 g_free(analog->data);
154 static struct sr_datafeed_analog *handle_qm_28x(const struct sr_dev_inst *sdi,
157 struct sr_datafeed_analog *analog;
166 fvalue = strtof(tokens[0], &eptr);
167 if (fvalue == 0.0 && eptr == tokens[0]) {
168 sr_err("Invalid float.");
172 /* TODO: Check malloc return value. */
173 analog = g_try_malloc0(sizeof(struct sr_datafeed_analog));
174 analog->num_samples = 1;
175 /* TODO: Check malloc return value. */
176 analog->data = g_try_malloc(sizeof(float));
177 *analog->data = fvalue;
180 if (!strcmp(tokens[1], "VAC") || !strcmp(tokens[1], "VDC")) {
181 analog->mq = SR_MQ_VOLTAGE;
182 analog->unit = SR_UNIT_VOLT;
183 if (!strcmp(tokens[2], "NORMAL")) {
184 if (tokens[1][1] == 'A') {
185 analog->mqflags |= SR_MQFLAG_AC;
186 analog->mqflags |= SR_MQFLAG_RMS;
188 analog->mqflags |= SR_MQFLAG_DC;
189 } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
193 } else if (!strcmp(tokens[1], "dBV") || !strcmp(tokens[1], "dBm")) {
194 analog->mq = SR_MQ_VOLTAGE;
195 if (tokens[1][2] == 'm')
196 analog->unit = SR_UNIT_DECIBEL_MW;
198 analog->unit = SR_UNIT_DECIBEL_VOLT;
199 analog->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
200 } else if (!strcmp(tokens[1], "CEL") || !strcmp(tokens[1], "FAR")) {
201 if (!strcmp(tokens[2], "NORMAL")) {
202 analog->mq = SR_MQ_TEMPERATURE;
203 if (tokens[1][0] == 'C')
204 analog->unit = SR_UNIT_CELSIUS;
206 analog->unit = SR_UNIT_FAHRENHEIT;
208 } else if (!strcmp(tokens[1], "OHM")) {
209 if (!strcmp(tokens[3], "NONE")) {
210 analog->mq = SR_MQ_RESISTANCE;
211 analog->unit = SR_UNIT_OHM;
212 if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
213 *analog->data = INFINITY;
214 } else if (strcmp(tokens[2], "NORMAL"))
216 } else if (!strcmp(tokens[3], "OPEN_CIRCUIT")) {
217 analog->mq = SR_MQ_CONTINUITY;
218 analog->unit = SR_UNIT_BOOLEAN;
220 } else if (!strcmp(tokens[3], "SHORT_CIRCUIT")) {
221 analog->mq = SR_MQ_CONTINUITY;
222 analog->unit = SR_UNIT_BOOLEAN;
225 } else if (!strcmp(tokens[1], "F")
226 && !strcmp(tokens[2], "NORMAL")
227 && !strcmp(tokens[3], "NONE")) {
228 analog->mq = SR_MQ_CAPACITANCE;
229 analog->unit = SR_UNIT_FARAD;
230 } else if (!strcmp(tokens[1], "AAC") || !strcmp(tokens[1], "ADC")) {
231 analog->mq = SR_MQ_CURRENT;
232 analog->unit = SR_UNIT_AMPERE;
233 if (!strcmp(tokens[2], "NORMAL")) {
234 if (tokens[1][1] == 'A') {
235 analog->mqflags |= SR_MQFLAG_AC;
236 analog->mqflags |= SR_MQFLAG_RMS;
238 analog->mqflags |= SR_MQFLAG_DC;
239 } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
243 } if (!strcmp(tokens[1], "Hz") && !strcmp(tokens[2], "NORMAL")) {
244 analog->mq = SR_MQ_FREQUENCY;
245 analog->unit = SR_UNIT_HERTZ;
246 } else if (!strcmp(tokens[1], "PCT") && !strcmp(tokens[2], "NORMAL")) {
247 analog->mq = SR_MQ_DUTY_CYCLE;
248 analog->unit = SR_UNIT_PERCENTAGE;
249 } else if (!strcmp(tokens[1], "S") && !strcmp(tokens[2], "NORMAL")) {
250 analog->mq = SR_MQ_PULSE_WIDTH;
251 analog->unit = SR_UNIT_SECOND;
252 } else if (!strcmp(tokens[1], "SIE") && !strcmp(tokens[2], "NORMAL")) {
253 analog->mq = SR_MQ_CONDUCTANCE;
254 analog->unit = SR_UNIT_SIEMENS;
257 if (analog->mq == -1) {
258 /* Not a valid measurement. */
259 g_free(analog->data);
267 static void handle_line(const struct sr_dev_inst *sdi)
269 struct dev_context *devc;
270 struct sr_datafeed_packet packet;
271 struct sr_datafeed_analog *analog;
275 sr_spew("Received line '%s' (%d).", devc->buf, devc->buflen);
277 if (devc->buflen == 1) {
278 if (devc->buf[0] != '0') {
279 /* Not just a CMD_ACK from the query command. */
280 sr_dbg("Got CMD_ACK '%c'.", devc->buf[0]);
281 devc->expect_response = FALSE;
288 tokens = g_strsplit(devc->buf, ",", 0);
290 if (devc->profile->model == FLUKE_187) {
291 devc->expect_response = FALSE;
292 analog = handle_qm_18x(sdi, tokens);
293 } else if (devc->profile->model == FLUKE_287) {
294 devc->expect_response = FALSE;
295 analog = handle_qm_28x(sdi, tokens);
302 /* Got a measurement. */
303 packet.type = SR_DF_ANALOG;
304 packet.payload = analog;
305 sr_session_send(devc->cb_data, &packet);
307 g_free(analog->data);
313 SR_PRIV int fluke_receive_data(int fd, int revents, void *cb_data)
315 struct sr_dev_inst *sdi;
316 struct dev_context *devc;
318 int64_t now, elapsed;
322 if (!(sdi = cb_data))
325 if (!(devc = sdi->priv))
328 if (revents == G_IO_IN) {
329 /* Serial data arrived. */
330 while(FLUKEDMM_BUFSIZE - devc->buflen - 1 > 0) {
331 len = serial_read(devc->serial, devc->buf + devc->buflen, 1);
335 *(devc->buf + devc->buflen) = '\0';
336 if (*(devc->buf + devc->buflen - 1) == '\r') {
337 *(devc->buf + --devc->buflen) = '\0';
344 if (devc->num_samples >= devc->limit_samples) {
345 sdi->driver->dev_acquisition_stop(sdi, cb_data);
349 now = g_get_monotonic_time() / 1000;
350 elapsed = now - devc->cmd_sent_at;
351 /* Send query command at poll_period interval, or after 1 second
352 * has elapsed. This will make it easier to recover from any
353 * out-of-sync or temporary disconnect issues. */
354 if ((devc->expect_response == FALSE && elapsed > devc->profile->poll_period)
355 || elapsed > devc->profile->timeout) {
356 if (serial_write(devc->serial, "QM\r", 3) == -1)
357 sr_err("Unable to send QM: %s.", strerror(errno));
358 devc->cmd_sent_at = now;
359 devc->expect_response = TRUE;