]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 Matthias Heidbrink <m-sigrok@heidbrink.biz> | |
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 "protocol.h" | |
21 | ||
22 | SR_PRIV const struct nmadmm_req nmadmm_requests[] = { | |
23 | { NMADMM_REQ_IDN, "IDN?" }, | |
24 | { NMADMM_REQ_IDN, "STATUS?" }, | |
25 | { 0, NULL }, | |
26 | }; | |
27 | ||
28 | static int nma_send_req(const struct sr_dev_inst *sdi, int req, char *params) | |
29 | { | |
30 | struct sr_serial_dev_inst *serial; | |
31 | struct dev_context *devc; | |
32 | char buf[NMADMM_BUFSIZE]; | |
33 | int len; | |
34 | ||
35 | if (!sdi || !(serial = sdi->conn) || !(devc = sdi->priv)) | |
36 | return SR_ERR_BUG; | |
37 | ||
38 | len = snprintf(buf, sizeof(buf), "%s%s\r\n", | |
39 | nmadmm_requests[req].req_str, params ? params : ""); | |
40 | ||
41 | sr_spew("Sending request: '%s'.", buf); | |
42 | ||
43 | devc->last_req = req; | |
44 | devc->last_req_pending = TRUE; | |
45 | ||
46 | if (serial_write(serial, buf, len) == -1) { | |
47 | sr_err("Unable to send request: %d %s.", | |
48 | errno, strerror(errno)); | |
49 | devc->last_req_pending = FALSE; | |
50 | return SR_ERR; | |
51 | } | |
52 | ||
53 | return SR_OK; | |
54 | } | |
55 | ||
56 | /** | |
57 | * Convert hexadecimal digit to int. | |
58 | * | |
59 | * @param[in] xgit Hexadecimal digit to convert. | |
60 | * @return Int value of xgit (0 on invalid xgit). | |
61 | */ | |
62 | SR_PRIV int xgittoint(char xgit) | |
63 | { | |
64 | if ((xgit >= '0') && (xgit <= '9')) | |
65 | return xgit - '0'; | |
66 | xgit = tolower(xgit); | |
67 | if ((xgit >= 'a') && (xgit <= 'f')) | |
68 | return xgit - 'a'; | |
69 | return 0; | |
70 | } | |
71 | ||
72 | /** | |
73 | * Process received line. It consists of 20 hex digits + \\r\\n, | |
74 | * e.g. '08100400018100400000'. | |
75 | */ | |
76 | static void nma_process_line(const struct sr_dev_inst *sdi) | |
77 | { | |
78 | struct dev_context *devc; | |
79 | int pos, flags; | |
80 | int vt, range; /* Measurement value type, range in device format */ | |
81 | int mmode, devstat; /* Measuring mode, device status */ | |
82 | float value; /* Measured value */ | |
83 | float scale; /* Scaling factor depending on range and function */ | |
84 | struct sr_datafeed_analog analog; | |
85 | struct sr_datafeed_packet packet; | |
86 | ||
87 | devc = sdi->priv; | |
88 | ||
89 | devc->buf[20] = '\0'; | |
90 | ||
91 | sr_spew("Received line '%s'.", devc->buf); | |
92 | ||
93 | /* Check line. */ | |
94 | if (strlen((const char *)devc->buf) != 20) { | |
95 | sr_err("line: Invalid status '%s', must be 20 hex digits.", | |
96 | devc->buf); | |
97 | devc->buflen = 0; | |
98 | return; | |
99 | } | |
100 | ||
101 | for (pos = 0; pos < 20; pos++) { | |
102 | if (!isxdigit(devc->buf[pos])) { | |
103 | sr_err("line: Expected hex digit in '%s' at pos %d!", | |
104 | devc->buf, pos); | |
105 | devc->buflen = 0; | |
106 | return; | |
107 | } | |
108 | } | |
109 | ||
110 | /* Start decoding. */ | |
111 | value = 0.0; | |
112 | scale = 1.0; | |
113 | memset(&analog, 0, sizeof(analog)); | |
114 | ||
115 | /* | |
116 | * The numbers are hex digits, starting from 0. | |
117 | * 0: Keyboard status, currently not interesting. | |
118 | * 1: Central switch status, currently not interesting. | |
119 | * 2: Type of measured value. | |
120 | */ | |
121 | vt = xgittoint(devc->buf[2]); | |
122 | switch (vt) { | |
123 | case 0: | |
124 | analog.mq = SR_MQ_VOLTAGE; | |
125 | break; | |
126 | case 1: | |
127 | analog.mq = SR_MQ_CURRENT; /* 2A */ | |
128 | break; | |
129 | case 2: | |
130 | analog.mq = SR_MQ_RESISTANCE; | |
131 | break; | |
132 | case 3: | |
133 | analog.mq = SR_MQ_CAPACITANCE; | |
134 | break; | |
135 | case 4: | |
136 | analog.mq = SR_MQ_TEMPERATURE; | |
137 | break; | |
138 | case 5: | |
139 | analog.mq = SR_MQ_FREQUENCY; | |
140 | break; | |
141 | case 6: | |
142 | analog.mq = SR_MQ_CURRENT; /* 10A */ | |
143 | break; | |
144 | case 7: | |
145 | analog.mq = SR_MQ_GAIN; /* TODO: Scale factor */ | |
146 | break; | |
147 | case 8: | |
148 | analog.mq = SR_MQ_GAIN; /* Percentage */ | |
149 | scale /= 100.0; | |
150 | break; | |
151 | case 9: | |
152 | analog.mq = SR_MQ_GAIN; /* dB */ | |
153 | scale /= 100.0; | |
154 | break; | |
155 | default: | |
156 | sr_err("Unknown value type: 0x%02x.", vt); | |
157 | break; | |
158 | } | |
159 | ||
160 | /* 3: Measurement range for measured value */ | |
161 | range = xgittoint(devc->buf[3]); | |
162 | switch (vt) { | |
163 | case 0: /* V */ | |
164 | scale *= pow(10.0, range - 5); | |
165 | break; | |
166 | case 1: /* A */ | |
167 | scale *= pow(10.0, range - 7); | |
168 | break; | |
169 | case 2: /* Ω */ | |
170 | scale *= pow(10.0, range - 2); | |
171 | break; | |
172 | case 3: /* F */ | |
173 | scale *= pow(10.0, range - 12); | |
174 | break; | |
175 | case 4: /* °C */ | |
176 | scale *= pow(10.0, range - 1); | |
177 | break; | |
178 | case 5: /* Hz */ | |
179 | scale *= pow(10.0, range - 2); | |
180 | break; | |
181 | // No default, other value types have fixed display format. | |
182 | } | |
183 | ||
184 | /* 5: Sign and 1st digit */ | |
185 | flags = xgittoint(devc->buf[5]); | |
186 | value = (flags & 0x03); | |
187 | if (flags & 0x04) | |
188 | scale *= -1; | |
189 | ||
190 | /* 6-9: 2nd-4th digit */ | |
191 | for (pos = 6; pos < 10; pos++) | |
192 | value = value * 10 + xgittoint(devc->buf[pos]); | |
193 | value *= scale; | |
194 | ||
195 | /* 10: Display counter */ | |
196 | mmode = xgittoint(devc->buf[10]); | |
197 | switch (mmode) { | |
198 | case 0: /* Frequency */ | |
199 | analog.unit = SR_UNIT_HERTZ; | |
200 | break; | |
201 | case 1: /* V TRMS, only type 5 */ | |
202 | analog.unit = SR_UNIT_VOLT; | |
203 | analog.mqflags |= (SR_MQFLAG_AC | SR_MQFLAG_DC | SR_MQFLAG_RMS); | |
204 | break; | |
205 | case 2: /* V AC */ | |
206 | analog.unit = SR_UNIT_VOLT; | |
207 | analog.mqflags |= SR_MQFLAG_AC; | |
208 | if (devc->type >= 3) | |
209 | analog.mqflags |= SR_MQFLAG_RMS; | |
210 | break; | |
211 | case 3: /* V DC */ | |
212 | analog.unit = SR_UNIT_VOLT; | |
213 | analog.mqflags |= SR_MQFLAG_DC; | |
214 | break; | |
215 | case 4: /* Ohm */ | |
216 | analog.unit = SR_UNIT_OHM; | |
217 | break; | |
218 | case 5: /* Continuity */ | |
219 | analog.unit = SR_UNIT_BOOLEAN; | |
220 | analog.mq = SR_MQ_CONTINUITY; | |
221 | /* TODO: Continuity handling is a bit odd in libsigrok. */ | |
222 | break; | |
223 | case 6: /* Degree Celsius */ | |
224 | analog.unit = SR_UNIT_CELSIUS; | |
225 | break; | |
226 | case 7: /* Capacity */ | |
227 | analog.unit = SR_UNIT_FARAD; | |
228 | break; | |
229 | case 8: /* Current DC */ | |
230 | analog.unit = SR_UNIT_AMPERE; | |
231 | analog.mqflags |= SR_MQFLAG_DC; | |
232 | break; | |
233 | case 9: /* Current AC */ | |
234 | analog.unit = SR_UNIT_AMPERE; | |
235 | analog.mqflags |= SR_MQFLAG_AC; | |
236 | if (devc->type >= 3) | |
237 | analog.mqflags |= SR_MQFLAG_RMS; | |
238 | break; | |
239 | case 0xa: /* Current TRMS, only type 5 */ | |
240 | analog.unit = SR_UNIT_AMPERE; | |
241 | analog.mqflags |= (SR_MQFLAG_AC | SR_MQFLAG_DC | SR_MQFLAG_RMS); | |
242 | break; | |
243 | case 0xb: /* Diode */ | |
244 | analog.unit = SR_UNIT_VOLT; | |
245 | analog.mqflags |= (SR_MQFLAG_DIODE | SR_MQFLAG_DC); | |
246 | break; | |
247 | default: | |
248 | sr_err("Unknown mmode: 0x%02x.", mmode); | |
249 | break; | |
250 | } | |
251 | ||
252 | /* 11: Device status */ | |
253 | devstat = xgittoint(devc->buf[11]); | |
254 | ||
255 | switch (devstat) { | |
256 | case 1: /* Normal measurement */ | |
257 | break; | |
258 | case 2: /* Input loop (limit, reference values) */ | |
259 | break; | |
260 | case 3: /* TRANS/SENS */ | |
261 | break; | |
262 | case 4: /* Error */ | |
263 | sr_err("Device error. Fuse?"); /* TODO: Really abort? */ | |
264 | devc->buflen = 0; | |
265 | return; /* Cannot continue. */ | |
266 | default: | |
267 | sr_err("Unknown device status: 0x%02x", devstat); | |
268 | break; | |
269 | } | |
270 | ||
271 | /* 12-19: Flags and display symbols */ | |
272 | /* 12, 13 */ | |
273 | flags = (xgittoint(devc->buf[12]) << 8) | xgittoint(devc->buf[13]); | |
274 | /* 0x80: PRINT TODO: Stop polling when discovered? */ | |
275 | /* 0x40: EXTR */ | |
276 | if (analog.mq == SR_MQ_CONTINUITY) { | |
277 | if (flags & 0x20) | |
278 | value = 1.0; /* Beep */ | |
279 | else | |
280 | value = 0.0; | |
281 | } | |
282 | /* 0x10: AVG */ | |
283 | /* 0x08: Diode */ | |
284 | if (flags & 0x04) /* REL */ | |
285 | analog.mqflags |= SR_MQFLAG_RELATIVE; | |
286 | /* 0x02: SHIFT */ | |
287 | if (flags & 0x01) /* % */ | |
288 | analog.unit = SR_UNIT_PERCENTAGE; | |
289 | ||
290 | /* 14, 15 */ | |
291 | flags = (xgittoint(devc->buf[14]) << 8) | xgittoint(devc->buf[15]); | |
292 | if (!(flags & 0x80)) /* MAN: Manual range */ | |
293 | analog.mqflags |= SR_MQFLAG_AUTORANGE; | |
294 | if (flags & 0x40) /* LOBATT1: Low battery, measurement still within specs */ | |
295 | devc->lowbatt = 1; | |
296 | /* 0x20: PEAK */ | |
297 | /* 0x10: COUNT */ | |
298 | if (flags & 0x08) /* HOLD */ | |
299 | analog.mqflags |= SR_MQFLAG_HOLD; | |
300 | /* 0x04: LIMIT */ | |
301 | if (flags & 0x02) /* MAX */ | |
302 | analog.mqflags |= SR_MQFLAG_MAX; | |
303 | if (flags & 0x01) /* MIN */ | |
304 | analog.mqflags |= SR_MQFLAG_MIN; | |
305 | ||
306 | /* 16, 17 */ | |
307 | flags = (xgittoint(devc->buf[16]) << 8) | xgittoint(devc->buf[17]); | |
308 | /* 0xe0: undefined */ | |
309 | if (flags & 0x10) { /* LOBATT2: Low battery, measurement inaccurate */ | |
310 | devc->lowbatt = 2; | |
311 | sr_warn("Low battery, measurement quality degraded!"); | |
312 | } | |
313 | /* 0x08: SCALED */ | |
314 | /* 0x04: RATE (=lower resolution, allows higher rata rate up to 10/s. */ | |
315 | /* 0x02: Current clamp */ | |
316 | if (flags & 0x01) { /* dB */ | |
317 | /* | |
318 | * TODO: The Norma has an adjustable dB reference value. If | |
319 | * changed from default, this is not correct. | |
320 | */ | |
321 | if (analog.unit == SR_UNIT_VOLT) | |
322 | analog.unit = SR_UNIT_DECIBEL_VOLT; | |
323 | else | |
324 | analog.unit = SR_UNIT_UNITLESS; | |
325 | } | |
326 | ||
327 | /* 18, 19 */ | |
328 | /* flags = (xgittoint(devc->buf[18]) << 8) | xgittoint(devc->buf[19]); */ | |
329 | /* 0x80: Undefined. */ | |
330 | /* 0x40: Remote mode, keyboard locked */ | |
331 | /* 0x38: Undefined. */ | |
332 | /* 0x04: MIN > MAX */ | |
333 | /* 0x02: Measured value < Min */ | |
334 | /* 0x01: Measured value > Max */ | |
335 | ||
336 | /* 4: Flags. Evaluating this after setting value! */ | |
337 | flags = xgittoint(devc->buf[4]); | |
338 | if (flags & 0x04) /* Invalid value */ | |
339 | value = NAN; | |
340 | else if (flags & 0x01) /* Overload */ | |
341 | value = INFINITY; | |
342 | if (flags & 0x02) { /* Duplicate value, has been sent before. */ | |
343 | sr_spew("Duplicate value, dismissing!"); | |
344 | devc->buflen = 0; | |
345 | return; | |
346 | } | |
347 | ||
348 | sr_spew("range=%d/scale=%f/value=%f", range, | |
349 | (double)scale, (double)value); | |
350 | ||
351 | /* Finish and send packet. */ | |
352 | analog.probes = sdi->probes; | |
353 | analog.num_samples = 1; | |
354 | analog.data = &value; | |
355 | ||
356 | memset(&packet, 0, sizeof(packet)); | |
357 | packet.type = SR_DF_ANALOG; | |
358 | packet.payload = &analog; | |
359 | sr_session_send(devc->cb_data, &packet); | |
360 | ||
361 | /* Finish processing. */ | |
362 | devc->num_samples++; | |
363 | devc->buflen = 0; | |
364 | } | |
365 | ||
366 | SR_PRIV int norma_dmm_receive_data(int fd, int revents, void *cb_data) | |
367 | { | |
368 | struct sr_dev_inst *sdi; | |
369 | struct dev_context *devc; | |
370 | struct sr_serial_dev_inst *serial; | |
371 | int len; | |
372 | gboolean terminating; | |
373 | gdouble elapsed_s; | |
374 | ||
375 | (void)fd; | |
376 | ||
377 | if (!(sdi = cb_data)) | |
378 | return TRUE; | |
379 | ||
380 | if (!(devc = sdi->priv)) | |
381 | return TRUE; | |
382 | ||
383 | serial = sdi->conn; | |
384 | if (revents == G_IO_IN) { | |
385 | /* Serial data arrived. */ | |
386 | while (NMADMM_BUFSIZE - devc->buflen - 1 > 0) { | |
387 | len = serial_read(serial, devc->buf + devc->buflen, 1); | |
388 | if (len < 1) | |
389 | break; | |
390 | devc->buflen += len; | |
391 | *(devc->buf + devc->buflen) = '\0'; | |
392 | if (*(devc->buf + devc->buflen - 1) == '\n') { | |
393 | /* | |
394 | * TODO: According to specs, should be \r, but | |
395 | * then we'd have to get rid of the \n. | |
396 | */ | |
397 | devc->last_req_pending = FALSE; | |
398 | nma_process_line(sdi); | |
399 | break; | |
400 | } | |
401 | } | |
402 | } | |
403 | ||
404 | /* If number of samples or time limit reached, stop aquisition. */ | |
405 | terminating = FALSE; | |
406 | if (devc->limit_samples && (devc->num_samples >= devc->limit_samples)) { | |
407 | sdi->driver->dev_acquisition_stop(sdi, cb_data); | |
408 | terminating = TRUE; | |
409 | } | |
410 | ||
411 | if (devc->limit_msec) { | |
412 | elapsed_s = g_timer_elapsed(devc->elapsed_msec, NULL); | |
413 | if ((elapsed_s * 1000) >= devc->limit_msec) { | |
414 | sdi->driver->dev_acquisition_stop(sdi, cb_data); | |
415 | terminating = TRUE; | |
416 | } | |
417 | } | |
418 | ||
419 | /* Request next package. */ | |
420 | if (!terminating && !devc->last_req_pending) { | |
421 | if (nma_send_req(sdi, NMADMM_REQ_STATUS, NULL) != SR_OK) | |
422 | return FALSE; | |
423 | } | |
424 | ||
425 | return TRUE; | |
426 | } |