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/>.
21 #include "libsigrok.h"
22 #include "libsigrok-internal.h"
28 #define LOG_PREFIX "scpi_serial"
30 #define BUFFER_SIZE 1024
33 struct sr_serial_dev_inst *serial;
34 char buffer[BUFFER_SIZE];
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 */
48 static GSList *scpi_serial_scan(struct drv_context *drvc)
50 GSList *l, *r, *resources = NULL;
56 for (i = 0; i < ARRAY_SIZE(scpi_serial_usb_ids); i++) {
57 if ((l = sr_serial_find_usb(scpi_serial_usb_ids[i].vendor_id,
58 scpi_serial_usb_ids[i].product_id)) == NULL)
60 for (r = l; r; r = r->next) {
61 if (scpi_serial_usb_ids[i].serialcomm)
62 res = g_strdup_printf("%s:%s", (char *) r->data,
63 scpi_serial_usb_ids[i].serialcomm);
65 res = g_strdup(r->data);
66 resources = g_slist_append(resources, res);
68 g_slist_free_full(l, g_free);
74 static int scpi_serial_dev_inst_new(void *priv, struct drv_context *drvc,
75 const char *resource, char **params, const char *serialcomm)
77 struct scpi_serial *sscpi = priv;
82 if (!(sscpi->serial = sr_serial_dev_inst_new(resource, serialcomm)))
88 static int scpi_serial_open(void *priv)
90 struct scpi_serial *sscpi = priv;
91 struct sr_serial_dev_inst *serial = sscpi->serial;
93 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
96 if (serial_flush(serial) != SR_OK)
105 static int scpi_serial_source_add(void *priv, int events, int timeout,
106 sr_receive_data_callback cb, void *cb_data)
108 struct scpi_serial *sscpi = priv;
109 struct sr_serial_dev_inst *serial = sscpi->serial;
111 return serial_source_add(serial, events, timeout, cb, cb_data);
114 static int scpi_serial_source_remove(void *priv)
116 struct scpi_serial *sscpi = priv;
117 struct sr_serial_dev_inst *serial = sscpi->serial;
119 return serial_source_remove(serial);
122 static int scpi_serial_send(void *priv, const char *command)
124 int len, result, written;
125 gchar *terminated_command;
126 struct scpi_serial *sscpi = priv;
127 struct sr_serial_dev_inst *serial = sscpi->serial;
129 terminated_command = g_strconcat(command, "\n", NULL);
130 len = strlen(terminated_command);
132 while (written < len) {
133 result = serial_write(serial, terminated_command + written, len - written);
135 sr_err("Error while sending SCPI command: '%s'.", command);
136 g_free(terminated_command);
142 g_free(terminated_command);
144 sr_spew("Successfully sent SCPI command: '%s'.", command);
149 static int scpi_serial_read_begin(void *priv)
156 static int scpi_serial_read_data(void *priv, char *buf, int maxlen)
158 struct scpi_serial *sscpi = priv;
161 len = BUFFER_SIZE - sscpi->count;
163 /* Try to read new data into the buffer if there is space. */
165 ret = serial_read(sscpi->serial, sscpi->buffer + sscpi->read,
166 BUFFER_SIZE - sscpi->count);
174 sr_spew("Read %d bytes into buffer.", ret);
177 /* Return as many bytes as possible from buffer, excluding any trailing newline. */
178 if (sscpi->read < sscpi->count) {
179 len = sscpi->count - sscpi->read;
182 if (sscpi->buffer[sscpi->read + len - 1] == '\n')
184 sr_spew("Returning %d bytes from buffer.", len);
185 memcpy(buf, sscpi->buffer + sscpi->read, len);
187 if (sscpi->read == BUFFER_SIZE) {
188 sr_spew("Resetting buffer.");
198 static int scpi_serial_read_complete(void *priv)
200 struct scpi_serial *sscpi = priv;
202 /* If the next character is a newline, discard it and report complete. */
203 if (sscpi->read < sscpi->count && sscpi->buffer[sscpi->read] == '\n') {
211 static int scpi_serial_close(void *priv)
213 struct scpi_serial *sscpi = priv;
215 return serial_close(sscpi->serial);
218 static void scpi_serial_free(void *priv)
220 struct scpi_serial *sscpi = priv;
222 sr_serial_dev_inst_free(sscpi->serial);
225 SR_PRIV const struct sr_scpi_dev_inst scpi_serial_dev = {
228 .priv_size = sizeof(struct scpi_serial),
229 .scan = scpi_serial_scan,
230 .dev_inst_new = scpi_serial_dev_inst_new,
231 .open = scpi_serial_open,
232 .source_add = scpi_serial_source_add,
233 .source_remove = scpi_serial_source_remove,
234 .send = scpi_serial_send,
235 .read_begin = scpi_serial_read_begin,
236 .read_data = scpi_serial_read_data,
237 .read_complete = scpi_serial_read_complete,
238 .close = scpi_serial_close,
239 .free = scpi_serial_free,