]> sigrok.org Git - libsigrok.git/blame_incremental - src/scpi/scpi_serial.c
scpi_serial: add "GWInstek VCP" (PID 0x0030) as seen in GDM-834x
[libsigrok.git] / src / scpi / scpi_serial.c
... / ...
CommitLineData
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 poljar (Damir Jelić) <poljarinho@gmail.com>
5 * Copyright (C) 2013 Martin Ling <martin-sigrok@earth.li>
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 <config.h>
22#include <glib.h>
23#include <stdlib.h>
24#include <string.h>
25#include <libsigrok/libsigrok.h>
26#include "libsigrok-internal.h"
27#include "scpi.h"
28
29#define LOG_PREFIX "scpi_serial"
30
31#ifdef HAVE_SERIAL_COMM
32
33struct scpi_serial {
34 struct sr_serial_dev_inst *serial;
35 gboolean got_newline;
36};
37
38/* Default serial port options for some known USB devices */
39static const struct {
40 uint16_t vendor_id;
41 uint16_t product_id;
42 const char *serialcomm;
43} scpi_serial_usb_ids[] = {
44 { 0x0403, 0xed72, "115200/8n1/flow=1" }, /* Hameg HO720 */
45 { 0x0403, 0xed73, "115200/8n1/flow=1" }, /* Hameg HO730 */
46 { 0x0aad, 0x0117, "115200/8n1" }, /* R&S HMO series, previously branded as Hameg HMO */
47 { 0x0aad, 0x0118, "115200/8n1" }, /* R&S HMO series, previously branded as Hameg HMO */
48 { 0x0aad, 0x0119, "115200/8n1" }, /* R&S HMO series, previously branded as Hameg HMO */
49 { 0x2184, 0x0030, "115200/8n1" }, /* GW-Instek GDM-8341 (VCP, SiLabs CP210x) */
50 { 0x2184, 0x0058, "115200/8n1" }, /* GW-Instek GDM-9061 (USBCDC mode) */
51};
52
53static GSList *scpi_serial_scan(struct drv_context *drvc)
54{
55 GSList *l, *r, *resources = NULL;
56 gchar *res;
57 unsigned i;
58
59 (void)drvc;
60
61 for (i = 0; i < ARRAY_SIZE(scpi_serial_usb_ids); i++) {
62 if (!(l = sr_serial_find_usb(scpi_serial_usb_ids[i].vendor_id,
63 scpi_serial_usb_ids[i].product_id)))
64 continue;
65 for (r = l; r; r = r->next) {
66 if (scpi_serial_usb_ids[i].serialcomm)
67 res = g_strdup_printf("%s:%s", (char *) r->data,
68 scpi_serial_usb_ids[i].serialcomm);
69 else
70 res = g_strdup(r->data);
71 resources = g_slist_append(resources, res);
72 }
73 g_slist_free_full(l, g_free);
74 }
75
76 return resources;
77}
78
79static int scpi_serial_dev_inst_new(void *priv, struct drv_context *drvc,
80 const char *resource, char **params, const char *serialcomm)
81{
82 GSList *l, *r;
83 unsigned i;
84 struct scpi_serial *sscpi = priv;
85
86 (void)drvc;
87 (void)params;
88
89 /* If no serial port option is specified on the command-line using the
90 * "serialcomm" driver option, but the device is connected through USB
91 * and it requires a known default serial port option, then used it in
92 * order to avoid data corruption or even worse problems.
93 */
94 if (!serialcomm) {
95 for (i = 0; i < ARRAY_SIZE(scpi_serial_usb_ids); i++) {
96 if (!(l = sr_serial_find_usb(scpi_serial_usb_ids[i].vendor_id,
97 scpi_serial_usb_ids[i].product_id)))
98 continue;
99 for (r = l; r; r = r->next)
100 if (!strcmp(resource, r->data) && scpi_serial_usb_ids[i].serialcomm)
101 serialcomm = scpi_serial_usb_ids[i].serialcomm;
102 g_slist_free_full(l, g_free);
103 }
104 }
105
106 sscpi->serial = sr_serial_dev_inst_new(resource, serialcomm);
107
108 return SR_OK;
109}
110
111static int scpi_serial_open(struct sr_scpi_dev_inst *scpi)
112{
113 struct scpi_serial *sscpi = scpi->priv;
114 struct sr_serial_dev_inst *serial = sscpi->serial;
115
116 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
117 return SR_ERR;
118
119 sscpi->got_newline = FALSE;
120
121 return SR_OK;
122}
123
124static int scpi_serial_connection_id(struct sr_scpi_dev_inst *scpi,
125 char **connection_id)
126{
127 struct scpi_serial *sscpi = scpi->priv;
128 struct sr_serial_dev_inst *serial = sscpi->serial;
129
130 *connection_id = g_strdup(serial->port);
131
132 return SR_OK;
133}
134
135static int scpi_serial_source_add(struct sr_session *session, void *priv,
136 int events, int timeout, sr_receive_data_callback cb, void *cb_data)
137{
138 struct scpi_serial *sscpi = priv;
139 struct sr_serial_dev_inst *serial = sscpi->serial;
140
141 return serial_source_add(session, serial, events, timeout, cb, cb_data);
142}
143
144static int scpi_serial_source_remove(struct sr_session *session, void *priv)
145{
146 struct scpi_serial *sscpi = priv;
147 struct sr_serial_dev_inst *serial = sscpi->serial;
148
149 return serial_source_remove(session, serial);
150}
151
152static int scpi_serial_send(void *priv, const char *command)
153{
154 int result;
155 struct scpi_serial *sscpi = priv;
156 struct sr_serial_dev_inst *serial = sscpi->serial;
157
158 result = serial_write_blocking(serial, command, strlen(command), 0);
159 if (result < 0) {
160 sr_err("Error while sending SCPI command '%s': %d.",
161 command, result);
162 return SR_ERR;
163 }
164
165 sr_spew("Successfully sent SCPI command: '%s'.", command);
166
167 return SR_OK;
168}
169
170static int scpi_serial_read_begin(void *priv)
171{
172 struct scpi_serial *sscpi = priv;
173 sscpi->got_newline = FALSE;
174
175 return SR_OK;
176}
177
178static int scpi_serial_read_data(void *priv, char *buf, int maxlen)
179{
180 struct scpi_serial *sscpi = priv;
181 int ret;
182
183 /* Try to read new data into the buffer. */
184 ret = serial_read_nonblocking(sscpi->serial, buf, maxlen);
185 if (ret < 0)
186 return ret;
187
188 /*
189 * Check for line termination at the end of the receive data.
190 * Handle the usual case of NL, as well as the unusual NL+CR
191 * combination (some GWInstek DMMs were found to do this).
192 */
193 sscpi->got_newline = FALSE;
194 if (ret >= 1 && buf[ret - 1] == '\n') {
195 sscpi->got_newline = TRUE;
196 sr_spew("Received NL terminator");
197 } else if (ret >= 2 && buf[ret - 2] == '\n' && buf[ret - 1] == '\r') {
198 ret--;
199 sscpi->got_newline = TRUE;
200 sr_spew("Received NL+CR terminator");
201 }
202
203 return ret;
204}
205
206static int scpi_serial_read_complete(void *priv)
207{
208 struct scpi_serial *sscpi = priv;
209
210 return sscpi->got_newline;
211}
212
213static int scpi_serial_close(struct sr_scpi_dev_inst *scpi)
214{
215 struct scpi_serial *sscpi = scpi->priv;
216
217 return serial_close(sscpi->serial);
218}
219
220static void scpi_serial_free(void *priv)
221{
222 struct scpi_serial *sscpi = priv;
223
224 sr_serial_dev_inst_free(sscpi->serial);
225}
226
227SR_PRIV const struct sr_scpi_dev_inst scpi_serial_dev = {
228 .name = "serial",
229 .prefix = "",
230 .transport = SCPI_TRANSPORT_SERIAL,
231 .priv_size = sizeof(struct scpi_serial),
232 .scan = scpi_serial_scan,
233 .dev_inst_new = scpi_serial_dev_inst_new,
234 .open = scpi_serial_open,
235 .connection_id = scpi_serial_connection_id,
236 .source_add = scpi_serial_source_add,
237 .source_remove = scpi_serial_source_remove,
238 .send = scpi_serial_send,
239 .read_begin = scpi_serial_read_begin,
240 .read_data = scpi_serial_read_data,
241 .read_complete = scpi_serial_read_complete,
242 .close = scpi_serial_close,
243 .free = scpi_serial_free,
244};
245
246#endif