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/>.
21 * Norma DM9x0/Siemens B102x DMMs driver.
28 #define LINE_LENGTH 20
30 SR_PRIV const struct nmadmm_req nmadmm_requests[] = {
31 { NMADMM_REQ_IDN, "IDN?" },
32 { NMADMM_REQ_IDN, "STATUS?" },
36 static int nma_send_req(const struct sr_dev_inst *sdi, int req, char *params)
38 struct sr_serial_dev_inst *serial;
39 struct dev_context *devc;
40 char buf[NMADMM_BUFSIZE];
43 if (!sdi || !(serial = sdi->conn) || !(devc = sdi->priv))
46 len = snprintf(buf, sizeof(buf), "%s%s\r\n",
47 nmadmm_requests[req].req_str, params ? params : "");
49 sr_spew("Sending request: '%s'.", buf);
52 devc->last_req_pending = TRUE;
54 if (serial_write_blocking(serial, buf, len,
55 serial_timeout(serial, len)) < 0) {
56 sr_err("Unable to send request.");
57 devc->last_req_pending = FALSE;
61 devc->req_sent_at = g_get_monotonic_time();
67 * Convert hexadecimal digit to int.
69 * @param[in] xgit Hexadecimal digit to convert.
70 * @return Int value of xgit (0 on invalid xgit).
72 SR_PRIV int xgittoint(char xgit)
74 if ((xgit >= '0') && (xgit <= '9'))
77 if ((xgit >= 'a') && (xgit <= 'f'))
83 * Process received line. It consists of 20 hex digits + \\r\\n,
84 * e.g. '08100400018100400000'.
86 static void nma_process_line(const struct sr_dev_inst *sdi)
88 struct dev_context *devc;
90 int vt, range; /* Measurement value type, range in device format */
91 int mmode, devstat; /* Measuring mode, device status */
92 float value; /* Measured value */
93 float scale; /* Scaling factor depending on range and function */
94 struct sr_datafeed_analog analog;
95 struct sr_analog_encoding encoding;
96 struct sr_analog_meaning meaning;
97 struct sr_analog_spec spec;
98 struct sr_datafeed_packet packet;
102 devc->buf[LINE_LENGTH] = '\0';
104 sr_spew("Received line '%s'.", devc->buf);
107 if (strlen((const char *)devc->buf) != LINE_LENGTH) {
108 sr_err("line: Invalid status '%s', must be 20 hex digits.",
114 for (pos = 0; pos < LINE_LENGTH; pos++) {
115 if (!isxdigit(devc->buf[pos])) {
116 sr_err("line: Expected hex digit in '%s' at pos %d!",
123 /* Start decoding. */
126 /* TODO: Use proper 'digits' value for this device (and its modes). */
127 sr_analog_init(&analog, &encoding, &meaning, &spec, 2);
130 * The numbers are hex digits, starting from 0.
131 * 0: Keyboard status, currently not interesting.
132 * 1: Central switch status, currently not interesting.
133 * 2: Type of measured value.
135 vt = xgittoint(devc->buf[2]);
138 analog.meaning->mq = SR_MQ_VOLTAGE;
141 analog.meaning->mq = SR_MQ_CURRENT; /* 2A */
144 analog.meaning->mq = SR_MQ_RESISTANCE;
147 analog.meaning->mq = SR_MQ_CAPACITANCE;
150 analog.meaning->mq = SR_MQ_TEMPERATURE;
153 analog.meaning->mq = SR_MQ_FREQUENCY;
156 analog.meaning->mq = SR_MQ_CURRENT; /* 10A */
159 analog.meaning->mq = SR_MQ_GAIN; /* TODO: Scale factor */
162 analog.meaning->mq = SR_MQ_GAIN; /* Percentage */
166 analog.meaning->mq = SR_MQ_GAIN; /* dB */
170 sr_err("Unknown value type: 0x%02x.", vt);
174 /* 3: Measurement range for measured value */
175 range = xgittoint(devc->buf[3]);
178 scale *= pow(10.0, range - 5);
181 scale *= pow(10.0, range - 7);
184 scale *= pow(10.0, range - 2);
187 scale *= pow(10.0, range - 12);
190 scale *= pow(10.0, range - 1);
193 scale *= pow(10.0, range - 2);
195 // No default, other value types have fixed display format.
198 /* 5: Sign and 1st digit */
199 flags = xgittoint(devc->buf[5]);
200 value = (flags & 0x03);
204 /* 6-9: 2nd-4th digit */
205 for (pos = 6; pos < 10; pos++)
206 value = value * 10 + xgittoint(devc->buf[pos]);
209 /* 10: Display counter */
210 mmode = xgittoint(devc->buf[10]);
212 case 0: /* Frequency */
213 analog.meaning->unit = SR_UNIT_HERTZ;
215 case 1: /* V TRMS, only type 5 */
216 analog.meaning->unit = SR_UNIT_VOLT;
217 analog.meaning->mqflags |= (SR_MQFLAG_AC | SR_MQFLAG_DC | SR_MQFLAG_RMS);
220 analog.meaning->unit = SR_UNIT_VOLT;
221 analog.meaning->mqflags |= SR_MQFLAG_AC;
223 analog.meaning->mqflags |= SR_MQFLAG_RMS;
226 analog.meaning->unit = SR_UNIT_VOLT;
227 analog.meaning->mqflags |= SR_MQFLAG_DC;
230 analog.meaning->unit = SR_UNIT_OHM;
232 case 5: /* Continuity */
233 analog.meaning->unit = SR_UNIT_BOOLEAN;
234 analog.meaning->mq = SR_MQ_CONTINUITY;
235 /* TODO: Continuity handling is a bit odd in libsigrok. */
237 case 6: /* Degree Celsius */
238 analog.meaning->unit = SR_UNIT_CELSIUS;
240 case 7: /* Capacity */
241 analog.meaning->unit = SR_UNIT_FARAD;
243 case 8: /* Current DC */
244 analog.meaning->unit = SR_UNIT_AMPERE;
245 analog.meaning->mqflags |= SR_MQFLAG_DC;
247 case 9: /* Current AC */
248 analog.meaning->unit = SR_UNIT_AMPERE;
249 analog.meaning->mqflags |= SR_MQFLAG_AC;
251 analog.meaning->mqflags |= SR_MQFLAG_RMS;
253 case 0xa: /* Current TRMS, only type 5 */
254 analog.meaning->unit = SR_UNIT_AMPERE;
255 analog.meaning->mqflags |= (SR_MQFLAG_AC | SR_MQFLAG_DC | SR_MQFLAG_RMS);
257 case 0xb: /* Diode */
258 analog.meaning->unit = SR_UNIT_VOLT;
259 analog.meaning->mqflags |= (SR_MQFLAG_DIODE | SR_MQFLAG_DC);
262 sr_err("Unknown mmode: 0x%02x.", mmode);
266 /* 11: Device status */
267 devstat = xgittoint(devc->buf[11]);
270 case 1: /* Normal measurement */
272 case 2: /* Input loop (limit, reference values) */
274 case 3: /* TRANS/SENS */
277 sr_err("Device error. Fuse?"); /* TODO: Really abort? */
279 return; /* Cannot continue. */
281 sr_err("Unknown device status: 0x%02x", devstat);
285 /* 12-19: Flags and display symbols */
287 flags = (xgittoint(devc->buf[12]) << 8) | xgittoint(devc->buf[13]);
288 /* 0x80: PRINT TODO: Stop polling when discovered? */
290 if (analog.meaning->mq == SR_MQ_CONTINUITY) {
292 value = 1.0; /* Beep */
298 if (flags & 0x04) /* REL */
299 analog.meaning->mqflags |= SR_MQFLAG_RELATIVE;
301 if (flags & 0x01) /* % */
302 analog.meaning->unit = SR_UNIT_PERCENTAGE;
305 flags = (xgittoint(devc->buf[14]) << 8) | xgittoint(devc->buf[15]);
306 if (!(flags & 0x80)) /* MAN: Manual range */
307 analog.meaning->mqflags |= SR_MQFLAG_AUTORANGE;
308 if (flags & 0x40) /* LOBATT1: Low battery, measurement still within specs */
312 if (flags & 0x08) /* HOLD */
313 analog.meaning->mqflags |= SR_MQFLAG_HOLD;
315 if (flags & 0x02) /* MAX */
316 analog.meaning->mqflags |= SR_MQFLAG_MAX;
317 if (flags & 0x01) /* MIN */
318 analog.meaning->mqflags |= SR_MQFLAG_MIN;
321 flags = (xgittoint(devc->buf[16]) << 8) | xgittoint(devc->buf[17]);
322 /* 0xe0: undefined */
323 if (flags & 0x10) { /* LOBATT2: Low battery, measurement inaccurate */
325 sr_warn("Low battery, measurement quality degraded!");
328 /* 0x04: RATE (=lower resolution, allows higher data rate up to 10/s. */
329 /* 0x02: Current clamp */
330 if (flags & 0x01) { /* dB */
332 * TODO: The Norma has an adjustable dB reference value. If
333 * changed from default, this is not correct.
335 if (analog.meaning->unit == SR_UNIT_VOLT)
336 analog.meaning->unit = SR_UNIT_DECIBEL_VOLT;
338 analog.meaning->unit = SR_UNIT_UNITLESS;
342 /* flags = (xgittoint(devc->buf[18]) << 8) | xgittoint(devc->buf[19]); */
343 /* 0x80: Undefined. */
344 /* 0x40: Remote mode, keyboard locked */
345 /* 0x38: Undefined. */
346 /* 0x04: MIN > MAX */
347 /* 0x02: Measured value < Min */
348 /* 0x01: Measured value > Max */
350 /* 4: Flags. Evaluating this after setting value! */
351 flags = xgittoint(devc->buf[4]);
352 if (flags & 0x04) /* Invalid value */
354 else if (flags & 0x01) /* Overload */
356 if (flags & 0x02) { /* Duplicate value, has been sent before. */
357 sr_spew("Duplicate value, dismissing!");
362 sr_spew("range=%d/scale=%f/value=%f", range,
363 (double)scale, (double)value);
365 /* Finish and send packet. */
366 analog.meaning->channels = sdi->channels;
367 analog.num_samples = 1;
368 analog.data = &value;
370 memset(&packet, 0, sizeof(struct sr_datafeed_packet));
371 packet.type = SR_DF_ANALOG;
372 packet.payload = &analog;
373 sr_session_send(sdi, &packet);
375 /* Finish processing. */
376 sr_sw_limits_update_samples_read(&devc->limits, 1);
380 SR_PRIV int norma_dmm_receive_data(int fd, int revents, void *cb_data)
382 struct sr_dev_inst *sdi;
383 struct dev_context *devc;
384 struct sr_serial_dev_inst *serial;
389 if (!(sdi = cb_data))
392 if (!(devc = sdi->priv))
396 if (revents == G_IO_IN) {
397 /* Serial data arrived. */
398 while (NMADMM_BUFSIZE - devc->buflen - 1 > 0) {
399 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, 1);
403 *(devc->buf + devc->buflen) = '\0';
404 if (*(devc->buf + devc->buflen - 1) == '\n') {
406 * TODO: According to specs, should be \r, but
407 * then we'd have to get rid of the \n.
409 devc->last_req_pending = FALSE;
410 nma_process_line(sdi);
416 if (sr_sw_limits_check(&devc->limits)) {
417 sdi->driver->dev_acquisition_stop(sdi);
419 /* Request next package. */
420 if (devc->last_req_pending) {
421 gint64 elapsed_us = g_get_monotonic_time() - devc->req_sent_at;
422 if (elapsed_us > NMADMM_TIMEOUT_MS * 1000) {/* Timeout! */
423 sr_spew("Request timeout!");
424 devc->last_req_pending = FALSE;
427 if (!devc->last_req_pending) {
428 if (nma_send_req(sdi, NMADMM_REQ_STATUS, NULL) != SR_OK)