]> sigrok.org Git - libsigrok.git/blob - hardware/serial-dmm/protocol.c
serial-dmm: Add support for the Voltcraft VC-830.
[libsigrok.git] / hardware / serial-dmm / protocol.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
5  * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <stdlib.h>
22 #include <math.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <glib.h>
26 #include "libsigrok.h"
27 #include "libsigrok-internal.h"
28 #include "protocol.h"
29
30 static void log_dmm_packet(const uint8_t *buf)
31 {
32         sr_dbg("DMM packet: %02x %02x %02x %02x %02x %02x %02x"
33                " %02x %02x %02x %02x %02x %02x %02x",
34                buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
35                buf[7], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13]);
36 }
37
38 static void handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi,
39                           int dmm, void *info)
40 {
41         float floatval;
42         struct sr_datafeed_packet packet;
43         struct sr_datafeed_analog analog;
44         struct dev_context *devc;
45
46         log_dmm_packet(buf);
47         devc = sdi->priv;
48
49         memset(&analog, 0, sizeof(struct sr_datafeed_analog));
50
51         analog.probes = sdi->probes;
52         analog.num_samples = 1;
53         analog.mq = -1;
54
55         dmms[dmm].packet_parse(buf, &floatval, &analog, info);
56         analog.data = &floatval;
57
58         /* If this DMM needs additional handling, call the resp. function. */
59         if (dmms[dmm].dmm_details)
60                 dmms[dmm].dmm_details(&analog, info);
61
62         if (analog.mq != -1) {
63                 /* Got a measurement. */
64                 packet.type = SR_DF_ANALOG;
65                 packet.payload = &analog;
66                 sr_session_send(devc->cb_data, &packet);
67                 devc->num_samples++;
68         }
69 }
70
71 static void handle_new_data(struct sr_dev_inst *sdi, int dmm, void *info)
72 {
73         struct dev_context *devc;
74         int len, i, offset = 0;
75         struct sr_serial_dev_inst *serial;
76
77         devc = sdi->priv;
78         serial = sdi->conn;
79
80         /* Try to get as much data as the buffer can hold. */
81         len = DMM_BUFSIZE - devc->buflen;
82         len = serial_read(serial, devc->buf + devc->buflen, len);
83         if (len < 1) {
84                 sr_err("Serial port read error: %d.", len);
85                 return;
86         }
87         devc->buflen += len;
88
89         /* Now look for packets in that data. */
90         while ((devc->buflen - offset) >= dmms[dmm].packet_size) {
91                 if (dmms[dmm].packet_valid(devc->buf + offset)) {
92                         handle_packet(devc->buf + offset, sdi, dmm, info);
93                         offset += dmms[dmm].packet_size;
94                 } else {
95                         offset++;
96                 }
97         }
98
99         /* If we have any data left, move it to the beginning of our buffer. */
100         for (i = 0; i < devc->buflen - offset; i++)
101                 devc->buf[i] = devc->buf[offset + i];
102         devc->buflen -= offset;
103 }
104
105 static int receive_data(int fd, int revents, int dmm, void *info, void *cb_data)
106 {
107         struct sr_dev_inst *sdi;
108         struct dev_context *devc;
109         struct sr_serial_dev_inst *serial;
110         int64_t time;
111         int ret;
112
113         (void)fd;
114
115         if (!(sdi = cb_data))
116                 return TRUE;
117
118         if (!(devc = sdi->priv))
119                 return TRUE;
120
121         serial = sdi->conn;
122
123         if (revents == G_IO_IN) {
124                 /* Serial data arrived. */
125                 handle_new_data(sdi, dmm, info);
126         } else {
127                 /* Timeout, send another packet request (if DMM needs it). */
128                 if (dmms[dmm].packet_request) {
129                         ret = dmms[dmm].packet_request(serial);
130                         if (ret < 0) {
131                                 sr_err("Failed to request packet: %d.", ret);
132                                 return FALSE;
133                         }
134                 }
135         }
136
137         if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
138                 sr_info("Requested number of samples reached.");
139                 sdi->driver->dev_acquisition_stop(sdi, cb_data);
140                 return TRUE;
141         }
142
143         if (devc->limit_msec) {
144                 time = (g_get_monotonic_time() - devc->starttime) / 1000;
145                 if (time > (int64_t)devc->limit_msec) {
146                         sr_info("Requested time limit reached.");
147                         sdi->driver->dev_acquisition_stop(sdi, cb_data);
148                         return TRUE;
149                 }
150         }
151
152         return TRUE;
153 }
154
155 #define RECEIVE_DATA(ID_UPPER, DMM_DRIVER) \
156 SR_PRIV int receive_data_##ID_UPPER(int fd, int revents, void *cb_data) { \
157         struct DMM_DRIVER##_info info; \
158         return receive_data(fd, revents, ID_UPPER, &info, cb_data); }
159
160 /* Driver-specific receive_data() wrappers */
161 RECEIVE_DATA(DIGITEK_DT4000ZC, fs9721)
162 RECEIVE_DATA(TEKPOWER_TP4000ZC, fs9721)
163 RECEIVE_DATA(METEX_ME31, metex14)
164 RECEIVE_DATA(PEAKTECH_3410, metex14)
165 RECEIVE_DATA(MASTECH_MAS345, metex14)
166 RECEIVE_DATA(VA_VA18B, fs9721)
167 RECEIVE_DATA(METEX_M3640D, metex14)
168 RECEIVE_DATA(PEAKTECH_4370, metex14)
169 RECEIVE_DATA(PCE_PCE_DM32, fs9721)
170 RECEIVE_DATA(RADIOSHACK_22_168, metex14)
171 RECEIVE_DATA(RADIOSHACK_22_805, metex14)
172 RECEIVE_DATA(RADIOSHACK_22_812, rs9lcd)
173 RECEIVE_DATA(TECPEL_DMM_8060_SER, fs9721)
174 RECEIVE_DATA(TECPEL_DMM_8061_SER, fs9721)
175 RECEIVE_DATA(VOLTCRAFT_M3650D, metex14)
176 RECEIVE_DATA(VOLTCRAFT_VC820_SER, fs9721)
177 RECEIVE_DATA(VOLTCRAFT_VC830_SER, fs9922)
178 RECEIVE_DATA(VOLTCRAFT_VC840_SER, fs9721)
179 RECEIVE_DATA(UNI_T_UT61D_SER, fs9922)
180 RECEIVE_DATA(UNI_T_UT61E_SER, es51922)