2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Matthias Heidbrink <m-sigrok@heidbrink.biz>
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/>.
22 SR_PRIV const struct nmadmm_req nmadmm_requests[] = {
23 { NMADMM_REQ_IDN, "IDN?" },
24 { NMADMM_REQ_IDN, "STATUS?" },
28 static int nma_send_req(const struct sr_dev_inst *sdi, int req, char *params)
30 struct sr_serial_dev_inst *serial;
31 struct dev_context *devc;
32 char buf[NMADMM_BUFSIZE];
35 if (!sdi || !(serial = sdi->conn) || !(devc = sdi->priv))
38 len = snprintf(buf, sizeof(buf), "%s%s\r\n",
39 nmadmm_requests[req].req_str, params ? params : "");
41 sr_spew("Sending request: '%s'.", buf);
44 devc->last_req_pending = TRUE;
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;
57 * Convert hexadecimal digit to int.
59 * @param[in] xgit Hexadecimal digit to convert.
60 * @return Int value of xgit (0 on invalid xgit).
62 SR_PRIV int xgittoint(char xgit)
64 if ((xgit >= '0') && (xgit <= '9'))
67 if ((xgit >= 'a') && (xgit <= 'f'))
73 * Process received line. It consists of 20 hex digits + \\r\\n,
74 * e.g. '08100400018100400000'.
76 static void nma_process_line(const struct sr_dev_inst *sdi)
78 struct dev_context *devc;
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;
91 sr_spew("Received line '%s'.", devc->buf);
94 if (strlen((const char *)devc->buf) != 20) {
95 sr_err("line: Invalid status '%s', must be 20 hex digits.",
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!",
110 /* Start decoding. */
113 memset(&analog, 0, sizeof(analog));
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.
121 vt = xgittoint(devc->buf[2]);
124 analog.mq = SR_MQ_VOLTAGE;
127 analog.mq = SR_MQ_CURRENT; /* 2A */
130 analog.mq = SR_MQ_RESISTANCE;
133 analog.mq = SR_MQ_CAPACITANCE;
136 analog.mq = SR_MQ_TEMPERATURE;
139 analog.mq = SR_MQ_FREQUENCY;
142 analog.mq = SR_MQ_CURRENT; /* 10A */
145 analog.mq = SR_MQ_GAIN; /* TODO: Scale factor */
148 analog.mq = SR_MQ_GAIN; /* Percentage */
152 analog.mq = SR_MQ_GAIN; /* dB */
156 sr_err("Unknown value type: 0x%02x.", vt);
160 /* 3: Measurement range for measured value */
161 range = xgittoint(devc->buf[3]);
164 scale *= pow(10.0, range - 5);
167 scale *= pow(10.0, range - 7);
170 scale *= pow(10.0, range - 2);
173 scale *= pow(10.0, range - 12);
176 scale *= pow(10.0, range - 1);
179 scale *= pow(10.0, range - 2);
181 // No default, other value types have fixed display format.
184 /* 5: Sign and 1st digit */
185 flags = xgittoint(devc->buf[5]);
186 value = (flags & 0x03);
190 /* 6-9: 2nd-4th digit */
191 for (pos = 6; pos < 10; pos++)
192 value = value * 10 + xgittoint(devc->buf[pos]);
195 /* 10: Display counter */
196 mmode = xgittoint(devc->buf[10]);
198 case 0: /* Frequency */
199 analog.unit = SR_UNIT_HERTZ;
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);
206 analog.unit = SR_UNIT_VOLT;
207 analog.mqflags |= SR_MQFLAG_AC;
209 analog.mqflags |= SR_MQFLAG_RMS;
212 analog.unit = SR_UNIT_VOLT;
213 analog.mqflags |= SR_MQFLAG_DC;
216 analog.unit = SR_UNIT_OHM;
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. */
223 case 6: /* Degree Celsius */
224 analog.unit = SR_UNIT_CELSIUS;
226 case 7: /* Capacity */
227 analog.unit = SR_UNIT_FARAD;
229 case 8: /* Current DC */
230 analog.unit = SR_UNIT_AMPERE;
231 analog.mqflags |= SR_MQFLAG_DC;
233 case 9: /* Current AC */
234 analog.unit = SR_UNIT_AMPERE;
235 analog.mqflags |= SR_MQFLAG_AC;
237 analog.mqflags |= SR_MQFLAG_RMS;
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);
243 case 0xb: /* Diode */
244 analog.unit = SR_UNIT_VOLT;
245 analog.mqflags |= (SR_MQFLAG_DIODE | SR_MQFLAG_DC);
248 sr_err("Unknown mmode: 0x%02x.", mmode);
252 /* 11: Device status */
253 devstat = xgittoint(devc->buf[11]);
256 case 1: /* Normal measurement */
258 case 2: /* Input loop (limit, reference values) */
260 case 3: /* TRANS/SENS */
263 sr_err("Device error. Fuse?"); /* TODO: Really abort? */
265 return; /* Cannot continue. */
267 sr_err("Unknown device status: 0x%02x", devstat);
271 /* 12-19: Flags and display symbols */
273 flags = (xgittoint(devc->buf[12]) << 8) | xgittoint(devc->buf[13]);
274 /* 0x80: PRINT TODO: Stop polling when discovered? */
276 if (analog.mq == SR_MQ_CONTINUITY) {
278 value = 1.0; /* Beep */
284 if (flags & 0x04) /* REL */
285 analog.mqflags |= SR_MQFLAG_RELATIVE;
287 if (flags & 0x01) /* % */
288 analog.unit = SR_UNIT_PERCENTAGE;
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 */
298 if (flags & 0x08) /* HOLD */
299 analog.mqflags |= SR_MQFLAG_HOLD;
301 if (flags & 0x02) /* MAX */
302 analog.mqflags |= SR_MQFLAG_MAX;
303 if (flags & 0x01) /* MIN */
304 analog.mqflags |= SR_MQFLAG_MIN;
307 flags = (xgittoint(devc->buf[16]) << 8) | xgittoint(devc->buf[17]);
308 /* 0xe0: undefined */
309 if (flags & 0x10) { /* LOBATT2: Low battery, measurement inaccurate */
311 sr_warn("Low battery, measurement quality degraded!");
314 /* 0x04: RATE (=lower resolution, allows higher rata rate up to 10/s. */
315 /* 0x02: Current clamp */
316 if (flags & 0x01) { /* dB */
318 * TODO: The Norma has an adjustable dB reference value. If
319 * changed from default, this is not correct.
321 if (analog.unit == SR_UNIT_VOLT)
322 analog.unit = SR_UNIT_DECIBEL_VOLT;
324 analog.unit = SR_UNIT_UNITLESS;
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 */
336 /* 4: Flags. Evaluating this after setting value! */
337 flags = xgittoint(devc->buf[4]);
338 if (flags & 0x04) /* Invalid value */
340 else if (flags & 0x01) /* Overload */
342 if (flags & 0x02) { /* Duplicate value, has been sent before. */
343 sr_spew("Duplicate value, dismissing!");
348 sr_spew("range=%d/scale=%f/value=%f", range,
349 (double)scale, (double)value);
351 /* Finish and send packet. */
352 analog.probes = sdi->probes;
353 analog.num_samples = 1;
354 analog.data = &value;
356 memset(&packet, 0, sizeof(packet));
357 packet.type = SR_DF_ANALOG;
358 packet.payload = &analog;
359 sr_session_send(devc->cb_data, &packet);
361 /* Finish processing. */
366 SR_PRIV int norma_dmm_receive_data(int fd, int revents, void *cb_data)
368 struct sr_dev_inst *sdi;
369 struct dev_context *devc;
370 struct sr_serial_dev_inst *serial;
372 gboolean terminating;
377 if (!(sdi = cb_data))
380 if (!(devc = sdi->priv))
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);
391 *(devc->buf + devc->buflen) = '\0';
392 if (*(devc->buf + devc->buflen - 1) == '\n') {
394 * TODO: According to specs, should be \r, but
395 * then we'd have to get rid of the \n.
397 devc->last_req_pending = FALSE;
398 nma_process_line(sdi);
404 /* If number of samples or time limit reached, stop aquisition. */
406 if (devc->limit_samples && (devc->num_samples >= devc->limit_samples)) {
407 sdi->driver->dev_acquisition_stop(sdi, cb_data);
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);
419 /* Request next package. */
420 if (!terminating && !devc->last_req_pending) {
421 if (nma_send_req(sdi, NMADMM_REQ_STATUS, NULL) != SR_OK)