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/>.
28 static int nma_send_req(const struct sr_dev_inst *sdi, enum nmadmm_req_t reqt,
32 /** Convert hexadecimal digit to int.
33 * \param[in] xgit Hexadecimal digit to convert.
34 * \return Int value of xgit (0 on invalid xgit).
36 SR_PRIV int xgittoint(char xgit)
38 if ((xgit >= '0') && (xgit <= '9'))
41 if ((xgit >= 'a') && (xgit <= 'f'))
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)
51 struct dev_context *devc;
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 */
58 struct sr_datafeed_analog analog;
59 struct sr_datafeed_packet packet;
65 sr_spew("Received line '%s'.", devc->buf);
68 if (strlen((char*)devc->buf) != 20) {
69 sr_err("line: Invalid status '%s', must be 20 hex digits");
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!",
84 memset(&analog, 0, sizeof(analog));
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]);
92 case 0: analog.mq = SR_MQ_VOLTAGE;
94 case 1: analog.mq = SR_MQ_CURRENT; // 2A
96 case 2: analog.mq = SR_MQ_RESISTANCE;
98 case 3: analog.mq = SR_MQ_CAPACITANCE;
100 case 4: analog.mq = SR_MQ_TEMPERATURE;
102 case 5: analog.mq = SR_MQ_FREQUENCY;
104 case 6: analog.mq = SR_MQ_CURRENT; // 10A
106 case 7: analog.mq = SR_MQ_GAIN; // TODO: Scale factor
108 case 8: analog.mq = SR_MQ_GAIN; // Percentage
111 case 9: analog.mq = SR_MQ_GAIN; // dB
114 default:sr_err("Unknown value type 0x%02x", vt);
117 /* 3: Measurement range for measured value */
118 range = xgittoint(devc->buf[3]);
121 scale *= pow(10.0, range - 5);
124 scale *= pow(10.0, range - 7);
127 scale *= pow(10.0, range - 2);
130 scale *= pow(10.0, range - 12);
133 scale *= pow(10.0, range - 1);
136 scale *= pow(10.0, range - 2);
138 // No default, other value types have fixed display format
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]);
150 /* 10: Display counter */
151 mmode = xgittoint(devc->buf[10]);
153 case 0: /* Frequency */
154 analog.unit = SR_UNIT_HERTZ;
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);
161 analog.unit = SR_UNIT_VOLT;
162 analog.mqflags |= SR_MQFLAG_AC;
163 if (devc->type >= 3) analog.mqflags |= SR_MQFLAG_RMS;
166 analog.unit = SR_UNIT_VOLT;
167 analog.mqflags |= SR_MQFLAG_DC;
170 analog.unit = SR_UNIT_OHM;
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. */
177 case 6: /* Degree Celsius */
178 analog.unit = SR_UNIT_CELSIUS;
180 case 7: /* Capacity */
181 analog.unit = SR_UNIT_FARAD;
183 case 8: /* Current DC */
184 analog.unit = SR_UNIT_AMPERE;
185 analog.mqflags |= SR_MQFLAG_DC;
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;
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);
196 case 0x0B:/* Diode */
197 analog.unit = SR_UNIT_VOLT;
198 analog.mqflags |= (SR_MQFLAG_DIODE | SR_MQFLAG_DC);
201 sr_err("unknown mmode 0x%02x", mmode);
205 /* 11: Device status */
206 devstat = xgittoint(devc->buf[11]);
209 case 1: // Normal measurement
211 case 2: // Input loop (limit, reference values)
213 case 3: // TRANS/SENS
216 sr_err("Device error. Fuse?"); // TODO: Wirklich abbrechen???
218 return; // Cannot continue.
220 sr_err("Unknown device status 0x02x", devstat);
224 /* 12-19: Flags and display symbols */
226 flags = (xgittoint(devc->buf[12]) << 8) | xgittoint(devc->buf[13]);
227 /* 0x80: PRINT TODO: Stop polling when discovered? */
229 if (analog.mq == SR_MQ_CONTINUITY) {
230 if (flags & 0x20) value = 1.0; /* Beep */
235 if (flags & 0x04) /* REL */
236 analog.mqflags |= SR_MQFLAG_RELATIVE;
238 if (flags & 0x01) /* % */
239 analog.unit = SR_UNIT_PERCENTAGE;
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.*/
249 if (flags & 0x08) // HOLD
250 analog.mqflags |= SR_MQFLAG_HOLD;
252 if (flags & 0x02) // MAX
253 analog.mqflags |= SR_MQFLAG_MAX;
254 if (flags & 0x01) // MIN
255 analog.mqflags |= SR_MQFLAG_MIN;
258 flags = (xgittoint(devc->buf[16]) << 8) | xgittoint(devc->buf[17]);
259 /* 0xe0: undefined */
260 if (flags & 0x10) { /* LOBATT2 Low battery, measurement inaccurate */
262 sr_warn("Low battery, measurement quality degraded!");
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. */
273 analog.unit = SR_UNIT_UNITLESS;
277 /* flags = (xgittoint(devc->buf[18]) << 8) | xgittoint(devc->buf[19]);*/
278 /* 0x80: Undefined. */
279 /* 0x40: Remote mode, keyboard locked */
280 /* 0x38: Undefined. */
282 /* 0x02: Measured value < Min */
283 /* 0x01: Measured value > Max */
285 /* 4: Flags. Evaluating this after setting value! */
286 flags = xgittoint(devc->buf[4]);
287 if (flags & 0x04) { // Invalid value
290 else if (flags & 0x01) { // Overload
293 if (flags & 0x02) { // Duplicate value, has been sent before.
294 sr_spew("Duplicate value, dismissing!");
299 sr_spew("range=%d/scale=%f/value=%f",range,(double)scale,(double)value);
301 /* Finish and send packet */
302 analog.probes = sdi->probes;
303 analog.num_samples = 1;
304 analog.data = &value;
306 memset(&packet, 0, sizeof(packet));
307 packet.type = SR_DF_ANALOG;
308 packet.payload = &analog;
309 sr_session_send(devc->cb_data, &packet);
311 /* Finish processing */
317 SR_PRIV int norma_dmm_receive_data(int fd, int revents, void *cb_data)
319 struct sr_dev_inst *sdi;
320 struct dev_context *devc;
321 struct sr_serial_dev_inst *serial;
323 gboolean terminating;
327 if (!(sdi = cb_data))
330 if (!(devc = sdi->priv))
334 if (revents == G_IO_IN) {
335 /* Serial data arrived. */
336 while(NMADMM_BUFSIZE - devc->buflen - 1 > 0) {
337 len = serial_read(serial, devc->buf + devc->buflen, 1);
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);
352 // If number of samples or time limit reached, stop aquisition.
354 if (devc->limit_samples && (devc->num_samples >= devc->limit_samples)) {
355 sdi->driver->dev_acquisition_stop(sdi, cb_data);
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);
367 // Request next package
368 if (!terminating && !devc->last_req_pending) {
369 if (nma_send_req(sdi, NMADMM_REQ_STATUS, NULL) != SR_OK)
377 static int nma_send_req(const struct sr_dev_inst *sdi, enum nmadmm_req_t req,
380 struct sr_serial_dev_inst *serial;
381 struct dev_context *devc;
382 char buf[NMADMM_BUFSIZE];
386 if (!sdi || !(serial = sdi->conn) || !(devc = sdi->priv))
389 len = snprintf(buf, sizeof(buf), "%s%s\r\n",
390 nmadmm_requests[req].reqstr, params?params:"");
392 sr_spew("Sending request: '%s'", buf);
394 devc->last_req = req;
395 devc->last_req_pending = TRUE;
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;
408 SR_PRIV const struct nmadmm_req nmadmm_requests[] = {
409 { NMADMM_REQ_IDN, "IDN?"},
410 { NMADMM_REQ_IDN, "STATUS?"},