]> sigrok.org Git - libsigrok.git/blame - hardware/fluke-dmm/fluke.c
fluke-dmm: support for all basic 287 functionality
[libsigrok.git] / hardware / fluke-dmm / fluke.c
CommitLineData
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
31static struct sr_datafeed_analog *handle_qm_v2(const struct sr_dev_inst *sdi,
32 char **tokens)
33{
34 struct sr_datafeed_analog *analog;
35 float fvalue;
36 char *eptr;
37
38 (void)sdi;
39 fvalue = strtof(tokens[0], &eptr);
40 if (fvalue == 0.0 && eptr == tokens[0]) {
41 sr_err("fluke-dmm: invalid float");
42 return NULL;
43 }
44
45 analog = g_try_malloc0(sizeof(struct sr_datafeed_analog));
46 analog->num_samples = 1;
47 analog->data = g_try_malloc(sizeof(float));
48 *analog->data = fvalue;
49 analog->mq = -1;
50
51 if (!strcmp(tokens[1], "VAC") || !strcmp(tokens[1], "VDC")) {
52 analog->mq = SR_MQ_VOLTAGE;
53 analog->unit = SR_UNIT_VOLT;
54 if (!strcmp(tokens[2], "NORMAL")) {
55 if (tokens[1][1] == 'A') {
56 analog->mqflags |= SR_MQFLAG_AC;
57 analog->mqflags |= SR_MQFLAG_RMS;
58 } else
59 analog->mqflags |= SR_MQFLAG_DC;
60 } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
61 *analog->data = NAN;
62 } else
63 analog->mq = -1;
64 } if (!strcmp(tokens[1], "CEL") || !strcmp(tokens[1], "FAR")) {
65 if (!strcmp(tokens[2], "NORMAL")) {
66 analog->mq = SR_MQ_TEMPERATURE;
67 if (tokens[1][0] == 'C')
68 analog->unit = SR_UNIT_CELSIUS;
69 else
70 analog->unit = SR_UNIT_FAHRENHEIT;
71 }
72 } if (!strcmp(tokens[1], "OHM")) {
73 if (!strcmp(tokens[3], "NONE")) {
74 analog->mq = SR_MQ_RESISTANCE;
75 analog->unit = SR_UNIT_OHM;
76 if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
77 *analog->data = INFINITY;
78 } else if (strcmp(tokens[2], "NORMAL"))
79 analog->mq = -1;
80 } else if (!strcmp(tokens[3], "OPEN_CIRCUIT")) {
81 analog->mq = SR_MQ_CONTINUITY;
82 analog->unit = SR_UNIT_BOOLEAN;
83 *analog->data = 0.0;
84 } else if (!strcmp(tokens[3], "SHORT_CIRCUIT")) {
85 analog->mq = SR_MQ_CONTINUITY;
86 analog->unit = SR_UNIT_BOOLEAN;
87 *analog->data = 1.0;
88 }
89 } if (!strcmp(tokens[1], "F")
90 && !strcmp(tokens[2], "NORMAL")
91 && !strcmp(tokens[3], "NONE")) {
92 analog->mq = SR_MQ_CAPACITANCE;
93 analog->unit = SR_UNIT_FARAD;
94 } else if (!strcmp(tokens[1], "AAC") || !strcmp(tokens[1], "ADC")) {
95 analog->mq = SR_MQ_CURRENT;
96 analog->unit = SR_UNIT_AMPERE;
97 analog->mqflags = SR_MQFLAG_RMS;
98 if (!strcmp(tokens[2], "NORMAL")) {
99 if (tokens[1][1] == 'A') {
100 analog->mqflags |= SR_MQFLAG_AC;
101 analog->mqflags |= SR_MQFLAG_RMS;
102 } else
103 analog->mqflags |= SR_MQFLAG_DC;
104 } else if (!strcmp(tokens[2], "OL") || !strcmp(tokens[2], "OL_MINUS")) {
105 *analog->data = NAN;
106 } else
107 analog->mq = -1;
108 } if (!strcmp(tokens[1], "Hz") && !strcmp(tokens[2], "NORMAL")) {
109 analog->mq = SR_MQ_FREQUENCY;
110 analog->unit = SR_UNIT_HERTZ;
111 } if (!strcmp(tokens[1], "PCT") && !strcmp(tokens[2], "NORMAL")) {
112 analog->mq = SR_MQ_DUTY_CYCLE;
113 analog->unit = SR_UNIT_PERCENTAGE;
114 } if (!strcmp(tokens[1], "S") && !strcmp(tokens[2], "NORMAL")) {
115 analog->mq = SR_MQ_PULSE_WIDTH;
116 analog->unit = SR_UNIT_SECOND;
117 } if (!strcmp(tokens[1], "SIE") && !strcmp(tokens[2], "NORMAL")) {
118 analog->mq = SR_MQ_CONDUCTANCE;
119 analog->unit = SR_UNIT_SIEMENS;
120 }
121
122
123 if (analog->mq == -1) {
124 /* Not a valid measurement. */
125 g_free(analog->data);
126 g_free(analog);
127 analog = NULL;
128 }
129
130 return analog;
131}
132
133static void handle_line(const struct sr_dev_inst *sdi)
134{
135 struct dev_context *devc;
136 struct sr_datafeed_packet packet;
137 struct sr_datafeed_analog *analog;
138 char **tokens;
139
140 devc = sdi->priv;
141 sr_spew("fluke-dmm: received line '%s' (%d)", devc->buf, devc->buflen);
142
143 if (devc->buflen == 1) {
144 if (devc->buf[0] != '0') {
145 /* Not just a CMD_ACK from the query command. */
146 sr_dbg("fluke-dmm: got CMD_ACK '%c'", devc->buf[0]);
147 devc->expect_response = FALSE;
148 }
149 devc->buflen = 0;
150 return;
151 }
152
153 analog = NULL;
154 tokens = g_strsplit(devc->buf, ",", 0);
155 if (devc->profile->model == FLUKE_187) {
156 if (!strcmp(tokens[0], "QM")) {
157 /* Yes, this is the response to the query. */
158 devc->expect_response = FALSE;
159 sr_spew("187 line");
160 /* TODO */
161 devc->buflen = 0;
162 }
163 } else if (devc->profile->model == FLUKE_287) {
164 devc->expect_response = FALSE;
165 analog = handle_qm_v2(sdi, tokens);
166 }
167 g_strfreev(tokens);
168 devc->buflen = 0;
169
170 if (analog) {
171 /* Got a measurement. */
172 sr_dbg("val %f", *analog->data);
173 packet.type = SR_DF_ANALOG;
174 packet.payload = analog;
175 sr_session_send(devc->cb_data, &packet);
176 devc->num_samples++;
177 g_free(analog->data);
178 g_free(analog);
179 }
180
181}
182
183SR_PRIV int fluke_receive_data(int fd, int revents, void *cb_data)
184{
185 const struct sr_dev_inst *sdi;
186 struct dev_context *devc;
187 int len;
188 int64_t now, elapsed;
189
190 if (!(sdi = cb_data))
191 return TRUE;
192
193 if (!(devc = sdi->priv))
194 return TRUE;
195
196 if (revents == G_IO_IN) {
197 /* Serial data arrived. */
198 while(FLUKEDMM_BUFSIZE - devc->buflen - 1 > 0) {
199 len = serial_read(fd, devc->buf + devc->buflen, 1);
200 if (len < 1)
201 break;
202 devc->buflen++;
203 *(devc->buf + devc->buflen) = '\0';
204 if (*(devc->buf + devc->buflen - 1) == '\r') {
205 *(devc->buf + --devc->buflen) = '\0';
206 handle_line(sdi);
207 break;
208 }
209 }
210 }
211
212 if (devc->num_samples >= devc->limit_samples) {
213 sdi->driver->dev_acquisition_stop(sdi, cb_data);
214 return TRUE;
215 }
216
217 now = g_get_monotonic_time() / 1000;
218 elapsed = now - devc->cmd_sent_at;
219 /* Send query command at poll_period interval, or after 1 second
220 * has elapsed. This will make it recover from any out-of-sync
221 * or temporary disconnect issues. */
222 if ((devc->expect_response == FALSE && elapsed > devc->profile->poll_period)
223 || elapsed > 1000) {
224 sr_spew("fluke-dmm: sending QM");
225 if (serial_write(devc->serial->fd, "QM\r", 3) == -1)
226 sr_err("fluke-dmm: unable to send QM: %s", strerror(errno));
227 devc->cmd_sent_at = now;
228 devc->expect_response = TRUE;
229 }
230
231 return TRUE;
232}
233
234