2012-11-26 23:09:05 +00:00
|
|
|
/*
|
2013-02-02 10:35:20 +00:00
|
|
|
* This file is part of the libsigrok project.
|
2012-11-26 23:09:05 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "protocol.h"
|
|
|
|
|
2013-02-02 10:35:20 +00:00
|
|
|
#define MAX_PACKET_LEN 22
|
|
|
|
|
|
|
|
/* Flags passed from the DMM. */
|
2012-11-26 23:09:05 +00:00
|
|
|
struct brymen_flags {
|
2013-02-02 10:35:20 +00:00
|
|
|
gboolean is_low_batt, is_decibel, is_duty_cycle, is_hertz, is_amp;
|
|
|
|
gboolean is_beep, is_ohm, is_fahrenheit, is_celsius, is_capacitance;
|
|
|
|
gboolean is_diode, is_volt, is_dc, is_ac;
|
2012-11-26 23:09:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct bm850_command {
|
|
|
|
uint8_t dle;
|
|
|
|
uint8_t stx;
|
|
|
|
uint8_t cmd;
|
|
|
|
uint8_t arg[2];
|
|
|
|
uint8_t checksum;
|
|
|
|
uint8_t dle2;
|
|
|
|
uint8_t etx;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct brymen_header {
|
|
|
|
uint8_t dle;
|
|
|
|
uint8_t stx;
|
|
|
|
uint8_t cmd;
|
|
|
|
uint8_t len;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct brymen_tail {
|
|
|
|
uint8_t checksum;
|
|
|
|
uint8_t dle;
|
|
|
|
uint8_t etx;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We only have one command because we only support the BM-857. However, the
|
2013-02-02 10:35:20 +00:00
|
|
|
* driver is easily extensible to support more models, as the protocols are
|
|
|
|
* very similar.
|
2012-11-26 23:09:05 +00:00
|
|
|
*/
|
|
|
|
enum {
|
|
|
|
BM_CMD_REQUEST_READING = 0x00,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int bm_send_command(uint8_t command, uint8_t arg1, uint8_t arg2,
|
|
|
|
struct sr_serial_dev_inst *serial)
|
|
|
|
{
|
2013-02-02 10:35:20 +00:00
|
|
|
struct bm850_command cmdout;
|
|
|
|
int written;
|
|
|
|
|
|
|
|
cmdout.dle = 0x10;
|
|
|
|
cmdout.stx = 0x02;
|
|
|
|
cmdout.cmd = command;
|
|
|
|
cmdout.arg[0] = arg1;
|
|
|
|
cmdout.arg[1] = arg2;
|
|
|
|
cmdout.checksum = arg1 ^ arg2;
|
|
|
|
cmdout.dle2 = 0x10;
|
|
|
|
cmdout.etx = 0x03;
|
|
|
|
|
|
|
|
/* TODO: How to compute the checksum? Hardware seems to ignore it. */
|
|
|
|
|
|
|
|
/* Request reading. */
|
|
|
|
written = serial_write(serial, &cmdout, sizeof(cmdout));
|
|
|
|
if (written != sizeof(cmdout))
|
|
|
|
return SR_ERR;
|
|
|
|
|
|
|
|
return SR_OK;
|
2012-11-26 23:09:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SR_PRIV int brymen_packet_request(struct sr_serial_dev_inst *serial)
|
|
|
|
{
|
|
|
|
return bm_send_command(BM_CMD_REQUEST_READING, 0, 0, serial);
|
|
|
|
}
|
|
|
|
|
|
|
|
SR_PRIV int brymen_packet_length(const uint8_t *buf, int *len)
|
|
|
|
{
|
|
|
|
struct brymen_header *hdr;
|
|
|
|
int packet_len;
|
2013-02-02 10:35:20 +00:00
|
|
|
size_t buflen;
|
|
|
|
|
|
|
|
buflen = *len;
|
|
|
|
hdr = (void *)buf;
|
|
|
|
|
2012-11-26 23:09:05 +00:00
|
|
|
/* Did we receive a complete header yet? */
|
2013-02-02 10:35:20 +00:00
|
|
|
if (buflen < sizeof(*hdr))
|
2012-11-26 23:09:05 +00:00
|
|
|
return PACKET_NEED_MORE_DATA;
|
2013-02-02 10:35:20 +00:00
|
|
|
|
2012-11-26 23:09:05 +00:00
|
|
|
if (hdr->dle != 0x10 || hdr->stx != 0x02)
|
|
|
|
return PACKET_INVALID_HEADER;
|
2013-02-02 10:35:20 +00:00
|
|
|
|
|
|
|
/* Our packet includes the header, the payload, and the tail. */
|
2012-11-26 23:09:05 +00:00
|
|
|
packet_len = sizeof(*hdr) + hdr->len + sizeof(struct brymen_tail);
|
2013-02-02 10:35:20 +00:00
|
|
|
|
|
|
|
/* In case we pick up an invalid header, limit our search. */
|
|
|
|
if (packet_len > MAX_PACKET_LEN) {
|
2012-11-26 23:09:05 +00:00
|
|
|
sr_spew("Header specifies an invalid payload length: %i.",
|
|
|
|
hdr->len);
|
|
|
|
return PACKET_INVALID_HEADER;
|
|
|
|
}
|
2013-02-02 10:35:20 +00:00
|
|
|
|
2012-11-26 23:09:05 +00:00
|
|
|
*len = packet_len;
|
|
|
|
sr_spew("Expecting a %d-byte packet.", *len);
|
|
|
|
return PACKET_HEADER_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
SR_PRIV gboolean brymen_packet_is_valid(const uint8_t *buf)
|
|
|
|
{
|
|
|
|
struct brymen_header *hdr;
|
|
|
|
struct brymen_tail *tail;
|
|
|
|
int i;
|
|
|
|
uint8_t chksum = 0;
|
2013-02-02 10:35:20 +00:00
|
|
|
uint8_t *payload;
|
2012-11-26 23:09:05 +00:00
|
|
|
|
2013-02-02 10:35:20 +00:00
|
|
|
payload = (uint8_t *)(buf + sizeof(struct brymen_header));
|
|
|
|
|
|
|
|
hdr = (void *)buf;
|
|
|
|
tail = (void *)(payload + hdr->len);
|
2012-11-26 23:09:05 +00:00
|
|
|
|
|
|
|
for (i = 0; i< hdr->len; i++)
|
|
|
|
chksum ^= payload[i];
|
|
|
|
|
|
|
|
if (tail->checksum != chksum) {
|
2013-02-02 10:35:20 +00:00
|
|
|
sr_dbg("Packet has invalid checksum 0x%.2x. Expected 0x%.2x.",
|
2012-11-26 23:09:05 +00:00
|
|
|
chksum, tail->checksum);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-02-02 10:35:20 +00:00
|
|
|
static int parse_value(const char *strbuf, int len, float *floatval)
|
2012-11-26 23:09:05 +00:00
|
|
|
{
|
|
|
|
int s, d;
|
|
|
|
char str[32];
|
|
|
|
|
|
|
|
if (strstr(strbuf, "OL")) {
|
|
|
|
sr_dbg("Overlimit.");
|
|
|
|
*floatval = INFINITY;
|
|
|
|
return SR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(str, 0, sizeof(str));
|
2014-01-21 16:02:27 +00:00
|
|
|
/* Spaces may interfere with parsing the exponent. Strip them. */
|
|
|
|
for (s = 0, d = 0; s < len; s++) {
|
2012-11-26 23:09:05 +00:00
|
|
|
if (strbuf[s] != ' ')
|
|
|
|
str[d++] = strbuf[s];
|
2014-01-21 16:02:27 +00:00
|
|
|
}
|
|
|
|
if (sr_atof_ascii(str, floatval) != SR_OK)
|
|
|
|
return SR_ERR;
|
2012-11-26 23:09:05 +00:00
|
|
|
|
|
|
|
return SR_OK;
|
|
|
|
}
|
2013-02-02 10:35:20 +00:00
|
|
|
|
2012-11-26 23:09:05 +00:00
|
|
|
static void parse_flags(const uint8_t *buf, struct brymen_flags *info)
|
|
|
|
{
|
2013-02-02 10:35:20 +00:00
|
|
|
info->is_low_batt = (buf[4 + 3] & (1 << 7)) != 0;
|
|
|
|
|
|
|
|
info->is_decibel = (buf[4 + 1] & (1 << 5)) != 0;
|
|
|
|
info->is_duty_cycle = (buf[4 + 1] & (1 << 3)) != 0;
|
|
|
|
info->is_hertz = (buf[4 + 1] & (1 << 2)) != 0;
|
|
|
|
info->is_amp = (buf[4 + 1] & (1 << 1)) != 0;
|
|
|
|
info->is_beep = (buf[4 + 1] & (1 << 0)) != 0;
|
|
|
|
|
|
|
|
info->is_ohm = (buf[4 + 0] & (1 << 7)) != 0;
|
|
|
|
info->is_fahrenheit = (buf[4 + 0] & (1 << 6)) != 0;
|
|
|
|
info->is_celsius = (buf[4 + 0] & (1 << 5)) != 0;
|
|
|
|
info->is_diode = (buf[4 + 0] & (1 << 4)) != 0;
|
|
|
|
info->is_capacitance = (buf[4 + 0] & (1 << 3)) != 0;
|
|
|
|
info->is_volt = (buf[4 + 0] & (1 << 2)) != 0;
|
|
|
|
info->is_dc = (buf[4 + 0] & (1 << 1)) != 0;
|
|
|
|
info->is_ac = (buf[4 + 0] & (1 << 0)) != 0;
|
2012-11-26 23:09:05 +00:00
|
|
|
}
|
|
|
|
|
2014-01-21 16:02:27 +00:00
|
|
|
SR_PRIV int brymen_parse(const uint8_t *buf, float *floatval,
|
|
|
|
struct sr_datafeed_analog *analog, void *info)
|
2012-11-26 23:09:05 +00:00
|
|
|
{
|
|
|
|
struct brymen_flags flags;
|
2013-02-02 10:35:20 +00:00
|
|
|
struct brymen_header *hdr;
|
|
|
|
uint8_t *bfunc;
|
2012-11-26 23:09:05 +00:00
|
|
|
int asciilen;
|
|
|
|
|
|
|
|
(void)info;
|
2013-02-02 10:35:20 +00:00
|
|
|
|
|
|
|
hdr = (void *)buf;
|
|
|
|
bfunc = (uint8_t *)(buf + sizeof(struct brymen_header));
|
|
|
|
|
2012-11-26 23:09:05 +00:00
|
|
|
analog->mqflags = 0;
|
|
|
|
|
2013-02-02 10:35:20 +00:00
|
|
|
/* Give some debug info about the package. */
|
2012-11-26 23:09:05 +00:00
|
|
|
asciilen = hdr->len - 4;
|
|
|
|
sr_dbg("DMM flags: %.2x %.2x %.2x %.2x",
|
|
|
|
bfunc[3], bfunc[2], bfunc[1], bfunc[0]);
|
2013-02-02 10:35:20 +00:00
|
|
|
/* Value is an ASCII string. */
|
2012-11-26 23:09:05 +00:00
|
|
|
sr_dbg("DMM packet: \"%.*s\"", asciilen, bfunc + 4);
|
|
|
|
|
|
|
|
parse_flags(buf, &flags);
|
2014-01-21 16:02:27 +00:00
|
|
|
if (parse_value((const char *)(bfunc + 4), asciilen, floatval) != SR_OK)
|
|
|
|
return SR_ERR;
|
2012-11-26 23:09:05 +00:00
|
|
|
|
2013-02-02 10:35:20 +00:00
|
|
|
if (flags.is_volt) {
|
2012-11-26 23:09:05 +00:00
|
|
|
analog->mq = SR_MQ_VOLTAGE;
|
|
|
|
analog->unit = SR_UNIT_VOLT;
|
|
|
|
}
|
2013-02-02 10:35:20 +00:00
|
|
|
if (flags.is_amp) {
|
2012-11-26 23:09:05 +00:00
|
|
|
analog->mq = SR_MQ_CURRENT;
|
|
|
|
analog->unit = SR_UNIT_AMPERE;
|
|
|
|
}
|
2013-02-02 10:35:20 +00:00
|
|
|
if (flags.is_ohm) {
|
|
|
|
if (flags.is_beep)
|
2012-11-26 23:09:05 +00:00
|
|
|
analog->mq = SR_MQ_CONTINUITY;
|
|
|
|
else
|
|
|
|
analog->mq = SR_MQ_RESISTANCE;
|
|
|
|
analog->unit = SR_UNIT_OHM;
|
|
|
|
}
|
2013-02-02 10:35:20 +00:00
|
|
|
if (flags.is_hertz) {
|
2012-11-26 23:09:05 +00:00
|
|
|
analog->mq = SR_MQ_FREQUENCY;
|
|
|
|
analog->unit = SR_UNIT_HERTZ;
|
|
|
|
}
|
2013-02-02 10:35:20 +00:00
|
|
|
if (flags.is_duty_cycle) {
|
2012-11-26 23:09:05 +00:00
|
|
|
analog->mq = SR_MQ_DUTY_CYCLE;
|
|
|
|
analog->unit = SR_UNIT_PERCENTAGE;
|
|
|
|
}
|
2013-02-02 10:35:20 +00:00
|
|
|
if (flags.is_capacitance) {
|
2012-11-26 23:09:05 +00:00
|
|
|
analog->mq = SR_MQ_CAPACITANCE;
|
|
|
|
analog->unit = SR_UNIT_FARAD;
|
|
|
|
}
|
2013-02-02 10:35:20 +00:00
|
|
|
if (flags.is_fahrenheit) {
|
2012-11-26 23:09:05 +00:00
|
|
|
analog->mq = SR_MQ_TEMPERATURE;
|
|
|
|
analog->unit = SR_UNIT_FAHRENHEIT;
|
|
|
|
}
|
2013-02-02 10:35:20 +00:00
|
|
|
if (flags.is_celsius) {
|
2012-11-26 23:09:05 +00:00
|
|
|
analog->mq = SR_MQ_TEMPERATURE;
|
|
|
|
analog->unit = SR_UNIT_CELSIUS;
|
|
|
|
}
|
2013-02-02 10:35:20 +00:00
|
|
|
if (flags.is_capacitance) {
|
2012-11-26 23:09:05 +00:00
|
|
|
analog->mq = SR_MQ_CAPACITANCE;
|
|
|
|
analog->unit = SR_UNIT_FARAD;
|
|
|
|
}
|
2013-02-02 10:35:20 +00:00
|
|
|
|
2012-11-26 23:09:05 +00:00
|
|
|
/*
|
2013-02-02 10:35:20 +00:00
|
|
|
* The high-end Brymen models have a configurable reference impedance.
|
2012-11-26 23:09:05 +00:00
|
|
|
* When the reference impedance is changed, the DMM sends one packet
|
|
|
|
* with the value of the new reference impedance. Both decibel and ohm
|
|
|
|
* flags are set in this case, so we must be careful to correctly
|
2013-02-02 10:35:20 +00:00
|
|
|
* identify the value as ohm, not dBmW.
|
2012-11-26 23:09:05 +00:00
|
|
|
*/
|
2013-02-02 10:35:20 +00:00
|
|
|
if (flags.is_decibel && !flags.is_ohm) {
|
2012-11-26 23:09:05 +00:00
|
|
|
analog->mq = SR_MQ_POWER;
|
|
|
|
analog->unit = SR_UNIT_DECIBEL_MW;
|
|
|
|
/*
|
|
|
|
* For some reason, dBm measurements are sent by the multimeter
|
|
|
|
* with a value three orders of magnitude smaller than the
|
|
|
|
* displayed value.
|
2013-02-02 10:35:20 +00:00
|
|
|
*/
|
2012-11-26 23:09:05 +00:00
|
|
|
*floatval *= 1000;
|
|
|
|
}
|
|
|
|
|
2013-02-02 10:35:20 +00:00
|
|
|
if (flags.is_diode)
|
2012-11-26 23:09:05 +00:00
|
|
|
analog->mqflags |= SR_MQFLAG_DIODE;
|
2013-02-02 10:35:20 +00:00
|
|
|
/* We can have both AC+DC in a single measurement. */
|
|
|
|
if (flags.is_ac)
|
2012-11-26 23:09:05 +00:00
|
|
|
analog->mqflags |= SR_MQFLAG_AC;
|
2013-02-02 10:35:20 +00:00
|
|
|
if (flags.is_dc)
|
2012-11-26 23:09:05 +00:00
|
|
|
analog->mqflags |= SR_MQFLAG_DC;
|
|
|
|
|
2013-02-02 10:35:20 +00:00
|
|
|
if (flags.is_low_batt)
|
2012-11-26 23:09:05 +00:00
|
|
|
sr_info("Low battery!");
|
|
|
|
|
|
|
|
return SR_OK;
|
2013-02-02 10:35:20 +00:00
|
|
|
}
|