2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 poljar (Damir Jelić) <poljarinho@gmail.com>
5 * Copyright (C) 2013 Martin Ling <martin-sigrok@earth.li>
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.
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.
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/>.
25 #include <libsigrok/libsigrok.h>
26 #include "libsigrok-internal.h"
29 #define LOG_PREFIX "scpi_serial"
31 #ifdef HAVE_SERIAL_COMM
34 struct sr_serial_dev_inst *serial;
38 /* Default serial port options for some known USB devices */
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 */
51 static GSList *scpi_serial_scan(struct drv_context *drvc)
53 GSList *l, *r, *resources = NULL;
59 for (i = 0; i < ARRAY_SIZE(scpi_serial_usb_ids); i++) {
60 if (!(l = sr_serial_find_usb(scpi_serial_usb_ids[i].vendor_id,
61 scpi_serial_usb_ids[i].product_id)))
63 for (r = l; r; r = r->next) {
64 if (scpi_serial_usb_ids[i].serialcomm)
65 res = g_strdup_printf("%s:%s", (char *) r->data,
66 scpi_serial_usb_ids[i].serialcomm);
68 res = g_strdup(r->data);
69 resources = g_slist_append(resources, res);
71 g_slist_free_full(l, g_free);
77 static int scpi_serial_dev_inst_new(void *priv, struct drv_context *drvc,
78 const char *resource, char **params, const char *serialcomm)
82 struct scpi_serial *sscpi = priv;
87 /* If no serial port option is specified on the command-line using the
88 * "serialcomm" driver option, but the device is connected through USB
89 * and it requires a known default serial port option, then used it in
90 * order to avoid data corruption or even worse problems.
93 for (i = 0; i < ARRAY_SIZE(scpi_serial_usb_ids); i++) {
94 if (!(l = sr_serial_find_usb(scpi_serial_usb_ids[i].vendor_id,
95 scpi_serial_usb_ids[i].product_id)))
97 for (r = l; r; r = r->next)
98 if (!strcmp(resource, r->data) && scpi_serial_usb_ids[i].serialcomm)
99 serialcomm = scpi_serial_usb_ids[i].serialcomm;
100 g_slist_free_full(l, g_free);
104 sscpi->serial = sr_serial_dev_inst_new(resource, serialcomm);
109 static int scpi_serial_open(struct sr_scpi_dev_inst *scpi)
111 struct scpi_serial *sscpi = scpi->priv;
112 struct sr_serial_dev_inst *serial = sscpi->serial;
114 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
117 sscpi->got_newline = FALSE;
122 static int scpi_serial_connection_id(struct sr_scpi_dev_inst *scpi,
123 char **connection_id)
125 struct scpi_serial *sscpi = scpi->priv;
126 struct sr_serial_dev_inst *serial = sscpi->serial;
128 *connection_id = g_strdup(serial->port);
133 static int scpi_serial_source_add(struct sr_session *session, void *priv,
134 int events, int timeout, sr_receive_data_callback cb, void *cb_data)
136 struct scpi_serial *sscpi = priv;
137 struct sr_serial_dev_inst *serial = sscpi->serial;
139 return serial_source_add(session, serial, events, timeout, cb, cb_data);
142 static int scpi_serial_source_remove(struct sr_session *session, void *priv)
144 struct scpi_serial *sscpi = priv;
145 struct sr_serial_dev_inst *serial = sscpi->serial;
147 return serial_source_remove(session, serial);
150 static int scpi_serial_send(void *priv, const char *command)
153 struct scpi_serial *sscpi = priv;
154 struct sr_serial_dev_inst *serial = sscpi->serial;
156 result = serial_write_blocking(serial, command, strlen(command), 0);
158 sr_err("Error while sending SCPI command '%s': %d.",
163 sr_spew("Successfully sent SCPI command: '%s'.", command);
168 static int scpi_serial_read_begin(void *priv)
170 struct scpi_serial *sscpi = priv;
171 sscpi->got_newline = FALSE;
176 static int scpi_serial_read_data(void *priv, char *buf, int maxlen)
178 struct scpi_serial *sscpi = priv;
181 /* Try to read new data into the buffer. */
182 ret = serial_read_nonblocking(sscpi->serial, buf, maxlen);
188 if (buf[ret - 1] == '\n') {
189 sscpi->got_newline = TRUE;
190 sr_spew("Received terminator");
192 sscpi->got_newline = FALSE;
199 static int scpi_serial_read_complete(void *priv)
201 struct scpi_serial *sscpi = priv;
203 return sscpi->got_newline;
206 static int scpi_serial_close(struct sr_scpi_dev_inst *scpi)
208 struct scpi_serial *sscpi = scpi->priv;
210 return serial_close(sscpi->serial);
213 static void scpi_serial_free(void *priv)
215 struct scpi_serial *sscpi = priv;
217 sr_serial_dev_inst_free(sscpi->serial);
220 SR_PRIV const struct sr_scpi_dev_inst scpi_serial_dev = {
223 .transport = SCPI_TRANSPORT_SERIAL,
224 .priv_size = sizeof(struct scpi_serial),
225 .scan = scpi_serial_scan,
226 .dev_inst_new = scpi_serial_dev_inst_new,
227 .open = scpi_serial_open,
228 .connection_id = scpi_serial_connection_id,
229 .source_add = scpi_serial_source_add,
230 .source_remove = scpi_serial_source_remove,
231 .send = scpi_serial_send,
232 .read_begin = scpi_serial_read_begin,
233 .read_data = scpi_serial_read_data,
234 .read_complete = scpi_serial_read_complete,
235 .close = scpi_serial_close,
236 .free = scpi_serial_free,