2 * This file is part of the libsigrok project.
4 * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
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/>.
23 #define MAX_PACKET_LEN 22
25 /* Flags passed from the DMM. */
27 gboolean is_low_batt, is_decibel, is_duty_cycle, is_hertz, is_amp;
28 gboolean is_beep, is_ohm, is_fahrenheit, is_celsius, is_capacitance;
29 gboolean is_diode, is_volt, is_dc, is_ac;
32 struct bm850_command {
42 struct brymen_header {
56 * We only have one command because we only support the BM-857. However, the
57 * driver is easily extensible to support more models, as the protocols are
61 BM_CMD_REQUEST_READING = 0x00,
64 static int bm_send_command(uint8_t command, uint8_t arg1, uint8_t arg2,
65 struct sr_serial_dev_inst *serial)
67 struct bm850_command cmdout;
75 cmdout.checksum = arg1 ^ arg2;
79 /* TODO: How to compute the checksum? Hardware seems to ignore it. */
81 /* Request reading. */
82 written = serial_write_blocking(serial, &cmdout, sizeof(cmdout),
83 serial_timeout(serial, sizeof(cmdout)));
84 if (written != sizeof(cmdout))
90 SR_PRIV int brymen_packet_request(struct sr_serial_dev_inst *serial)
92 return bm_send_command(BM_CMD_REQUEST_READING, 0, 0, serial);
95 SR_PRIV int brymen_packet_length(const uint8_t *buf, int *len)
97 struct brymen_header *hdr;
104 /* Did we receive a complete header yet? */
105 if (buflen < sizeof(*hdr))
106 return PACKET_NEED_MORE_DATA;
108 if (hdr->dle != 0x10 || hdr->stx != 0x02)
109 return PACKET_INVALID_HEADER;
111 /* Our packet includes the header, the payload, and the tail. */
112 packet_len = sizeof(*hdr) + hdr->len + sizeof(struct brymen_tail);
114 /* In case we pick up an invalid header, limit our search. */
115 if (packet_len > MAX_PACKET_LEN) {
116 sr_spew("Header specifies an invalid payload length: %i.",
118 return PACKET_INVALID_HEADER;
122 sr_spew("Expecting a %d-byte packet.", *len);
123 return PACKET_HEADER_OK;
126 SR_PRIV gboolean brymen_packet_is_valid(const uint8_t *buf)
128 struct brymen_header *hdr;
129 struct brymen_tail *tail;
134 payload = (uint8_t *)(buf + sizeof(struct brymen_header));
137 tail = (void *)(payload + hdr->len);
139 for (i = 0; i< hdr->len; i++)
140 chksum ^= payload[i];
142 if (tail->checksum != chksum) {
143 sr_dbg("Packet has invalid checksum 0x%.2x. Expected 0x%.2x.",
144 chksum, tail->checksum);
151 static int parse_value(const char *strbuf, int len, float *floatval)
156 if (strstr(strbuf, "OL")) {
157 sr_dbg("Overlimit.");
158 *floatval = INFINITY;
162 memset(str, 0, sizeof(str));
163 /* Spaces may interfere with parsing the exponent. Strip them. */
164 for (s = 0, d = 0; s < len; s++) {
165 if (strbuf[s] != ' ')
166 str[d++] = strbuf[s];
168 if (sr_atof_ascii(str, floatval) != SR_OK)
174 static void parse_flags(const uint8_t *buf, struct brymen_flags *info)
176 info->is_low_batt = (buf[4 + 3] & (1 << 7)) != 0;
178 info->is_decibel = (buf[4 + 1] & (1 << 5)) != 0;
179 info->is_duty_cycle = (buf[4 + 1] & (1 << 3)) != 0;
180 info->is_hertz = (buf[4 + 1] & (1 << 2)) != 0;
181 info->is_amp = (buf[4 + 1] & (1 << 1)) != 0;
182 info->is_beep = (buf[4 + 1] & (1 << 0)) != 0;
184 info->is_ohm = (buf[4 + 0] & (1 << 7)) != 0;
185 info->is_fahrenheit = (buf[4 + 0] & (1 << 6)) != 0;
186 info->is_celsius = (buf[4 + 0] & (1 << 5)) != 0;
187 info->is_diode = (buf[4 + 0] & (1 << 4)) != 0;
188 info->is_capacitance = (buf[4 + 0] & (1 << 3)) != 0;
189 info->is_volt = (buf[4 + 0] & (1 << 2)) != 0;
190 info->is_dc = (buf[4 + 0] & (1 << 1)) != 0;
191 info->is_ac = (buf[4 + 0] & (1 << 0)) != 0;
194 SR_PRIV int brymen_parse(const uint8_t *buf, float *floatval,
195 struct sr_datafeed_analog *analog, void *info)
197 struct brymen_flags flags;
198 struct brymen_header *hdr;
205 bfunc = (uint8_t *)(buf + sizeof(struct brymen_header));
207 analog->meaning->mqflags = 0;
209 /* Give some debug info about the package. */
210 asciilen = hdr->len - 4;
211 sr_dbg("DMM flags: %.2x %.2x %.2x %.2x",
212 bfunc[3], bfunc[2], bfunc[1], bfunc[0]);
213 /* Value is an ASCII string. */
214 sr_dbg("DMM packet: \"%.*s\"", asciilen, bfunc + 4);
216 parse_flags(buf, &flags);
217 if (parse_value((const char *)(bfunc + 4), asciilen, floatval) != SR_OK)
221 analog->meaning->mq = SR_MQ_VOLTAGE;
222 analog->meaning->unit = SR_UNIT_VOLT;
225 analog->meaning->mq = SR_MQ_CURRENT;
226 analog->meaning->unit = SR_UNIT_AMPERE;
230 analog->meaning->mq = SR_MQ_CONTINUITY;
232 analog->meaning->mq = SR_MQ_RESISTANCE;
233 analog->meaning->unit = SR_UNIT_OHM;
235 if (flags.is_hertz) {
236 analog->meaning->mq = SR_MQ_FREQUENCY;
237 analog->meaning->unit = SR_UNIT_HERTZ;
239 if (flags.is_duty_cycle) {
240 analog->meaning->mq = SR_MQ_DUTY_CYCLE;
241 analog->meaning->unit = SR_UNIT_PERCENTAGE;
243 if (flags.is_capacitance) {
244 analog->meaning->mq = SR_MQ_CAPACITANCE;
245 analog->meaning->unit = SR_UNIT_FARAD;
247 if (flags.is_fahrenheit) {
248 analog->meaning->mq = SR_MQ_TEMPERATURE;
249 analog->meaning->unit = SR_UNIT_FAHRENHEIT;
251 if (flags.is_celsius) {
252 analog->meaning->mq = SR_MQ_TEMPERATURE;
253 analog->meaning->unit = SR_UNIT_CELSIUS;
255 if (flags.is_capacitance) {
256 analog->meaning->mq = SR_MQ_CAPACITANCE;
257 analog->meaning->unit = SR_UNIT_FARAD;
261 * The high-end Brymen models have a configurable reference impedance.
262 * When the reference impedance is changed, the DMM sends one packet
263 * with the value of the new reference impedance. Both decibel and ohm
264 * flags are set in this case, so we must be careful to correctly
265 * identify the value as ohm, not dBmW.
267 if (flags.is_decibel && !flags.is_ohm) {
268 analog->meaning->mq = SR_MQ_POWER;
269 analog->meaning->unit = SR_UNIT_DECIBEL_MW;
271 * For some reason, dBm measurements are sent by the multimeter
272 * with a value three orders of magnitude smaller than the
279 analog->meaning->mqflags |= SR_MQFLAG_DIODE;
280 /* We can have both AC+DC in a single measurement. */
282 analog->meaning->mqflags |= SR_MQFLAG_AC;
284 analog->meaning->mqflags |= SR_MQFLAG_DC;
286 if (flags.is_low_batt)
287 sr_info("Low battery!");