]>
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 | ||
e790bd5c UH |
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; | |
f8e76e2e | 37 | |
e790bd5c UH |
38 | len = snprintf(buf, sizeof(buf), "%s%s\r\n", |
39 | nmadmm_requests[req].req_str, params ? params : ""); | |
f8e76e2e | 40 | |
e790bd5c | 41 | sr_spew("Sending request: '%s'.", buf); |
f8e76e2e | 42 | |
e790bd5c UH |
43 | devc->last_req = req; |
44 | devc->last_req_pending = TRUE; | |
f8e76e2e | 45 | |
e790bd5c UH |
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). | |
f8e76e2e MH |
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 | ||
e790bd5c UH |
72 | /** |
73 | * Process received line. It consists of 20 hex digits + \r\n, | |
74 | * e.g. '08100400018100400000'. | |
75 | */ | |
f8e76e2e MH |
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 */ | |
f8e76e2e MH |
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 | ||
e790bd5c UH |
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); | |
f8e76e2e MH |
97 | devc->buflen = 0; |
98 | return; | |
99 | } | |
e790bd5c UH |
100 | |
101 | for (pos = 0; pos < 20; pos++) { | |
f8e76e2e MH |
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 | } | |
e790bd5c | 108 | } |
f8e76e2e MH |
109 | |
110 | /* Start decoding. */ | |
111 | value = 0.0; | |
112 | scale = 1.0; | |
113 | memset(&analog, 0, sizeof(analog)); | |
114 | ||
e790bd5c UH |
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 | */ | |
f8e76e2e MH |
121 | vt = xgittoint(devc->buf[2]); |
122 | switch (vt) { | |
e790bd5c UH |
123 | case 0: |
124 | analog.mq = SR_MQ_VOLTAGE; | |
f8e76e2e | 125 | break; |
e790bd5c UH |
126 | case 1: |
127 | analog.mq = SR_MQ_CURRENT; /* 2A */ | |
f8e76e2e | 128 | break; |
e790bd5c UH |
129 | case 2: |
130 | analog.mq = SR_MQ_RESISTANCE; | |
f8e76e2e | 131 | break; |
e790bd5c UH |
132 | case 3: |
133 | analog.mq = SR_MQ_CAPACITANCE; | |
f8e76e2e | 134 | break; |
e790bd5c UH |
135 | case 4: |
136 | analog.mq = SR_MQ_TEMPERATURE; | |
f8e76e2e | 137 | break; |
e790bd5c UH |
138 | case 5: |
139 | analog.mq = SR_MQ_FREQUENCY; | |
f8e76e2e | 140 | break; |
e790bd5c UH |
141 | case 6: |
142 | analog.mq = SR_MQ_CURRENT; /* 10A */ | |
f8e76e2e | 143 | break; |
e790bd5c UH |
144 | case 7: |
145 | analog.mq = SR_MQ_GAIN; /* TODO: Scale factor */ | |
f8e76e2e | 146 | break; |
e790bd5c UH |
147 | case 8: |
148 | analog.mq = SR_MQ_GAIN; /* Percentage */ | |
f8e76e2e MH |
149 | scale /= 100.0; |
150 | break; | |
e790bd5c UH |
151 | case 9: |
152 | analog.mq = SR_MQ_GAIN; /* dB */ | |
f8e76e2e MH |
153 | scale /= 100.0; |
154 | break; | |
e790bd5c UH |
155 | default: |
156 | sr_err("Unknown value type: 0x%02x.", vt); | |
157 | break; | |
f8e76e2e | 158 | } |
e790bd5c | 159 | |
f8e76e2e MH |
160 | /* 3: Measurement range for measured value */ |
161 | range = xgittoint(devc->buf[3]); | |
162 | switch (vt) { | |
e790bd5c | 163 | case 0: /* V */ |
f8e76e2e MH |
164 | scale *= pow(10.0, range - 5); |
165 | break; | |
e790bd5c | 166 | case 1: /* A */ |
f8e76e2e MH |
167 | scale *= pow(10.0, range - 7); |
168 | break; | |
e790bd5c | 169 | case 2: /* Ω */ |
f8e76e2e MH |
170 | scale *= pow(10.0, range - 2); |
171 | break; | |
e790bd5c | 172 | case 3: /* F */ |
f8e76e2e MH |
173 | scale *= pow(10.0, range - 12); |
174 | break; | |
e790bd5c | 175 | case 4: /* °C */ |
f8e76e2e MH |
176 | scale *= pow(10.0, range - 1); |
177 | break; | |
e790bd5c | 178 | case 5: /* Hz */ |
f8e76e2e MH |
179 | scale *= pow(10.0, range - 2); |
180 | break; | |
e790bd5c | 181 | // No default, other value types have fixed display format. |
f8e76e2e MH |
182 | } |
183 | ||
184 | /* 5: Sign and 1st digit */ | |
185 | flags = xgittoint(devc->buf[5]); | |
186 | value = (flags & 0x03); | |
e790bd5c UH |
187 | if (flags & 0x04) |
188 | scale *= -1; | |
189 | ||
f8e76e2e MH |
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) { | |
e790bd5c | 198 | case 0: /* Frequency */ |
f8e76e2e MH |
199 | analog.unit = SR_UNIT_HERTZ; |
200 | break; | |
e790bd5c | 201 | case 1: /* V TRMS, only type 5 */ |
f8e76e2e MH |
202 | analog.unit = SR_UNIT_VOLT; |
203 | analog.mqflags |= (SR_MQFLAG_AC | SR_MQFLAG_DC | SR_MQFLAG_RMS); | |
204 | break; | |
e790bd5c | 205 | case 2: /* V AC */ |
f8e76e2e MH |
206 | analog.unit = SR_UNIT_VOLT; |
207 | analog.mqflags |= SR_MQFLAG_AC; | |
e790bd5c UH |
208 | if (devc->type >= 3) |
209 | analog.mqflags |= SR_MQFLAG_RMS; | |
f8e76e2e | 210 | break; |
e790bd5c | 211 | case 3: /* V DC */ |
f8e76e2e MH |
212 | analog.unit = SR_UNIT_VOLT; |
213 | analog.mqflags |= SR_MQFLAG_DC; | |
214 | break; | |
e790bd5c | 215 | case 4: /* Ohm */ |
f8e76e2e MH |
216 | analog.unit = SR_UNIT_OHM; |
217 | break; | |
e790bd5c | 218 | case 5: /* Continuity */ |
f8e76e2e MH |
219 | analog.unit = SR_UNIT_BOOLEAN; |
220 | analog.mq = SR_MQ_CONTINUITY; | |
e790bd5c | 221 | /* TODO: Continuity handling is a bit odd in libsigrok. */ |
f8e76e2e MH |
222 | break; |
223 | case 6: /* Degree Celsius */ | |
224 | analog.unit = SR_UNIT_CELSIUS; | |
225 | break; | |
e790bd5c | 226 | case 7: /* Capacity */ |
f8e76e2e MH |
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; | |
e790bd5c | 233 | case 9: /* Current AC */ |
f8e76e2e MH |
234 | analog.unit = SR_UNIT_AMPERE; |
235 | analog.mqflags |= SR_MQFLAG_AC; | |
e790bd5c UH |
236 | if (devc->type >= 3) |
237 | analog.mqflags |= SR_MQFLAG_RMS; | |
f8e76e2e | 238 | break; |
e790bd5c | 239 | case 0xa: /* Current TRMS, only type 5 */ |
f8e76e2e MH |
240 | analog.unit = SR_UNIT_AMPERE; |
241 | analog.mqflags |= (SR_MQFLAG_AC | SR_MQFLAG_DC | SR_MQFLAG_RMS); | |
242 | break; | |
e790bd5c | 243 | case 0xb: /* Diode */ |
f8e76e2e MH |
244 | analog.unit = SR_UNIT_VOLT; |
245 | analog.mqflags |= (SR_MQFLAG_DIODE | SR_MQFLAG_DC); | |
246 | break; | |
247 | default: | |
e790bd5c | 248 | sr_err("Unknown mmode: 0x%02x.", mmode); |
f8e76e2e MH |
249 | break; |
250 | } | |
251 | ||
252 | /* 11: Device status */ | |
253 | devstat = xgittoint(devc->buf[11]); | |
254 | ||
255 | switch (devstat) { | |
e790bd5c | 256 | case 1: /* Normal measurement */ |
f8e76e2e | 257 | break; |
e790bd5c | 258 | case 2: /* Input loop (limit, reference values) */ |
f8e76e2e | 259 | break; |
e790bd5c | 260 | case 3: /* TRANS/SENS */ |
f8e76e2e | 261 | break; |
e790bd5c UH |
262 | case 4: /* Error */ |
263 | sr_err("Device error. Fuse?"); /* TODO: Really abort? */ | |
f8e76e2e | 264 | devc->buflen = 0; |
e790bd5c | 265 | return; /* Cannot continue. */ |
f8e76e2e | 266 | default: |
e790bd5c | 267 | sr_err("Unknown device status: 0x%02x", devstat); |
f8e76e2e MH |
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? */ | |
e790bd5c | 275 | /* 0x40: EXTR */ |
f8e76e2e | 276 | if (analog.mq == SR_MQ_CONTINUITY) { |
e790bd5c UH |
277 | if (flags & 0x20) |
278 | value = 1.0; /* Beep */ | |
279 | else | |
280 | value = 0.0; | |
f8e76e2e MH |
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]); | |
e790bd5c | 292 | if (!(flags & 0x80)) /* MAN: Manual range */ |
f8e76e2e | 293 | analog.mqflags |= SR_MQFLAG_AUTORANGE; |
e790bd5c | 294 | if (flags & 0x40) /* LOBATT1: Low battery, measurement still within specs */ |
f8e76e2e | 295 | devc->lowbatt = 1; |
e790bd5c UH |
296 | /* 0x20: PEAK */ |
297 | /* 0x10: COUNT */ | |
298 | if (flags & 0x08) /* HOLD */ | |
f8e76e2e MH |
299 | analog.mqflags |= SR_MQFLAG_HOLD; |
300 | /* 0x04: LIMIT */ | |
e790bd5c | 301 | if (flags & 0x02) /* MAX */ |
f8e76e2e | 302 | analog.mqflags |= SR_MQFLAG_MAX; |
e790bd5c | 303 | if (flags & 0x01) /* MIN */ |
f8e76e2e MH |
304 | analog.mqflags |= SR_MQFLAG_MIN; |
305 | ||
306 | /* 16, 17 */ | |
307 | flags = (xgittoint(devc->buf[16]) << 8) | xgittoint(devc->buf[17]); | |
308 | /* 0xe0: undefined */ | |
e790bd5c | 309 | if (flags & 0x10) { /* LOBATT2: Low battery, measurement inaccurate */ |
f8e76e2e MH |
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. */ | |
e790bd5c UH |
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 | */ | |
f8e76e2e MH |
321 | if (analog.unit == SR_UNIT_VOLT) |
322 | analog.unit = SR_UNIT_DECIBEL_VOLT; | |
f8e76e2e MH |
323 | else |
324 | analog.unit = SR_UNIT_UNITLESS; | |
325 | } | |
326 | ||
327 | /* 18, 19 */ | |
e790bd5c | 328 | /* flags = (xgittoint(devc->buf[18]) << 8) | xgittoint(devc->buf[19]); */ |
f8e76e2e MH |
329 | /* 0x80: Undefined. */ |
330 | /* 0x40: Remote mode, keyboard locked */ | |
331 | /* 0x38: Undefined. */ | |
e790bd5c | 332 | /* 0x04: MIN > MAX */ |
f8e76e2e MH |
333 | /* 0x02: Measured value < Min */ |
334 | /* 0x01: Measured value > Max */ | |
335 | ||
e790bd5c | 336 | /* 4: Flags. Evaluating this after setting value! */ |
f8e76e2e | 337 | flags = xgittoint(devc->buf[4]); |
e790bd5c | 338 | if (flags & 0x04) /* Invalid value */ |
f8e76e2e | 339 | value = NAN; |
e790bd5c | 340 | else if (flags & 0x01) /* Overload */ |
f8e76e2e | 341 | value = INFINITY; |
e790bd5c | 342 | if (flags & 0x02) { /* Duplicate value, has been sent before. */ |
f8e76e2e MH |
343 | sr_spew("Duplicate value, dismissing!"); |
344 | devc->buflen = 0; | |
345 | return; | |
346 | } | |
347 | ||
e790bd5c UH |
348 | sr_spew("range=%d/scale=%f/value=%f", range, |
349 | (double)scale, (double)value); | |
f8e76e2e | 350 | |
e790bd5c | 351 | /* Finish and send packet. */ |
f8e76e2e MH |
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 | ||
e790bd5c | 361 | /* Finish processing. */ |
f8e76e2e MH |
362 | devc->num_samples++; |
363 | devc->buflen = 0; | |
364 | } | |
365 | ||
bfd48770 MH |
366 | SR_PRIV int norma_dmm_receive_data(int fd, int revents, void *cb_data) |
367 | { | |
f8e76e2e | 368 | struct sr_dev_inst *sdi; |
bfd48770 | 369 | struct dev_context *devc; |
f8e76e2e MH |
370 | struct sr_serial_dev_inst *serial; |
371 | int len; | |
372 | gboolean terminating; | |
e790bd5c | 373 | gdouble elapsed_s; |
bfd48770 MH |
374 | |
375 | (void)fd; | |
376 | ||
377 | if (!(sdi = cb_data)) | |
378 | return TRUE; | |
379 | ||
380 | if (!(devc = sdi->priv)) | |
381 | return TRUE; | |
382 | ||
f8e76e2e | 383 | serial = sdi->conn; |
bfd48770 | 384 | if (revents == G_IO_IN) { |
f8e76e2e | 385 | /* Serial data arrived. */ |
e790bd5c | 386 | while (NMADMM_BUFSIZE - devc->buflen - 1 > 0) { |
f8e76e2e MH |
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') { | |
e790bd5c UH |
393 | /* |
394 | * TODO: According to specs, should be \r, but | |
395 | * then we'd have to get rid of the \n. | |
396 | */ | |
f8e76e2e MH |
397 | devc->last_req_pending = FALSE; |
398 | nma_process_line(sdi); | |
399 | break; | |
400 | } | |
401 | } | |
402 | } | |
403 | ||
e790bd5c | 404 | /* If number of samples or time limit reached, stop aquisition. */ |
f8e76e2e MH |
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) { | |
e790bd5c UH |
412 | elapsed_s = g_timer_elapsed(devc->elapsed_msec, NULL); |
413 | if ((elapsed_s * 1000) >= devc->limit_msec) { | |
f8e76e2e MH |
414 | sdi->driver->dev_acquisition_stop(sdi, cb_data); |
415 | terminating = TRUE; | |
416 | } | |
417 | } | |
418 | ||
e790bd5c | 419 | /* Request next package. */ |
f8e76e2e MH |
420 | if (!terminating && !devc->last_req_pending) { |
421 | if (nma_send_req(sdi, NMADMM_REQ_STATUS, NULL) != SR_OK) | |
422 | return FALSE; | |
bfd48770 MH |
423 | } |
424 | ||
425 | return TRUE; | |
426 | } |