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