2 * This file is part of the libsigrok project.
4 * Copyright (C) 2018 Bastian Schmitz <bastian.schmitz@udo.edu>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 SR_PRIV int gpd_send_cmd(struct sr_serial_dev_inst *serial, const char *cmd, ...)
32 vsnprintf(cmdbuf, sizeof(cmdbuf), cmd, args);
35 cmd_esc = g_strescape(cmdbuf, NULL);
36 sr_dbg("Sending '%s'.", cmd_esc);
39 ret = serial_write_blocking(serial, cmdbuf, strlen(cmdbuf),
40 serial_timeout(serial, strlen(cmdbuf)));
42 sr_err("Error sending command: %d.", ret);
49 SR_PRIV int gpd_receive_reply(struct sr_serial_dev_inst *serial, char *buf,
52 int l_recv = 0, bufpos = 0, retc, l_startpos = 0, lines = 1;
53 gint64 start, remaining;
54 const int timeout_ms = 250;
56 if (!serial || !buf || (buflen <= 0))
59 start = g_get_monotonic_time();
60 remaining = timeout_ms;
62 while ((l_recv < lines) && (bufpos < (buflen + 1))) {
63 retc = serial_read_blocking(serial, &buf[bufpos], 1, remaining);
67 if (bufpos == 0 && buf[bufpos] == '\r')
69 if (bufpos == 0 && buf[bufpos] == '\n')
72 if (buf[bufpos] == '\n' || buf[bufpos] == '\r') {
74 sr_dbg("Received line '%s'.", &buf[l_startpos]);
76 l_startpos = bufpos + 1;
81 /* Reduce timeout by time elapsed. */
82 remaining = timeout_ms - ((g_get_monotonic_time() - start) / 1000);
84 return SR_ERR; /* Timeout. */
95 SR_PRIV int gpd_receive_data(int fd, int revents, void *cb_data)
97 struct sr_dev_inst *sdi;
98 struct dev_context *devc;
99 struct sr_serial_dev_inst *serial;
100 struct sr_datafeed_packet packet;
101 struct sr_datafeed_analog analog;
102 struct sr_analog_encoding encoding;
103 struct sr_analog_meaning meaning;
104 struct sr_analog_spec spec;
105 struct sr_channel *ch;
112 if (!(sdi = cb_data))
115 if (!(devc = sdi->priv))
120 if (revents == G_IO_IN) {
121 if (!devc->reply_pending) {
122 sr_err("No reply pending.");
123 gpd_receive_reply(serial, reply, sizeof(reply));
124 reply_esc = g_strescape(reply, NULL);
125 sr_err("Unexpected data '%s'.", reply_esc);
128 for (i = 0; i < devc->model->num_channels; i++) {
129 packet.type = SR_DF_ANALOG;
130 packet.payload = &analog;
133 gpd_receive_reply(serial, reply, sizeof(reply));
134 if (sscanf(reply, "%f", &devc->config[i].output_current_last) != 1) {
135 sr_err("Invalid reply to IOUT1?: '%s'.",
140 /* Send the value forward. */
141 sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
142 analog.num_samples = 1;
143 ch = g_slist_nth_data(sdi->channels, i);
144 analog.meaning->channels =
145 g_slist_append(NULL, ch);
146 analog.meaning->mq = SR_MQ_CURRENT;
147 analog.meaning->unit = SR_UNIT_AMPERE;
148 analog.meaning->mqflags = 0;
149 analog.encoding->digits = 3;
150 analog.spec->spec_digits = 3;
151 analog.data = &devc->config[i].output_current_last;
152 sr_session_send(sdi, &packet);
155 gpd_receive_reply(serial, reply, sizeof(reply));
156 if (sscanf(reply, "%f", &devc->config[i].output_voltage_last) != 1) {
157 sr_err("Invalid reply to VOUT1?: '%s'.",
162 /* Send the value forward. */
163 sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
164 analog.num_samples = 1;
165 ch = g_slist_nth_data(sdi->channels, i);
166 analog.meaning->channels =
167 g_slist_append(NULL, ch);
168 analog.meaning->mq = SR_MQ_VOLTAGE;
169 analog.meaning->unit = SR_UNIT_VOLT;
170 analog.meaning->mqflags = SR_MQFLAG_DC;
171 analog.encoding->digits = 3;
172 analog.spec->spec_digits = 3;
173 analog.data = &devc->config[i].output_voltage_last;
174 sr_session_send(sdi, &packet);
177 devc->reply_pending = FALSE;
178 sr_sw_limits_update_samples_read(&devc->limits, 1);
181 if (!devc->reply_pending) {
182 for (i = 0; i < devc->model->num_channels; i++)
183 gpd_send_cmd(serial, "IOUT%d?\nVOUT%d?\n",
185 devc->req_sent_at = g_get_monotonic_time();
186 devc->reply_pending = TRUE;
190 if (sr_sw_limits_check(&devc->limits)) {
191 sr_dev_acquisition_stop(sdi);