2012-10-21 01:39:37 +00:00
|
|
|
/*
|
2013-04-23 20:24:30 +00:00
|
|
|
* This file is part of the libsigrok project.
|
2012-10-21 01:39:37 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
|
2012-12-01 18:28:36 +00:00
|
|
|
* Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
|
2012-10-21 01:39:37 +00:00
|
|
|
*
|
|
|
|
* 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 <stdlib.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2012-12-16 20:23:49 +00:00
|
|
|
#include <glib.h>
|
2012-10-27 19:03:02 +00:00
|
|
|
#include "libsigrok.h"
|
|
|
|
#include "libsigrok-internal.h"
|
|
|
|
#include "protocol.h"
|
2012-10-21 01:39:37 +00:00
|
|
|
|
2012-11-05 22:25:59 +00:00
|
|
|
static void log_dmm_packet(const uint8_t *buf)
|
|
|
|
{
|
|
|
|
sr_dbg("DMM packet: %02x %02x %02x %02x %02x %02x %02x"
|
|
|
|
" %02x %02x %02x %02x %02x %02x %02x",
|
|
|
|
buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
|
|
|
|
buf[7], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13]);
|
|
|
|
}
|
|
|
|
|
2013-01-23 02:40:44 +00:00
|
|
|
static void handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi,
|
2012-12-01 18:28:36 +00:00
|
|
|
int dmm, void *info)
|
2012-10-21 01:39:37 +00:00
|
|
|
{
|
2012-11-01 14:58:09 +00:00
|
|
|
float floatval;
|
2012-10-21 01:39:37 +00:00
|
|
|
struct sr_datafeed_packet packet;
|
2013-02-02 11:22:18 +00:00
|
|
|
struct sr_datafeed_analog analog;
|
2013-01-23 02:40:44 +00:00
|
|
|
struct dev_context *devc;
|
2012-10-21 01:39:37 +00:00
|
|
|
|
2012-11-05 22:25:59 +00:00
|
|
|
log_dmm_packet(buf);
|
2013-01-23 02:40:44 +00:00
|
|
|
devc = sdi->priv;
|
2012-11-05 22:25:59 +00:00
|
|
|
|
2013-02-02 11:22:18 +00:00
|
|
|
memset(&analog, 0, sizeof(struct sr_datafeed_analog));
|
2012-10-27 19:03:02 +00:00
|
|
|
|
2013-02-02 11:22:18 +00:00
|
|
|
analog.probes = sdi->probes;
|
|
|
|
analog.num_samples = 1;
|
|
|
|
analog.mq = -1;
|
2012-10-21 01:39:37 +00:00
|
|
|
|
2013-02-02 11:22:18 +00:00
|
|
|
dmms[dmm].packet_parse(buf, &floatval, &analog, info);
|
|
|
|
analog.data = &floatval;
|
2012-10-30 04:42:10 +00:00
|
|
|
|
2012-12-16 20:23:49 +00:00
|
|
|
/* If this DMM needs additional handling, call the resp. function. */
|
2012-12-02 13:22:15 +00:00
|
|
|
if (dmms[dmm].dmm_details)
|
2013-02-02 11:22:18 +00:00
|
|
|
dmms[dmm].dmm_details(&analog, info);
|
2012-10-21 01:39:37 +00:00
|
|
|
|
2013-02-02 11:22:18 +00:00
|
|
|
if (analog.mq != -1) {
|
2012-10-21 01:39:37 +00:00
|
|
|
/* Got a measurement. */
|
|
|
|
packet.type = SR_DF_ANALOG;
|
2013-02-02 11:22:18 +00:00
|
|
|
packet.payload = &analog;
|
2012-10-21 01:39:37 +00:00
|
|
|
sr_session_send(devc->cb_data, &packet);
|
|
|
|
devc->num_samples++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-23 02:40:44 +00:00
|
|
|
static void handle_new_data(struct sr_dev_inst *sdi, int dmm, void *info)
|
2012-10-21 01:39:37 +00:00
|
|
|
{
|
2013-01-23 02:40:44 +00:00
|
|
|
struct dev_context *devc;
|
2012-10-27 19:03:02 +00:00
|
|
|
int len, i, offset = 0;
|
2013-04-23 17:07:15 +00:00
|
|
|
struct sr_serial_dev_inst *serial;
|
2012-10-27 19:03:02 +00:00
|
|
|
|
2013-01-23 02:40:44 +00:00
|
|
|
devc = sdi->priv;
|
2013-04-23 17:07:15 +00:00
|
|
|
serial = sdi->conn;
|
2013-01-23 02:40:44 +00:00
|
|
|
|
2012-10-27 19:03:02 +00:00
|
|
|
/* Try to get as much data as the buffer can hold. */
|
2012-10-21 01:39:37 +00:00
|
|
|
len = DMM_BUFSIZE - devc->buflen;
|
2013-04-23 17:07:15 +00:00
|
|
|
len = serial_read(serial, devc->buf + devc->buflen, len);
|
2012-10-21 01:39:37 +00:00
|
|
|
if (len < 1) {
|
2012-10-27 19:03:02 +00:00
|
|
|
sr_err("Serial port read error: %d.", len);
|
2012-10-21 01:39:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
devc->buflen += len;
|
|
|
|
|
2012-10-27 19:03:02 +00:00
|
|
|
/* Now look for packets in that data. */
|
2012-12-01 18:28:36 +00:00
|
|
|
while ((devc->buflen - offset) >= dmms[dmm].packet_size) {
|
|
|
|
if (dmms[dmm].packet_valid(devc->buf + offset)) {
|
2013-01-23 02:40:44 +00:00
|
|
|
handle_packet(devc->buf + offset, sdi, dmm, info);
|
2012-12-01 18:28:36 +00:00
|
|
|
offset += dmms[dmm].packet_size;
|
2012-10-21 01:39:37 +00:00
|
|
|
} else {
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-27 19:03:02 +00:00
|
|
|
/* If we have any data left, move it to the beginning of our buffer. */
|
|
|
|
for (i = 0; i < devc->buflen - offset; i++)
|
2012-10-21 01:39:37 +00:00
|
|
|
devc->buf[i] = devc->buf[offset + i];
|
|
|
|
devc->buflen -= offset;
|
|
|
|
}
|
|
|
|
|
2012-12-01 18:28:36 +00:00
|
|
|
static int receive_data(int fd, int revents, int dmm, void *info, void *cb_data)
|
2012-10-21 01:39:37 +00:00
|
|
|
{
|
2012-11-06 18:32:53 +00:00
|
|
|
struct sr_dev_inst *sdi;
|
2012-10-21 01:39:37 +00:00
|
|
|
struct dev_context *devc;
|
2013-04-23 17:07:15 +00:00
|
|
|
struct sr_serial_dev_inst *serial;
|
2012-12-23 18:48:48 +00:00
|
|
|
int64_t time;
|
2012-12-02 13:22:15 +00:00
|
|
|
int ret;
|
2012-10-21 01:39:37 +00:00
|
|
|
|
2012-11-12 11:50:42 +00:00
|
|
|
(void)fd;
|
|
|
|
|
2012-10-21 01:39:37 +00:00
|
|
|
if (!(sdi = cb_data))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if (!(devc = sdi->priv))
|
|
|
|
return TRUE;
|
|
|
|
|
2013-04-23 17:07:15 +00:00
|
|
|
serial = sdi->conn;
|
|
|
|
|
2012-10-27 19:03:02 +00:00
|
|
|
if (revents == G_IO_IN) {
|
2012-10-21 01:39:37 +00:00
|
|
|
/* Serial data arrived. */
|
2013-01-23 02:40:44 +00:00
|
|
|
handle_new_data(sdi, dmm, info);
|
2012-12-02 13:22:15 +00:00
|
|
|
} else {
|
|
|
|
/* Timeout, send another packet request (if DMM needs it). */
|
|
|
|
if (dmms[dmm].packet_request) {
|
2013-04-23 17:07:15 +00:00
|
|
|
ret = dmms[dmm].packet_request(serial);
|
2012-12-02 13:22:15 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
sr_err("Failed to request packet: %d.", ret);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2012-10-21 01:39:37 +00:00
|
|
|
}
|
|
|
|
|
2012-12-22 22:00:44 +00:00
|
|
|
if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
|
2012-12-26 00:29:27 +00:00
|
|
|
sr_info("Requested number of samples reached.");
|
2012-10-21 01:39:37 +00:00
|
|
|
sdi->driver->dev_acquisition_stop(sdi, cb_data);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-12-23 18:48:48 +00:00
|
|
|
if (devc->limit_msec) {
|
|
|
|
time = (g_get_monotonic_time() - devc->starttime) / 1000;
|
|
|
|
if (time > (int64_t)devc->limit_msec) {
|
2012-12-26 00:29:27 +00:00
|
|
|
sr_info("Requested time limit reached.");
|
2012-12-23 18:48:48 +00:00
|
|
|
sdi->driver->dev_acquisition_stop(sdi, cb_data);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-21 01:39:37 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2012-12-01 18:28:36 +00:00
|
|
|
|
2012-12-16 20:23:49 +00:00
|
|
|
#define RECEIVE_DATA(ID_UPPER, DMM_DRIVER) \
|
2012-12-13 00:19:17 +00:00
|
|
|
SR_PRIV int receive_data_##ID_UPPER(int fd, int revents, void *cb_data) { \
|
|
|
|
struct DMM_DRIVER##_info info; \
|
|
|
|
return receive_data(fd, revents, ID_UPPER, &info, cb_data); }
|
|
|
|
|
|
|
|
/* Driver-specific receive_data() wrappers */
|
2012-12-16 20:23:49 +00:00
|
|
|
RECEIVE_DATA(DIGITEK_DT4000ZC, fs9721)
|
|
|
|
RECEIVE_DATA(TEKPOWER_TP4000ZC, fs9721)
|
|
|
|
RECEIVE_DATA(METEX_ME31, metex14)
|
|
|
|
RECEIVE_DATA(PEAKTECH_3410, metex14)
|
|
|
|
RECEIVE_DATA(MASTECH_MAS345, metex14)
|
|
|
|
RECEIVE_DATA(VA_VA18B, fs9721)
|
|
|
|
RECEIVE_DATA(METEX_M3640D, metex14)
|
2013-10-29 19:31:24 +00:00
|
|
|
RECEIVE_DATA(METEX_M4650CR, metex14)
|
2012-12-16 20:23:49 +00:00
|
|
|
RECEIVE_DATA(PEAKTECH_4370, metex14)
|
|
|
|
RECEIVE_DATA(PCE_PCE_DM32, fs9721)
|
|
|
|
RECEIVE_DATA(RADIOSHACK_22_168, metex14)
|
2013-03-21 01:36:56 +00:00
|
|
|
RECEIVE_DATA(RADIOSHACK_22_805, metex14)
|
2012-12-16 20:23:49 +00:00
|
|
|
RECEIVE_DATA(RADIOSHACK_22_812, rs9lcd)
|
2013-05-01 01:13:15 +00:00
|
|
|
RECEIVE_DATA(TECPEL_DMM_8061_SER, fs9721)
|
2013-06-11 18:35:57 +00:00
|
|
|
RECEIVE_DATA(VOLTCRAFT_M3650D, metex14)
|
2013-10-29 17:10:10 +00:00
|
|
|
RECEIVE_DATA(VOLTCRAFT_M4650CR, metex14)
|
2012-12-19 21:50:07 +00:00
|
|
|
RECEIVE_DATA(VOLTCRAFT_VC820_SER, fs9721)
|
2013-06-22 14:48:00 +00:00
|
|
|
RECEIVE_DATA(VOLTCRAFT_VC830_SER, fs9922)
|
2012-12-19 21:50:07 +00:00
|
|
|
RECEIVE_DATA(VOLTCRAFT_VC840_SER, fs9721)
|
2013-07-05 12:02:50 +00:00
|
|
|
RECEIVE_DATA(UNI_T_UT60A_SER, fs9721)
|
|
|
|
RECEIVE_DATA(UNI_T_UT60E_SER, fs9721)
|
2013-04-29 18:15:02 +00:00
|
|
|
RECEIVE_DATA(UNI_T_UT61D_SER, fs9922)
|
2012-12-26 00:53:50 +00:00
|
|
|
RECEIVE_DATA(UNI_T_UT61E_SER, es51922)
|
2013-10-10 23:36:52 +00:00
|
|
|
RECEIVE_DATA(ISO_TECH_IDM103N, es519xx)
|