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/>.
21 #include "libsigrok.h"
22 #include "libsigrok-internal.h"
24 #include "fluke-dmm.h"
31 static struct sr_datafeed_analog *handle_qm_v1(const struct sr_dev_inst *sdi,
34 struct sr_datafeed_analog *analog;
40 if (strcmp(tokens[0], "QM"))
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("fluke-dmm: invalid float");
55 while(*e && *e == ' ')
58 analog = g_try_malloc0(sizeof(struct sr_datafeed_analog));
59 analog->num_samples = 1;
60 analog->data = g_try_malloc(sizeof(float));
64 *analog->data = fvalue;
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". */
74 analog->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
76 analog->mqflags |= SR_MQFLAG_DC;
77 } else if ((u = strstr(e, "dBV")) || (u = strstr(e, "dBm"))) {
78 analog->mq = SR_MQ_VOLTAGE;
80 analog->unit = SR_UNIT_DECIBEL_MW;
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;
88 *analog->data = INFINITY;
90 *analog->data *= 1000;
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;
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;
108 } else if ((u = strstr(e, "Deg C")) || (u = strstr(e, "Deg F"))) {
109 analog->mq = SR_MQ_TEMPERATURE;
111 analog->unit = SR_UNIT_CELSIUS;
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". */
119 analog->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
121 analog->mqflags |= SR_MQFLAG_DC;
124 *analog->data /= 1e+3;
125 else if (e[0] == 'u')
126 *analog->data /= 1e+6;
128 } else if ((u = strstr(e, "Hz"))) {
129 analog->mq = SR_MQ_FREQUENCY;
130 analog->unit = SR_UNIT_HERTZ;
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;
142 if (analog->mq == -1) {
143 /* Not a valid measurement. */
144 g_free(analog->data);
152 static struct sr_datafeed_analog *handle_qm_v2(const struct sr_dev_inst *sdi,
155 struct sr_datafeed_analog *analog;
160 fvalue = strtof(tokens[0], &eptr);
161 if (fvalue == 0.0 && eptr == tokens[0]) {
162 sr_err("fluke-dmm: invalid float");
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;
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;
180 analog->mqflags |= SR_MQFLAG_DC;
181 } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
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')
189 analog->unit = SR_UNIT_CELSIUS;
191 analog->unit = SR_UNIT_FAHRENHEIT;
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"))
201 } else if (!strcmp(tokens[3], "OPEN_CIRCUIT")) {
202 analog->mq = SR_MQ_CONTINUITY;
203 analog->unit = SR_UNIT_BOOLEAN;
205 } else if (!strcmp(tokens[3], "SHORT_CIRCUIT")) {
206 analog->mq = SR_MQ_CONTINUITY;
207 analog->unit = SR_UNIT_BOOLEAN;
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;
218 if (!strcmp(tokens[2], "NORMAL")) {
219 if (tokens[1][1] == 'A') {
220 analog->mqflags |= SR_MQFLAG_AC;
221 analog->mqflags |= SR_MQFLAG_RMS;
223 analog->mqflags |= SR_MQFLAG_DC;
224 } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
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;
242 if (analog->mq == -1) {
243 /* Not a valid measurement. */
244 g_free(analog->data);
252 static void handle_line(const struct sr_dev_inst *sdi)
254 struct dev_context *devc;
255 struct sr_datafeed_packet packet;
256 struct sr_datafeed_analog *analog;
260 sr_spew("fluke-dmm: received line '%s' (%d)", devc->buf, devc->buflen);
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;
273 tokens = g_strsplit(devc->buf, ",", 0);
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) {
279 devc->expect_response = FALSE;
280 analog = handle_qm_v2(sdi, tokens);
287 /* Got a measurement. */
288 packet.type = SR_DF_ANALOG;
289 packet.payload = analog;
290 sr_session_send(devc->cb_data, &packet);
292 g_free(analog->data);
298 SR_PRIV int fluke_receive_data(int fd, int revents, void *cb_data)
300 const struct sr_dev_inst *sdi;
301 struct dev_context *devc;
303 int64_t now, elapsed;
305 if (!(sdi = cb_data))
308 if (!(devc = sdi->priv))
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);
318 *(devc->buf + devc->buflen) = '\0';
319 if (*(devc->buf + devc->buflen - 1) == '\r') {
320 *(devc->buf + --devc->buflen) = '\0';
327 if (devc->num_samples >= devc->limit_samples) {
328 sdi->driver->dev_acquisition_stop(sdi, cb_data);
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)
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;