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