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/>.
24 #include "libsigrok.h"
25 #include "libsigrok-internal.h"
27 #define LOG_PREFIX "scpi_serial"
29 #define BUFFER_SIZE 1024
32 struct sr_serial_dev_inst *serial;
33 char buffer[BUFFER_SIZE];
41 const char *serialcomm;
42 } scpi_serial_usb_ids[] = {
43 { 0x0403, 0xed72, "115200/8n1/flow=1" }, /* Hameg HO720 */
44 { 0x0403, 0xed73, "115200/8n1/flow=1" }, /* Hameg HO730 */
47 static GSList *scpi_serial_scan(struct drv_context *drvc)
49 GSList *l, *r, *resources = NULL;
55 for (i = 0; i < ARRAY_SIZE(scpi_serial_usb_ids); i++) {
56 if (!(l = sr_serial_find_usb(scpi_serial_usb_ids[i].vendor_id,
57 scpi_serial_usb_ids[i].product_id)))
59 for (r = l; r; r = r->next) {
60 if (scpi_serial_usb_ids[i].serialcomm)
61 res = g_strdup_printf("%s:%s", (char *) r->data,
62 scpi_serial_usb_ids[i].serialcomm);
64 res = g_strdup(r->data);
65 resources = g_slist_append(resources, res);
67 g_slist_free_full(l, g_free);
73 static int scpi_serial_dev_inst_new(void *priv, struct drv_context *drvc,
74 const char *resource, char **params, const char *serialcomm)
76 struct scpi_serial *sscpi = priv;
81 sscpi->serial = sr_serial_dev_inst_new(resource, serialcomm);
86 static int scpi_serial_open(void *priv)
88 struct scpi_serial *sscpi = priv;
89 struct sr_serial_dev_inst *serial = sscpi->serial;
91 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
94 if (serial_flush(serial) != SR_OK)
103 static int scpi_serial_source_add(struct sr_session *session, void *priv,
104 int events, int timeout, sr_receive_data_callback cb, void *cb_data)
106 struct scpi_serial *sscpi = priv;
107 struct sr_serial_dev_inst *serial = sscpi->serial;
109 return serial_source_add(session, serial, events, timeout, cb, cb_data);
112 static int scpi_serial_source_remove(struct sr_session *session, void *priv)
114 struct scpi_serial *sscpi = priv;
115 struct sr_serial_dev_inst *serial = sscpi->serial;
117 return serial_source_remove(session, serial);
120 static int scpi_serial_send(void *priv, const char *command)
122 int len, result, written;
123 gchar *terminated_command;
124 struct scpi_serial *sscpi = priv;
125 struct sr_serial_dev_inst *serial = sscpi->serial;
127 terminated_command = g_strconcat(command, "\n", NULL);
128 len = strlen(terminated_command);
130 while (written < len) {
131 result = serial_write_nonblocking(serial,
132 terminated_command + written, len - written);
134 sr_err("Error while sending SCPI command: '%s'.", command);
135 g_free(terminated_command);
141 g_free(terminated_command);
143 sr_spew("Successfully sent SCPI command: '%s'.", command);
148 static int scpi_serial_read_begin(void *priv)
155 static int scpi_serial_read_data(void *priv, char *buf, int maxlen)
157 struct scpi_serial *sscpi = priv;
160 len = BUFFER_SIZE - sscpi->count;
162 /* Try to read new data into the buffer if there is space. */
164 ret = serial_read_nonblocking(sscpi->serial, sscpi->buffer + sscpi->read,
165 BUFFER_SIZE - sscpi->count);
173 sr_spew("Read %d bytes into buffer.", ret);
176 /* Return as many bytes as possible from buffer, excluding any trailing newline. */
177 if (sscpi->read < sscpi->count) {
178 len = sscpi->count - sscpi->read;
181 if (sscpi->buffer[sscpi->read + len - 1] == '\n')
183 sr_spew("Returning %d bytes from buffer.", len);
184 memcpy(buf, sscpi->buffer + sscpi->read, len);
186 if (sscpi->read == BUFFER_SIZE) {
187 sr_spew("Resetting buffer.");
197 static int scpi_serial_read_complete(void *priv)
199 struct scpi_serial *sscpi = priv;
201 /* If the next character is a newline, discard it and report complete. */
202 if (sscpi->read < sscpi->count && sscpi->buffer[sscpi->read] == '\n') {
210 static int scpi_serial_close(void *priv)
212 struct scpi_serial *sscpi = priv;
214 return serial_close(sscpi->serial);
217 static void scpi_serial_free(void *priv)
219 struct scpi_serial *sscpi = priv;
221 sr_serial_dev_inst_free(sscpi->serial);
224 SR_PRIV const struct sr_scpi_dev_inst scpi_serial_dev = {
227 .priv_size = sizeof(struct scpi_serial),
228 .scan = scpi_serial_scan,
229 .dev_inst_new = scpi_serial_dev_inst_new,
230 .open = scpi_serial_open,
231 .source_add = scpi_serial_source_add,
232 .source_remove = scpi_serial_source_remove,
233 .send = scpi_serial_send,
234 .read_begin = scpi_serial_read_begin,
235 .read_data = scpi_serial_read_data,
236 .read_complete = scpi_serial_read_complete,
237 .close = scpi_serial_close,
238 .free = scpi_serial_free,