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"
23 #include "fluke-dmm.h"
30 static struct sr_datafeed_analog *handle_qm_v1(const struct sr_dev_inst *sdi,
33 struct sr_datafeed_analog *analog;
39 if (strcmp(tokens[0], "QM"))
42 if ((e = strstr(tokens[1], "Out of range"))) {
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("Invalid float.");
54 while(*e && *e == ' ')
57 /* TODO: Check malloc return value. */
58 analog = g_try_malloc0(sizeof(struct sr_datafeed_analog));
59 analog->num_samples = 1;
60 /* TODO: Check malloc return value. */
61 analog->data = g_try_malloc(sizeof(float));
65 *analog->data = fvalue;
68 if ((u = strstr(e, "V DC")) || (u = strstr(e, "V AC"))) {
69 analog->mq = SR_MQ_VOLTAGE;
70 analog->unit = SR_UNIT_VOLT;
71 if (!is_oor && e[0] == 'm')
72 *analog->data /= 1000;
73 /* This catches "V AC", "V DC" and "V AC+DC". */
75 analog->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
77 analog->mqflags |= SR_MQFLAG_DC;
78 } else if ((u = strstr(e, "dBV")) || (u = strstr(e, "dBm"))) {
79 analog->mq = SR_MQ_VOLTAGE;
81 analog->unit = SR_UNIT_DECIBEL_MW;
83 analog->unit = SR_UNIT_DECIBEL_VOLT;
84 analog->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
85 } else if ((u = strstr(e, "Ohms"))) {
86 analog->mq = SR_MQ_RESISTANCE;
87 analog->unit = SR_UNIT_OHM;
89 *analog->data = INFINITY;
91 *analog->data *= 1000;
93 *analog->data *= 1000000;
94 } else if (!strcmp(e, "nS")) {
95 analog->mq = SR_MQ_CONDUCTANCE;
96 analog->unit = SR_UNIT_SIEMENS;
97 *analog->data /= 1e+9;
98 } else if ((u = strstr(e, "Farads"))) {
99 analog->mq = SR_MQ_CAPACITANCE;
100 analog->unit = SR_UNIT_FARAD;
103 *analog->data /= 1e+3;
104 else if (e[0] == 'u')
105 *analog->data /= 1e+6;
106 else if (e[0] == 'n')
107 *analog->data /= 1e+9;
109 } else if ((u = strstr(e, "Deg C")) || (u = strstr(e, "Deg F"))) {
110 analog->mq = SR_MQ_TEMPERATURE;
112 analog->unit = SR_UNIT_CELSIUS;
114 analog->unit = SR_UNIT_FAHRENHEIT;
115 } else if ((u = strstr(e, "A AC")) || (u = strstr(e, "A DC"))) {
116 analog->mq = SR_MQ_CURRENT;
117 analog->unit = SR_UNIT_AMPERE;
118 /* This catches "A AC", "A DC" and "A AC+DC". */
120 analog->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
122 analog->mqflags |= SR_MQFLAG_DC;
125 *analog->data /= 1e+3;
126 else if (e[0] == 'u')
127 *analog->data /= 1e+6;
129 } else if ((u = strstr(e, "Hz"))) {
130 analog->mq = SR_MQ_FREQUENCY;
131 analog->unit = SR_UNIT_HERTZ;
133 *analog->data *= 1e+3;
134 } else if (!strcmp(e, "%")) {
135 analog->mq = SR_MQ_DUTY_CYCLE;
136 analog->unit = SR_UNIT_PERCENTAGE;
137 } else if ((u = strstr(e, "ms"))) {
138 analog->mq = SR_MQ_PULSE_WIDTH;
139 analog->unit = SR_UNIT_SECOND;
140 *analog->data /= 1e+3;
143 if (analog->mq == -1) {
144 /* Not a valid measurement. */
145 g_free(analog->data);
153 static struct sr_datafeed_analog *handle_qm_v2(const struct sr_dev_inst *sdi,
156 struct sr_datafeed_analog *analog;
161 fvalue = strtof(tokens[0], &eptr);
162 if (fvalue == 0.0 && eptr == tokens[0]) {
163 sr_err("Invalid float.");
167 /* TODO: Check malloc return value. */
168 analog = g_try_malloc0(sizeof(struct sr_datafeed_analog));
169 analog->num_samples = 1;
170 /* TODO: Check malloc return value. */
171 analog->data = g_try_malloc(sizeof(float));
172 *analog->data = fvalue;
175 if (!strcmp(tokens[1], "VAC") || !strcmp(tokens[1], "VDC")) {
176 analog->mq = SR_MQ_VOLTAGE;
177 analog->unit = SR_UNIT_VOLT;
178 if (!strcmp(tokens[2], "NORMAL")) {
179 if (tokens[1][1] == 'A') {
180 analog->mqflags |= SR_MQFLAG_AC;
181 analog->mqflags |= SR_MQFLAG_RMS;
183 analog->mqflags |= SR_MQFLAG_DC;
184 } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
188 } else if (!strcmp(tokens[1], "dBV") || !strcmp(tokens[1], "dBm")) {
189 analog->mq = SR_MQ_VOLTAGE;
190 if (tokens[1][2] == 'm')
191 analog->unit = SR_UNIT_DECIBEL_MW;
193 analog->unit = SR_UNIT_DECIBEL_VOLT;
194 analog->mqflags |= SR_MQFLAG_AC | SR_MQFLAG_RMS;
195 } else if (!strcmp(tokens[1], "CEL") || !strcmp(tokens[1], "FAR")) {
196 if (!strcmp(tokens[2], "NORMAL")) {
197 analog->mq = SR_MQ_TEMPERATURE;
198 if (tokens[1][0] == 'C')
199 analog->unit = SR_UNIT_CELSIUS;
201 analog->unit = SR_UNIT_FAHRENHEIT;
203 } else if (!strcmp(tokens[1], "OHM")) {
204 if (!strcmp(tokens[3], "NONE")) {
205 analog->mq = SR_MQ_RESISTANCE;
206 analog->unit = SR_UNIT_OHM;
207 if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
208 *analog->data = INFINITY;
209 } else if (strcmp(tokens[2], "NORMAL"))
211 } else if (!strcmp(tokens[3], "OPEN_CIRCUIT")) {
212 analog->mq = SR_MQ_CONTINUITY;
213 analog->unit = SR_UNIT_BOOLEAN;
215 } else if (!strcmp(tokens[3], "SHORT_CIRCUIT")) {
216 analog->mq = SR_MQ_CONTINUITY;
217 analog->unit = SR_UNIT_BOOLEAN;
220 } else if (!strcmp(tokens[1], "F")
221 && !strcmp(tokens[2], "NORMAL")
222 && !strcmp(tokens[3], "NONE")) {
223 analog->mq = SR_MQ_CAPACITANCE;
224 analog->unit = SR_UNIT_FARAD;
225 } else if (!strcmp(tokens[1], "AAC") || !strcmp(tokens[1], "ADC")) {
226 analog->mq = SR_MQ_CURRENT;
227 analog->unit = SR_UNIT_AMPERE;
228 if (!strcmp(tokens[2], "NORMAL")) {
229 if (tokens[1][1] == 'A') {
230 analog->mqflags |= SR_MQFLAG_AC;
231 analog->mqflags |= SR_MQFLAG_RMS;
233 analog->mqflags |= SR_MQFLAG_DC;
234 } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
238 } if (!strcmp(tokens[1], "Hz") && !strcmp(tokens[2], "NORMAL")) {
239 analog->mq = SR_MQ_FREQUENCY;
240 analog->unit = SR_UNIT_HERTZ;
241 } else if (!strcmp(tokens[1], "PCT") && !strcmp(tokens[2], "NORMAL")) {
242 analog->mq = SR_MQ_DUTY_CYCLE;
243 analog->unit = SR_UNIT_PERCENTAGE;
244 } else if (!strcmp(tokens[1], "S") && !strcmp(tokens[2], "NORMAL")) {
245 analog->mq = SR_MQ_PULSE_WIDTH;
246 analog->unit = SR_UNIT_SECOND;
247 } else if (!strcmp(tokens[1], "SIE") && !strcmp(tokens[2], "NORMAL")) {
248 analog->mq = SR_MQ_CONDUCTANCE;
249 analog->unit = SR_UNIT_SIEMENS;
252 if (analog->mq == -1) {
253 /* Not a valid measurement. */
254 g_free(analog->data);
262 static void handle_line(const struct sr_dev_inst *sdi)
264 struct dev_context *devc;
265 struct sr_datafeed_packet packet;
266 struct sr_datafeed_analog *analog;
270 sr_spew("Received line '%s' (%d).", devc->buf, devc->buflen);
272 if (devc->buflen == 1) {
273 if (devc->buf[0] != '0') {
274 /* Not just a CMD_ACK from the query command. */
275 sr_dbg("Got CMD_ACK '%c'.", devc->buf[0]);
276 devc->expect_response = FALSE;
283 tokens = g_strsplit(devc->buf, ",", 0);
284 if (tokens[0] && tokens[1]) {
285 if (devc->profile->model == FLUKE_187) {
286 devc->expect_response = FALSE;
287 analog = handle_qm_v1(sdi, tokens);
288 } else if (devc->profile->model == FLUKE_287) {
289 devc->expect_response = FALSE;
290 analog = handle_qm_v2(sdi, tokens);
297 /* Got a measurement. */
298 packet.type = SR_DF_ANALOG;
299 packet.payload = analog;
300 sr_session_send(devc->cb_data, &packet);
302 g_free(analog->data);
308 SR_PRIV int fluke_receive_data(int fd, int revents, void *cb_data)
310 const struct sr_dev_inst *sdi;
311 struct dev_context *devc;
313 int64_t now, elapsed;
315 if (!(sdi = cb_data))
318 if (!(devc = sdi->priv))
321 if (revents == G_IO_IN) {
322 /* Serial data arrived. */
323 while(FLUKEDMM_BUFSIZE - devc->buflen - 1 > 0) {
324 len = serial_read(fd, devc->buf + devc->buflen, 1);
328 *(devc->buf + devc->buflen) = '\0';
329 if (*(devc->buf + devc->buflen - 1) == '\r') {
330 *(devc->buf + --devc->buflen) = '\0';
337 if (devc->num_samples >= devc->limit_samples) {
338 sdi->driver->dev_acquisition_stop(sdi, cb_data);
342 now = g_get_monotonic_time() / 1000;
343 elapsed = now - devc->cmd_sent_at;
344 /* Send query command at poll_period interval, or after 1 second
345 * has elapsed. This will make it recover from any out-of-sync
346 * or temporary disconnect issues. */
347 if ((devc->expect_response == FALSE && elapsed > devc->profile->poll_period)
349 sr_spew("Sending QM.");
350 if (serial_write(devc->serial->fd, "QM\r", 3) == -1)
351 sr_err("Unable to send QM: %s.", strerror(errno));
352 devc->cmd_sent_at = now;
353 devc->expect_response = TRUE;