norma-dmm: Minor cosmetics, coding-style, consistency fixes.

Also, add a missing "%" in an sr_err() and a missing parameter in
another sr_err().
This commit is contained in:
Uwe Hermann 2013-10-25 20:10:01 +02:00
parent f8e76e2e74
commit e790bd5cda
4 changed files with 413 additions and 410 deletions

View File

@ -423,7 +423,7 @@ fi
AM_CONDITIONAL(HW_NORMA_DMM, test x$HW_NORMA_DMM = xyes) AM_CONDITIONAL(HW_NORMA_DMM, test x$HW_NORMA_DMM = xyes)
if test "x$HW_NORMA_DMM" = "xyes"; then if test "x$HW_NORMA_DMM" = "xyes"; then
AC_DEFINE(HAVE_HW_NORMA_DMM, 1, [norma-dmm support]) AC_DEFINE(HAVE_HW_NORMA_DMM, 1, [Norma DMM support])
fi fi
AM_CONDITIONAL(HW_OLS, test x$HW_OLS = xyes) AM_CONDITIONAL(HW_OLS, test x$HW_OLS = xyes)

View File

@ -17,13 +17,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include "protocol.h" #include "protocol.h"
static const int32_t hwopts[] = { static const int32_t hwopts[] = {
SR_CONF_CONN, SR_CONF_CONN,
SR_CONF_SERIALCOMM, SR_CONF_SERIALCOMM,
@ -36,20 +31,18 @@ static const int32_t hwcaps[] = {
SR_CONF_CONTINUOUS, SR_CONF_CONTINUOUS,
}; };
#define BUF_MAX 50
#define SERIALCOMM "4800/8n1/dtr=1/rts=0/flow=1" #define SERIALCOMM "4800/8n1/dtr=1/rts=0/flow=1"
SR_PRIV struct sr_dev_driver norma_dmm_driver_info; SR_PRIV struct sr_dev_driver norma_dmm_driver_info;
static struct sr_dev_driver *di = &norma_dmm_driver_info; static struct sr_dev_driver *di = &norma_dmm_driver_info;
static int init(struct sr_context *sr_ctx) static int init(struct sr_context *sr_ctx)
{ {
return std_init(sr_ctx, di, LOG_PREFIX); return std_init(sr_ctx, di, LOG_PREFIX);
} }
static GSList *scan(GSList *options) static GSList *scan(GSList *options)
{ {
struct sr_dev_inst *sdi; struct sr_dev_inst *sdi;
@ -66,8 +59,6 @@ static GSList *scan(GSList *options)
char req[10]; char req[10];
int auxtype; int auxtype;
#define BUF_MAX (50)
devices = NULL; devices = NULL;
drvc = di->priv; drvc = di->priv;
drvc->instances = NULL; drvc->instances = NULL;
@ -97,14 +88,13 @@ static GSList *scan(GSList *options)
serial_flush(serial); serial_flush(serial);
len = BUF_MAX; if (!(buf = g_try_malloc(BUF_MAX))) {
if (!(buf = g_try_malloc(len))) {
sr_err("Serial buffer malloc failed."); sr_err("Serial buffer malloc failed.");
return NULL; return NULL;
} }
snprintf(req, sizeof(req), "%s\r\n", snprintf(req, sizeof(req), "%s\r\n",
nmadmm_requests[NMADMM_REQ_IDN].reqstr); nmadmm_requests[NMADMM_REQ_IDN].req_str);
for (cnt = 0; cnt < 7; cnt++) { for (cnt = 0; cnt < 7; cnt++) {
if (serial_write(serial, req, strlen(req)) == -1) { if (serial_write(serial, req, strlen(req)) == -1) {
sr_err("Unable to send identification request: %d %s.", sr_err("Unable to send identification request: %d %s.",
@ -117,7 +107,7 @@ static GSList *scan(GSList *options)
continue; continue;
buf[BUF_MAX - 1] = '\0'; buf[BUF_MAX - 1] = '\0';
/* Match id string, e.g. "1834 065 V1.06,IF V1.02" (DM950) */ /* Match ID string, e.g. "1834 065 V1.06,IF V1.02" (DM950) */
if (g_regex_match_simple("^1834 [^,]*,IF V*", (char *)buf, 0, 0)) { if (g_regex_match_simple("^1834 [^,]*,IF V*", (char *)buf, 0, 0)) {
auxtype = xgittoint(buf[7]); auxtype = xgittoint(buf[7]);
// TODO: Will this work with non-DM950? // TODO: Will this work with non-DM950?
@ -127,7 +117,7 @@ static GSList *scan(GSList *options)
if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE,
"Norma", fmttype, buf + 9))) "Norma", fmttype, buf + 9)))
return NULL; return NULL;
if (!(devc = g_try_malloc0(sizeof(*devc)))) { if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
sr_err("Device context malloc failed."); sr_err("Device context malloc failed.");
return NULL; return NULL;
} }
@ -146,9 +136,13 @@ static GSList *scan(GSList *options)
devices = g_slist_append(devices, sdi); devices = g_slist_append(devices, sdi);
break; break;
} }
/* The interface of the DM9x0 contains a cap that needs to
charge for up to 10s before the interface works, if not powered /*
externally. Therefore wait a little to improve chances. */ * The interface of the DM9x0 contains a cap that needs to
* charge for up to 10s before the interface works, if not
* powered externally. Therefore wait a little to improve
* chances.
*/
if (cnt == 3) { if (cnt == 3) {
sr_info("Waiting 5s to allow interface to settle."); sr_info("Waiting 5s to allow interface to settle.");
g_usleep(5 * 1000 * 1000); g_usleep(5 * 1000 * 1000);
@ -164,19 +158,16 @@ static GSList *scan(GSList *options)
return devices; return devices;
} }
static GSList *dev_list(void) static GSList *dev_list(void)
{ {
return ((struct drv_context *)(di->priv))->instances; return ((struct drv_context *)(di->priv))->instances;
} }
static int dev_clear(void) static int dev_clear(void)
{ {
return std_dev_clear(di, NULL); return std_dev_clear(di, NULL);
} }
static int dev_open(struct sr_dev_inst *sdi) static int dev_open(struct sr_dev_inst *sdi)
{ {
struct sr_serial_dev_inst *serial; struct sr_serial_dev_inst *serial;
@ -190,7 +181,6 @@ static int dev_open(struct sr_dev_inst *sdi)
return SR_OK; return SR_OK;
} }
static int dev_close(struct sr_dev_inst *sdi) static int dev_close(struct sr_dev_inst *sdi)
{ {
struct sr_serial_dev_inst *serial; struct sr_serial_dev_inst *serial;
@ -202,7 +192,7 @@ static int dev_close(struct sr_dev_inst *sdi)
sdi->status = SR_ST_INACTIVE; sdi->status = SR_ST_INACTIVE;
} }
// Free dynamically allocated resources. /* Free dynamically allocated resources. */
if ((devc = sdi->priv) && devc->version) { if ((devc = sdi->priv) && devc->version) {
g_free(devc->version); g_free(devc->version);
devc->version = NULL; devc->version = NULL;
@ -212,13 +202,11 @@ static int dev_close(struct sr_dev_inst *sdi)
return SR_OK; return SR_OK;
} }
static int cleanup(void) static int cleanup(void)
{ {
return dev_clear(); return dev_clear();
} }
static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi) static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
{ {
struct dev_context *devc; struct dev_context *devc;
@ -254,7 +242,6 @@ static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
return SR_OK; return SR_OK;
} }
static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi) static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
{ {
(void)sdi; (void)sdi;
@ -275,7 +262,6 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
return SR_OK; return SR_OK;
} }
static int dev_acquisition_start(const struct sr_dev_inst *sdi, static int dev_acquisition_start(const struct sr_dev_inst *sdi,
void *cb_data) void *cb_data)
{ {
@ -308,7 +294,6 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi,
return SR_OK; return SR_OK;
} }
static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
{ {
struct dev_context *devc; struct dev_context *devc;
@ -321,10 +306,9 @@ static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
sdi->conn, LOG_PREFIX); sdi->conn, LOG_PREFIX);
} }
SR_PRIV struct sr_dev_driver norma_dmm_driver_info = { SR_PRIV struct sr_dev_driver norma_dmm_driver_info = {
.name = "norma-dmm", .name = "norma-dmm",
.longname = "Norma DM910..950, Siemens B1024..1028 DMMs", .longname = "Norma DM9x0 / Siemens B102x DMMs",
.api_version = 1, .api_version = 1,
.init = init, .init = init,
.cleanup = cleanup, .cleanup = cleanup,

View File

@ -19,19 +19,45 @@
#include "protocol.h" #include "protocol.h"
#include <ctype.h> SR_PRIV const struct nmadmm_req nmadmm_requests[] = {
#include <errno.h> { NMADMM_REQ_IDN, "IDN?" },
#include <math.h> { NMADMM_REQ_IDN, "STATUS?" },
#include <string.h> { 0, NULL },
};
static int nma_send_req(const struct sr_dev_inst *sdi, int req, char *params)
{
struct sr_serial_dev_inst *serial;
struct dev_context *devc;
char buf[NMADMM_BUFSIZE];
int len;
static int nma_send_req(const struct sr_dev_inst *sdi, enum nmadmm_req_t reqt, if (!sdi || !(serial = sdi->conn) || !(devc = sdi->priv))
char* params); return SR_ERR_BUG;
len = snprintf(buf, sizeof(buf), "%s%s\r\n",
nmadmm_requests[req].req_str, params ? params : "");
/** Convert hexadecimal digit to int. sr_spew("Sending request: '%s'.", buf);
* \param[in] xgit Hexadecimal digit to convert.
* \return Int value of xgit (0 on invalid xgit). devc->last_req = req;
devc->last_req_pending = TRUE;
if (serial_write(serial, buf, len) == -1) {
sr_err("Unable to send request: %d %s.",
errno, strerror(errno));
devc->last_req_pending = FALSE;
return SR_ERR;
}
return SR_OK;
}
/**
* Convert hexadecimal digit to int.
*
* @param[in] xgit Hexadecimal digit to convert.
* @return Int value of xgit (0 on invalid xgit).
*/ */
SR_PRIV int xgittoint(char xgit) SR_PRIV int xgittoint(char xgit)
{ {
@ -43,9 +69,10 @@ SR_PRIV int xgittoint(char xgit)
return 0; return 0;
} }
/**
/** Process received line. It consists of 20 hex digits + \r\n, * Process received line. It consists of 20 hex digits + \r\n,
* e.g. '08100400018100400000'. */ * e.g. '08100400018100400000'.
*/
static void nma_process_line(const struct sr_dev_inst *sdi) static void nma_process_line(const struct sr_dev_inst *sdi)
{ {
struct dev_context *devc; struct dev_context *devc;
@ -54,7 +81,6 @@ static void nma_process_line(const struct sr_dev_inst *sdi)
int mmode, devstat; /* Measuring mode, device status */ int mmode, devstat; /* Measuring mode, device status */
float value; /* Measured value */ float value; /* Measured value */
float scale; /* Scaling factor depending on range and function */ float scale; /* Scaling factor depending on range and function */
struct sr_datafeed_analog analog; struct sr_datafeed_analog analog;
struct sr_datafeed_packet packet; struct sr_datafeed_packet packet;
@ -64,84 +90,103 @@ static void nma_process_line(const struct sr_dev_inst *sdi)
sr_spew("Received line '%s'.", devc->buf); sr_spew("Received line '%s'.", devc->buf);
/* Check line */ /* Check line. */
if (strlen((char*)devc->buf) != 20) { if (strlen((const char *)devc->buf) != 20) {
sr_err("line: Invalid status '%s', must be 20 hex digits"); sr_err("line: Invalid status '%s', must be 20 hex digits.",
devc->buf);
devc->buflen = 0; devc->buflen = 0;
return; return;
} }
for (pos = 0; pos < 20; pos++)
for (pos = 0; pos < 20; pos++) {
if (!isxdigit(devc->buf[pos])) { if (!isxdigit(devc->buf[pos])) {
sr_err("line: Expected hex digit in '%s' at pos %d!", sr_err("line: Expected hex digit in '%s' at pos %d!",
devc->buf, pos); devc->buf, pos);
devc->buflen = 0; devc->buflen = 0;
return; return;
} }
}
/* Start decoding. */ /* Start decoding. */
value = 0.0; value = 0.0;
scale = 1.0; scale = 1.0;
memset(&analog, 0, sizeof(analog)); memset(&analog, 0, sizeof(analog));
/* The numbers are hex digits, starting from 0. */ /*
/* 0: Keyboard status, currently not interesting. */ * The numbers are hex digits, starting from 0.
/* 1: Central switch status, currently not interesting. */ * 0: Keyboard status, currently not interesting.
/* 2: Type of measured value */ * 1: Central switch status, currently not interesting.
* 2: Type of measured value.
*/
vt = xgittoint(devc->buf[2]); vt = xgittoint(devc->buf[2]);
switch (vt) { switch (vt) {
case 0: analog.mq = SR_MQ_VOLTAGE; case 0:
analog.mq = SR_MQ_VOLTAGE;
break; break;
case 1: analog.mq = SR_MQ_CURRENT; // 2A case 1:
analog.mq = SR_MQ_CURRENT; /* 2A */
break; break;
case 2: analog.mq = SR_MQ_RESISTANCE; case 2:
analog.mq = SR_MQ_RESISTANCE;
break; break;
case 3: analog.mq = SR_MQ_CAPACITANCE; case 3:
analog.mq = SR_MQ_CAPACITANCE;
break; break;
case 4: analog.mq = SR_MQ_TEMPERATURE; case 4:
analog.mq = SR_MQ_TEMPERATURE;
break; break;
case 5: analog.mq = SR_MQ_FREQUENCY; case 5:
analog.mq = SR_MQ_FREQUENCY;
break; break;
case 6: analog.mq = SR_MQ_CURRENT; // 10A case 6:
analog.mq = SR_MQ_CURRENT; /* 10A */
break; break;
case 7: analog.mq = SR_MQ_GAIN; // TODO: Scale factor case 7:
analog.mq = SR_MQ_GAIN; /* TODO: Scale factor */
break; break;
case 8: analog.mq = SR_MQ_GAIN; // Percentage case 8:
analog.mq = SR_MQ_GAIN; /* Percentage */
scale /= 100.0; scale /= 100.0;
break; break;
case 9: analog.mq = SR_MQ_GAIN; // dB case 9:
analog.mq = SR_MQ_GAIN; /* dB */
scale /= 100.0; scale /= 100.0;
break; break;
default:sr_err("Unknown value type 0x%02x", vt); default:
sr_err("Unknown value type: 0x%02x.", vt);
break; break;
} }
/* 3: Measurement range for measured value */ /* 3: Measurement range for measured value */
range = xgittoint(devc->buf[3]); range = xgittoint(devc->buf[3]);
switch (vt) { switch (vt) {
case 0: // V case 0: /* V */
scale *= pow(10.0, range - 5); scale *= pow(10.0, range - 5);
break; break;
case 1: // A case 1: /* A */
scale *= pow(10.0, range - 7); scale *= pow(10.0, range - 7);
break; break;
case 2: // Ω case 2: /* Ω */
scale *= pow(10.0, range - 2); scale *= pow(10.0, range - 2);
break; break;
case 3: // F case 3: /* F */
scale *= pow(10.0, range - 12); scale *= pow(10.0, range - 12);
break; break;
case 4: // °C case 4: /* °C */
scale *= pow(10.0, range - 1); scale *= pow(10.0, range - 1);
break; break;
case 5: // Hz case 5: /* Hz */
scale *= pow(10.0, range - 2); scale *= pow(10.0, range - 2);
break; break;
// No default, other value types have fixed display format // No default, other value types have fixed display format.
} }
/* 5: Sign and 1st digit */ /* 5: Sign and 1st digit */
flags = xgittoint(devc->buf[5]); flags = xgittoint(devc->buf[5]);
value = (flags & 0x03); value = (flags & 0x03);
if (flags & 0x04) scale *= -1; if (flags & 0x04)
scale *= -1;
/* 6-9: 2nd-4th digit */ /* 6-9: 2nd-4th digit */
for (pos = 6; pos < 10; pos++) for (pos = 6; pos < 10; pos++)
value = value * 10 + xgittoint(devc->buf[pos]); value = value * 10 + xgittoint(devc->buf[pos]);
@ -160,7 +205,8 @@ static void nma_process_line(const struct sr_dev_inst *sdi)
case 2: /* V AC */ case 2: /* V AC */
analog.unit = SR_UNIT_VOLT; analog.unit = SR_UNIT_VOLT;
analog.mqflags |= SR_MQFLAG_AC; analog.mqflags |= SR_MQFLAG_AC;
if (devc->type >= 3) analog.mqflags |= SR_MQFLAG_RMS; if (devc->type >= 3)
analog.mqflags |= SR_MQFLAG_RMS;
break; break;
case 3: /* V DC */ case 3: /* V DC */
analog.unit = SR_UNIT_VOLT; analog.unit = SR_UNIT_VOLT;
@ -172,7 +218,7 @@ static void nma_process_line(const struct sr_dev_inst *sdi)
case 5: /* Continuity */ case 5: /* Continuity */
analog.unit = SR_UNIT_BOOLEAN; analog.unit = SR_UNIT_BOOLEAN;
analog.mq = SR_MQ_CONTINUITY; analog.mq = SR_MQ_CONTINUITY;
/* TODO Continuity handling is a bit odd in sigrok. */ /* TODO: Continuity handling is a bit odd in libsigrok. */
break; break;
case 6: /* Degree Celsius */ case 6: /* Degree Celsius */
analog.unit = SR_UNIT_CELSIUS; analog.unit = SR_UNIT_CELSIUS;
@ -187,18 +233,19 @@ static void nma_process_line(const struct sr_dev_inst *sdi)
case 9: /* Current AC */ case 9: /* Current AC */
analog.unit = SR_UNIT_AMPERE; analog.unit = SR_UNIT_AMPERE;
analog.mqflags |= SR_MQFLAG_AC; analog.mqflags |= SR_MQFLAG_AC;
if (devc->type >= 3) analog.mqflags |= SR_MQFLAG_RMS; if (devc->type >= 3)
analog.mqflags |= SR_MQFLAG_RMS;
break; break;
case 0xA:/* Current TRMS, only type 5 */ case 0xa: /* Current TRMS, only type 5 */
analog.unit = SR_UNIT_AMPERE; analog.unit = SR_UNIT_AMPERE;
analog.mqflags |= (SR_MQFLAG_AC | SR_MQFLAG_DC | SR_MQFLAG_RMS); analog.mqflags |= (SR_MQFLAG_AC | SR_MQFLAG_DC | SR_MQFLAG_RMS);
break; break;
case 0x0B:/* Diode */ case 0xb: /* Diode */
analog.unit = SR_UNIT_VOLT; analog.unit = SR_UNIT_VOLT;
analog.mqflags |= (SR_MQFLAG_DIODE | SR_MQFLAG_DC); analog.mqflags |= (SR_MQFLAG_DIODE | SR_MQFLAG_DC);
break; break;
default: default:
sr_err("unknown mmode 0x%02x", mmode); sr_err("Unknown mmode: 0x%02x.", mmode);
break; break;
} }
@ -206,18 +253,18 @@ static void nma_process_line(const struct sr_dev_inst *sdi)
devstat = xgittoint(devc->buf[11]); devstat = xgittoint(devc->buf[11]);
switch (devstat) { switch (devstat) {
case 1: // Normal measurement case 1: /* Normal measurement */
break; break;
case 2: // Input loop (limit, reference values) case 2: /* Input loop (limit, reference values) */
break; break;
case 3: // TRANS/SENS case 3: /* TRANS/SENS */
break; break;
case 4: // Error case 4: /* Error */
sr_err("Device error. Fuse?"); // TODO: Wirklich abbrechen??? sr_err("Device error. Fuse?"); /* TODO: Really abort? */
devc->buflen = 0; devc->buflen = 0;
return; // Cannot continue. return; /* Cannot continue. */
default: default:
sr_err("Unknown device status 0x02x", devstat); sr_err("Unknown device status: 0x%02x", devstat);
break; break;
} }
@ -227,8 +274,10 @@ static void nma_process_line(const struct sr_dev_inst *sdi)
/* 0x80: PRINT TODO: Stop polling when discovered? */ /* 0x80: PRINT TODO: Stop polling when discovered? */
/* 0x40: EXTR */ /* 0x40: EXTR */
if (analog.mq == SR_MQ_CONTINUITY) { if (analog.mq == SR_MQ_CONTINUITY) {
if (flags & 0x20) value = 1.0; /* Beep */ if (flags & 0x20)
else value = 0.0; value = 1.0; /* Beep */
else
value = 0.0;
} }
/* 0x10: AVG */ /* 0x10: AVG */
/* 0x08: Diode */ /* 0x08: Diode */
@ -240,35 +289,37 @@ static void nma_process_line(const struct sr_dev_inst *sdi)
/* 14, 15 */ /* 14, 15 */
flags = (xgittoint(devc->buf[14]) << 8) | xgittoint(devc->buf[15]); flags = (xgittoint(devc->buf[14]) << 8) | xgittoint(devc->buf[15]);
if (!(flags & 0x80)) /* MAN Manual range */ if (!(flags & 0x80)) /* MAN: Manual range */
analog.mqflags |= SR_MQFLAG_AUTORANGE; analog.mqflags |= SR_MQFLAG_AUTORANGE;
if (flags & 0x40)/* LOBAT1 Low battery,measurement still within specs.*/ if (flags & 0x40) /* LOBATT1: Low battery, measurement still within specs */
devc->lowbatt = 1; devc->lowbatt = 1;
/* 0x20: PEAK */ /* 0x20: PEAK */
/* 0x10: COUNT */ /* 0x10: COUNT */
if (flags & 0x08) // HOLD if (flags & 0x08) /* HOLD */
analog.mqflags |= SR_MQFLAG_HOLD; analog.mqflags |= SR_MQFLAG_HOLD;
/* 0x04: LIMIT */ /* 0x04: LIMIT */
if (flags & 0x02) // MAX if (flags & 0x02) /* MAX */
analog.mqflags |= SR_MQFLAG_MAX; analog.mqflags |= SR_MQFLAG_MAX;
if (flags & 0x01) // MIN if (flags & 0x01) /* MIN */
analog.mqflags |= SR_MQFLAG_MIN; analog.mqflags |= SR_MQFLAG_MIN;
/* 16, 17 */ /* 16, 17 */
flags = (xgittoint(devc->buf[16]) << 8) | xgittoint(devc->buf[17]); flags = (xgittoint(devc->buf[16]) << 8) | xgittoint(devc->buf[17]);
/* 0xe0: undefined */ /* 0xe0: undefined */
if (flags & 0x10) { /* LOBATT2 Low battery, measurement inaccurate */ if (flags & 0x10) { /* LOBATT2: Low battery, measurement inaccurate */
devc->lowbatt = 2; devc->lowbatt = 2;
sr_warn("Low battery, measurement quality degraded!"); sr_warn("Low battery, measurement quality degraded!");
} }
/* 0x08: SCALED */ /* 0x08: SCALED */
/* 0x04: RATE (=lower resolution, allows higher rata rate up to 10/s. */ /* 0x04: RATE (=lower resolution, allows higher rata rate up to 10/s. */
/* 0x02: Current Clamp */ /* 0x02: Current clamp */
if (flags & 0x01) { /* dB */ if (flags & 0x01) { /* dB */
/*
* TODO: The Norma has an adjustable dB reference value. If
* changed from default, this is not correct.
*/
if (analog.unit == SR_UNIT_VOLT) if (analog.unit == SR_UNIT_VOLT)
analog.unit = SR_UNIT_DECIBEL_VOLT; analog.unit = SR_UNIT_DECIBEL_VOLT;
/* TODO: The Norma has an adjustable dB reference value. If
* changed from default, this is not correct. */
else else
analog.unit = SR_UNIT_UNITLESS; analog.unit = SR_UNIT_UNITLESS;
} }
@ -284,21 +335,20 @@ static void nma_process_line(const struct sr_dev_inst *sdi)
/* 4: Flags. Evaluating this after setting value! */ /* 4: Flags. Evaluating this after setting value! */
flags = xgittoint(devc->buf[4]); flags = xgittoint(devc->buf[4]);
if (flags & 0x04) { // Invalid value if (flags & 0x04) /* Invalid value */
value = NAN; value = NAN;
} else if (flags & 0x01) /* Overload */
else if (flags & 0x01) { // Overload
value = INFINITY; value = INFINITY;
} if (flags & 0x02) { /* Duplicate value, has been sent before. */
if (flags & 0x02) { // Duplicate value, has been sent before.
sr_spew("Duplicate value, dismissing!"); sr_spew("Duplicate value, dismissing!");
devc->buflen = 0; devc->buflen = 0;
return; return;
} }
sr_spew("range=%d/scale=%f/value=%f",range,(double)scale,(double)value); sr_spew("range=%d/scale=%f/value=%f", range,
(double)scale, (double)value);
/* Finish and send packet */ /* Finish and send packet. */
analog.probes = sdi->probes; analog.probes = sdi->probes;
analog.num_samples = 1; analog.num_samples = 1;
analog.data = &value; analog.data = &value;
@ -308,12 +358,11 @@ static void nma_process_line(const struct sr_dev_inst *sdi)
packet.payload = &analog; packet.payload = &analog;
sr_session_send(devc->cb_data, &packet); sr_session_send(devc->cb_data, &packet);
/* Finish processing */ /* Finish processing. */
devc->num_samples++; devc->num_samples++;
devc->buflen = 0; devc->buflen = 0;
} }
SR_PRIV int norma_dmm_receive_data(int fd, int revents, void *cb_data) SR_PRIV int norma_dmm_receive_data(int fd, int revents, void *cb_data)
{ {
struct sr_dev_inst *sdi; struct sr_dev_inst *sdi;
@ -321,6 +370,7 @@ SR_PRIV int norma_dmm_receive_data(int fd, int revents, void *cb_data)
struct sr_serial_dev_inst *serial; struct sr_serial_dev_inst *serial;
int len; int len;
gboolean terminating; gboolean terminating;
gdouble elapsed_s;
(void)fd; (void)fd;
@ -340,8 +390,10 @@ SR_PRIV int norma_dmm_receive_data(int fd, int revents, void *cb_data)
devc->buflen += len; devc->buflen += len;
*(devc->buf + devc->buflen) = '\0'; *(devc->buf + devc->buflen) = '\0';
if (*(devc->buf + devc->buflen - 1) == '\n') { if (*(devc->buf + devc->buflen - 1) == '\n') {
/* TODO: According to specs, should be \r, but /*
* then we'd have to get rid of the \n */ * TODO: According to specs, should be \r, but
* then we'd have to get rid of the \n.
*/
devc->last_req_pending = FALSE; devc->last_req_pending = FALSE;
nma_process_line(sdi); nma_process_line(sdi);
break; break;
@ -349,7 +401,7 @@ SR_PRIV int norma_dmm_receive_data(int fd, int revents, void *cb_data)
} }
} }
// If number of samples or time limit reached, stop aquisition. /* If number of samples or time limit reached, stop aquisition. */
terminating = FALSE; terminating = FALSE;
if (devc->limit_samples && (devc->num_samples >= devc->limit_samples)) { if (devc->limit_samples && (devc->num_samples >= devc->limit_samples)) {
sdi->driver->dev_acquisition_stop(sdi, cb_data); sdi->driver->dev_acquisition_stop(sdi, cb_data);
@ -357,14 +409,14 @@ SR_PRIV int norma_dmm_receive_data(int fd, int revents, void *cb_data)
} }
if (devc->limit_msec) { if (devc->limit_msec) {
gdouble elapsed_s = g_timer_elapsed(devc->elapsed_msec, NULL); elapsed_s = g_timer_elapsed(devc->elapsed_msec, NULL);
if ((elapsed_s * 1000) >= devc->limit_msec) { if ((elapsed_s * 1000) >= devc->limit_msec) {
sdi->driver->dev_acquisition_stop(sdi, cb_data); sdi->driver->dev_acquisition_stop(sdi, cb_data);
terminating = TRUE; terminating = TRUE;
} }
} }
// Request next package /* Request next package. */
if (!terminating && !devc->last_req_pending) { if (!terminating && !devc->last_req_pending) {
if (nma_send_req(sdi, NMADMM_REQ_STATUS, NULL) != SR_OK) if (nma_send_req(sdi, NMADMM_REQ_STATUS, NULL) != SR_OK)
return FALSE; return FALSE;
@ -372,41 +424,3 @@ SR_PRIV int norma_dmm_receive_data(int fd, int revents, void *cb_data)
return TRUE; return TRUE;
} }
static int nma_send_req(const struct sr_dev_inst *sdi, enum nmadmm_req_t req,
char* params)
{
struct sr_serial_dev_inst *serial;
struct dev_context *devc;
char buf[NMADMM_BUFSIZE];
int len;
if (!sdi || !(serial = sdi->conn) || !(devc = sdi->priv))
return SR_ERR_BUG;
len = snprintf(buf, sizeof(buf), "%s%s\r\n",
nmadmm_requests[req].reqstr, params?params:"");
sr_spew("Sending request: '%s'", buf);
devc->last_req = req;
devc->last_req_pending = TRUE;
if (serial_write(serial, buf, len) == -1) {
sr_err("Unable to send request: %d %s.",
errno, strerror(errno));
devc->last_req_pending = FALSE;
return SR_ERR;
}
return SR_OK;
}
SR_PRIV const struct nmadmm_req nmadmm_requests[] = {
{ NMADMM_REQ_IDN, "IDN?"},
{ NMADMM_REQ_IDN, "STATUS?"},
{ 0, NULL }
};

View File

@ -21,6 +21,10 @@
#define LIBSIGROK_HARDWARE_NORMA_DMM_PROTOCOL_H #define LIBSIGROK_HARDWARE_NORMA_DMM_PROTOCOL_H
#include <stdint.h> #include <stdint.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <math.h>
#include <glib.h> #include <glib.h>
#include "libsigrok.h" #include "libsigrok.h"
#include "libsigrok-internal.h" #include "libsigrok-internal.h"
@ -36,17 +40,16 @@
#define NMADMM_BUFSIZE 256 #define NMADMM_BUFSIZE 256
/** Norma DMM request types (used ones only, the multimeters support about 50). /** Norma DMM request types (used ones only, the DMMs support about 50). */
*/ enum {
enum nmadmm_req_t {
NMADMM_REQ_IDN = 0, /**< Request identity */ NMADMM_REQ_IDN = 0, /**< Request identity */
NMADMM_REQ_STATUS, /**< Request device status (value + ...) */ NMADMM_REQ_STATUS, /**< Request device status (value + ...) */
}; };
/** Defines requests used to communicate with device. */ /** Defines requests used to communicate with device. */
struct nmadmm_req { struct nmadmm_req {
enum nmadmm_req_t req_t; /** Request type. */ int req_type; /**< Request type. */
const char* reqstr; /** Request string */ const char *req_str; /**< Request string. */
}; };
/** Strings for requests. */ /** Strings for requests. */
@ -57,6 +60,7 @@ struct dev_context {
/* Model-specific information */ /* Model-specific information */
char *version; /**< Version string */ char *version; /**< Version string */
int type; /**< DM9x0, e.g. 5 = DM950 */ int type; /**< DM9x0, e.g. 5 = DM950 */
/* Acquisition settings */ /* Acquisition settings */
uint64_t limit_samples; /**< Target number of samples */ uint64_t limit_samples; /**< Target number of samples */
uint64_t limit_msec; /**< Target sampling time */ uint64_t limit_msec; /**< Target sampling time */
@ -65,13 +69,14 @@ struct dev_context {
void *cb_data; void *cb_data;
/* Operational state */ /* Operational state */
enum nmadmm_req_t last_req; /**< Last request. */ int last_req; /**< Last request. */
gboolean last_req_pending; /**< Last request not answered yet. */ gboolean last_req_pending; /**< Last request not answered yet. */
int lowbatt; /**< Low battery. 1=low, 2=critical. */ int lowbatt; /**< Low battery. 1=low, 2=critical. */
/* Temporary state across callbacks */ /* Temporary state across callbacks */
uint64_t num_samples; /**< Current #samples. */ uint64_t num_samples; /**< Current #samples. */
GTimer* elapsed_msec; /**< Used for sampling with limit_msec*/ GTimer *elapsed_msec; /**< Used for limit_msec */
unsigned char buf[NMADMM_BUFSIZE]; /**< Buffer for read callback */ uint8_t buf[NMADMM_BUFSIZE]; /**< Buffer for read callback */
int buflen; /**< Data len in buf */ int buflen; /**< Data len in buf */
}; };