]> sigrok.org Git - libsigrok.git/blame_incremental - src/hardware/serial-dmm/protocol.c
Add initial Voltcraft VC-870 support.
[libsigrok.git] / src / hardware / serial-dmm / protocol.c
... / ...
CommitLineData
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
30static 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 %02x %02x "
34 "%02x %02x %02x %02x %02x %02x %02x",
35 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
36 buf[7], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13],
37 buf[14], buf[15], buf[16], buf[17], buf[18], buf[19], buf[20],
38 buf[21], buf[22]);
39}
40
41static void handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi,
42 int dmm, void *info)
43{
44 float floatval;
45 struct sr_datafeed_packet packet;
46 struct sr_datafeed_analog analog;
47 struct dev_context *devc;
48
49 log_dmm_packet(buf);
50 devc = sdi->priv;
51
52 memset(&analog, 0, sizeof(struct sr_datafeed_analog));
53
54 analog.channels = sdi->channels;
55 analog.num_samples = 1;
56 analog.mq = -1;
57
58 dmms[dmm].packet_parse(buf, &floatval, &analog, info);
59 analog.data = &floatval;
60
61 /* If this DMM needs additional handling, call the resp. function. */
62 if (dmms[dmm].dmm_details)
63 dmms[dmm].dmm_details(&analog, info);
64
65 if (analog.mq != -1) {
66 /* Got a measurement. */
67 packet.type = SR_DF_ANALOG;
68 packet.payload = &analog;
69 sr_session_send(devc->cb_data, &packet);
70 devc->num_samples++;
71 }
72}
73
74/** Request packet, if required. */
75SR_PRIV int req_packet(struct sr_dev_inst *sdi, int dmm)
76{
77 struct dev_context *devc;
78 struct sr_serial_dev_inst *serial;
79 int ret;
80
81 if (!dmms[dmm].packet_request)
82 return SR_OK;
83
84 devc = sdi->priv;
85 serial = sdi->conn;
86
87 if (devc->req_next_at && (devc->req_next_at > g_get_monotonic_time())) {
88 sr_spew("Not requesting new packet yet, %" PRIi64 " ms left.",
89 ((devc->req_next_at - g_get_monotonic_time()) / 1000));
90 return SR_OK;
91 }
92
93 ret = dmms[dmm].packet_request(serial);
94 if (ret < 0) {
95 sr_err("Failed to request packet: %d.", ret);
96 return ret;
97 }
98
99 if (dmms[dmm].req_timeout_ms)
100 devc->req_next_at = g_get_monotonic_time() + (dmms[dmm].req_timeout_ms * 1000);
101
102 return SR_OK;
103}
104
105static void handle_new_data(struct sr_dev_inst *sdi, int dmm, void *info)
106{
107 struct dev_context *devc;
108 int len, i, offset = 0;
109 struct sr_serial_dev_inst *serial;
110
111 devc = sdi->priv;
112 serial = sdi->conn;
113
114 /* Try to get as much data as the buffer can hold. */
115 len = DMM_BUFSIZE - devc->buflen;
116 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len);
117 if (len == 0)
118 return; /* No new bytes, nothing to do. */
119 if (len < 0) {
120 sr_err("Serial port read error: %d.", len);
121 return;
122 }
123 devc->buflen += len;
124
125 /* Now look for packets in that data. */
126 while ((devc->buflen - offset) >= dmms[dmm].packet_size) {
127 if (dmms[dmm].packet_valid(devc->buf + offset)) {
128 handle_packet(devc->buf + offset, sdi, dmm, info);
129 offset += dmms[dmm].packet_size;
130
131 /* Request next packet, if required. */
132 if (!dmms[dmm].packet_request)
133 break;
134 if (dmms[dmm].req_timeout_ms || dmms[dmm].req_delay_ms)
135 devc->req_next_at = g_get_monotonic_time() +
136 dmms[dmm].req_delay_ms * 1000;
137 req_packet(sdi, dmm);
138 } else {
139 offset++;
140 }
141 }
142
143 /* If we have any data left, move it to the beginning of our buffer. */
144 for (i = 0; i < devc->buflen - offset; i++)
145 devc->buf[i] = devc->buf[offset + i];
146 devc->buflen -= offset;
147}
148
149static int receive_data(int fd, int revents, int dmm, void *info, void *cb_data)
150{
151 struct sr_dev_inst *sdi;
152 struct dev_context *devc;
153 int64_t time;
154
155 (void)fd;
156
157 if (!(sdi = cb_data))
158 return TRUE;
159
160 if (!(devc = sdi->priv))
161 return TRUE;
162
163 if (revents == G_IO_IN) {
164 /* Serial data arrived. */
165 handle_new_data(sdi, dmm, info);
166 } else {
167 /* Timeout; send another packet request if DMM needs it. */
168 if (dmms[dmm].packet_request && (req_packet(sdi, dmm) < 0))
169 return FALSE;
170 }
171
172 if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
173 sr_info("Requested number of samples reached.");
174 sdi->driver->dev_acquisition_stop(sdi, cb_data);
175 return TRUE;
176 }
177
178 if (devc->limit_msec) {
179 time = (g_get_monotonic_time() - devc->starttime) / 1000;
180 if (time > (int64_t)devc->limit_msec) {
181 sr_info("Requested time limit reached.");
182 sdi->driver->dev_acquisition_stop(sdi, cb_data);
183 return TRUE;
184 }
185 }
186
187 return TRUE;
188}
189
190#define RECEIVE_DATA(ID_UPPER, DMM_DRIVER) \
191SR_PRIV int receive_data_##ID_UPPER(int fd, int revents, void *cb_data) { \
192 struct DMM_DRIVER##_info info; \
193 return receive_data(fd, revents, ID_UPPER, &info, cb_data); }
194
195/* Driver-specific receive_data() wrappers */
196RECEIVE_DATA(BBCGM_M2110, metex14) /* metex14_info used as a dummy. */
197RECEIVE_DATA(DIGITEK_DT4000ZC, fs9721)
198RECEIVE_DATA(TEKPOWER_TP4000ZC, fs9721)
199RECEIVE_DATA(METEX_ME31, metex14)
200RECEIVE_DATA(PEAKTECH_3410, metex14)
201RECEIVE_DATA(MASTECH_MAS345, metex14)
202RECEIVE_DATA(MASTECH_MS8250B, fs9721)
203RECEIVE_DATA(VA_VA18B, fs9721)
204RECEIVE_DATA(VA_VA40B, fs9721)
205RECEIVE_DATA(METEX_M3640D, metex14)
206RECEIVE_DATA(METEX_M4650CR, metex14)
207RECEIVE_DATA(PEAKTECH_4370, metex14)
208RECEIVE_DATA(PCE_PCE_DM32, fs9721)
209RECEIVE_DATA(RADIOSHACK_22_168, metex14)
210RECEIVE_DATA(RADIOSHACK_22_805, metex14)
211RECEIVE_DATA(RADIOSHACK_22_812, rs9lcd)
212RECEIVE_DATA(TECPEL_DMM_8061_SER, fs9721)
213RECEIVE_DATA(VOLTCRAFT_M3650CR, metex14)
214RECEIVE_DATA(VOLTCRAFT_M3650D, metex14)
215RECEIVE_DATA(VOLTCRAFT_M4650CR, metex14)
216RECEIVE_DATA(VOLTCRAFT_ME42, metex14)
217RECEIVE_DATA(VOLTCRAFT_VC820_SER, fs9721)
218RECEIVE_DATA(VOLTCRAFT_VC830_SER, fs9922)
219RECEIVE_DATA(VOLTCRAFT_VC840_SER, fs9721)
220RECEIVE_DATA(VOLTCRAFT_VC870_SER, vc870)
221RECEIVE_DATA(VOLTCRAFT_VC920_SER, ut71x)
222RECEIVE_DATA(VOLTCRAFT_VC940_SER, ut71x)
223RECEIVE_DATA(VOLTCRAFT_VC960_SER, ut71x)
224RECEIVE_DATA(UNI_T_UT60A_SER, fs9721)
225RECEIVE_DATA(UNI_T_UT60E_SER, fs9721)
226RECEIVE_DATA(UNI_T_UT60G_SER, es519xx)
227RECEIVE_DATA(UNI_T_UT61B_SER, fs9922)
228RECEIVE_DATA(UNI_T_UT61C_SER, fs9922)
229RECEIVE_DATA(UNI_T_UT61D_SER, fs9922)
230RECEIVE_DATA(UNI_T_UT61E_SER, es519xx)
231RECEIVE_DATA(UNI_T_UT71A_SER, ut71x)
232RECEIVE_DATA(UNI_T_UT71B_SER, ut71x)
233RECEIVE_DATA(UNI_T_UT71C_SER, ut71x)
234RECEIVE_DATA(UNI_T_UT71D_SER, ut71x)
235RECEIVE_DATA(UNI_T_UT71E_SER, ut71x)
236RECEIVE_DATA(ISO_TECH_IDM103N, es519xx)
237RECEIVE_DATA(TENMA_72_7745_SER, fs9721)
238RECEIVE_DATA(TENMA_72_7750_SER, es519xx)
239RECEIVE_DATA(BRYMEN_BM25X, bm25x)