2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 Uwe Hermann <uwe@hermann-uwe.de>
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 2 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #define REQ_TIMEOUT_MS 500
25 static int send_cmd(struct sr_serial_dev_inst *serial, const char *cmd)
30 cmd_esc = g_strescape(cmd, NULL);
31 sr_dbg("Sending '%s'.", cmd_esc);
33 if ((ret = serial_write_blocking(serial, cmd, strlen(cmd))) < 0) {
34 sr_err("Error sending command: %d.", ret);
44 static int parse_volt_curr_mode(struct sr_dev_inst *sdi, char **tokens)
48 struct dev_context *devc;
52 /* Bytes 0-3: Voltage. */
53 str = g_strndup(tokens[0], 4);
54 val = g_ascii_strtod(str, NULL) / 100;
58 /* Bytes 4-7: Current. */
59 str = g_strndup((tokens[0] + 4), 4);
60 val = g_ascii_strtod(str, NULL) / 100;
64 /* Byte 8: Mode ('0' means CV, '1' means CC). */
65 devc->cc_mode = (tokens[0][8] == '1');
70 static void send_sample(struct sr_dev_inst *sdi)
72 struct dev_context *devc;
73 struct sr_datafeed_packet packet;
74 struct sr_datafeed_analog analog;
78 packet.type = SR_DF_ANALOG;
79 packet.payload = &analog;
80 analog.channels = sdi->channels;
81 analog.num_samples = 1;
83 analog.mq = SR_MQ_VOLTAGE;
84 analog.unit = SR_UNIT_VOLT;
85 analog.mqflags = SR_MQFLAG_DC;
86 analog.data = &devc->voltage;
87 sr_session_send(sdi, &packet);
89 analog.mq = SR_MQ_CURRENT;
90 analog.unit = SR_UNIT_AMPERE;
92 analog.data = &devc->current;
93 sr_session_send(sdi, &packet);
98 static int parse_reply(struct sr_dev_inst *sdi)
100 struct dev_context *devc;
101 char *reply_esc, **tokens;
105 reply_esc = g_strescape(devc->buf, NULL);
106 sr_dbg("Received '%s'.", reply_esc);
108 tokens = g_strsplit(devc->buf, "\r", 0);
110 if (parse_volt_curr_mode(sdi, tokens) < 0)
120 static int handle_new_data(struct sr_dev_inst *sdi)
123 struct dev_context *devc;
124 struct sr_serial_dev_inst *serial;
129 len = serial_read(serial, devc->buf + devc->buflen, 1);
134 devc->buf[devc->buflen] = '\0';
136 /* Wait until we received an "OK\r" (among other bytes). */
137 if (!g_str_has_suffix(devc->buf, "OK\r"))
145 devc->reply_pending = FALSE;
150 SR_PRIV int hcs_receive_data(int fd, int revents, void *cb_data)
152 struct sr_dev_inst *sdi;
153 struct dev_context *devc;
154 struct sr_serial_dev_inst *serial;
155 int64_t t, elapsed_us;
159 if (!(sdi = cb_data))
162 if (!(devc = sdi->priv))
167 if (revents == G_IO_IN) {
168 /* New data arrived. */
169 handle_new_data(sdi);
174 if (devc->limit_samples && (devc->num_samples >= devc->limit_samples)) {
175 sr_info("Requested number of samples reached.");
176 sdi->driver->dev_acquisition_stop(sdi, cb_data);
180 if (devc->limit_msec) {
181 t = (g_get_monotonic_time() - devc->starttime) / 1000;
182 if (t > (int64_t)devc->limit_msec) {
183 sr_info("Requested time limit reached.");
184 sdi->driver->dev_acquisition_stop(sdi, cb_data);
189 /* Request next packet, if required. */
190 if (sdi->status == SR_ST_ACTIVE) {
191 if (devc->reply_pending) {
192 elapsed_us = g_get_monotonic_time() - devc->req_sent_at;
193 if (elapsed_us > (REQ_TIMEOUT_MS * 1000))
194 devc->reply_pending = FALSE;
198 /* Send command to get voltage, current, and mode (CC or CV). */
199 if (send_cmd(serial, "GETD\r") < 0)
202 devc->req_sent_at = g_get_monotonic_time();
203 devc->reply_pending = TRUE;