2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2014 Matthias Heidbrink <m-sigrok@heidbrink.biz>
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 2 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, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 * <em>Manson HCS-3xxx Series</em> power supply driver
30 #define REQ_TIMEOUT_MS 500
32 SR_PRIV int hcs_send_cmd(struct sr_serial_dev_inst *serial, const char *cmd, ...)
40 vsnprintf(cmdbuf, sizeof(cmdbuf), cmd, args);
43 cmd_esc = g_strescape(cmdbuf, NULL);
44 sr_dbg("Sending '%s'.", cmd_esc);
47 if ((ret = serial_write_blocking(serial, cmdbuf, strlen(cmdbuf),
48 serial_timeout(serial, strlen(cmdbuf)))) < 0) {
49 sr_err("Error sending command: %d.", ret);
57 * Read data from interface into buffer blocking until @a lines number of \\r chars
59 * @param serial Previously initialized serial port structure.
60 * @param[in] lines Number of \\r-terminated lines to read (1-n).
61 * @param buf Buffer for result. Contents is NUL-terminated on success.
62 * @param[in] buflen Buffer length (>0).
63 * @retval SR_OK Lines received and ending with "OK\r" (success).
64 * @retval SR_ERR Error.
65 * @retval SR_ERR_ARG Invalid argument.
67 SR_PRIV int hcs_read_reply(struct sr_serial_dev_inst *serial, int lines, char *buf, int buflen)
73 if (!serial || (lines <= 0) || !buf || (buflen <= 0))
76 while ((l_recv < lines) && (bufpos < (buflen + 1))) {
77 retc = serial_read_blocking(serial, &buf[bufpos], 1, 0);
80 if (buf[bufpos] == '\r')
86 if ((l_recv == lines) && (g_str_has_suffix(buf, "OK\r")))
92 /** Interpret result of GETD command. */
93 SR_PRIV int hcs_parse_volt_curr_mode(struct sr_dev_inst *sdi, char **tokens)
97 struct dev_context *devc;
101 /* Bytes 0-3: Voltage. */
102 str = g_strndup(tokens[0], 4);
103 val = g_ascii_strtod(str, NULL) / 100;
107 /* Bytes 4-7: Current. */
108 str = g_strndup((tokens[0] + 4), 4);
109 val = g_ascii_strtod(str, NULL) / 100;
113 /* Byte 8: Mode ('0' means CV, '1' means CC). */
114 devc->cc_mode = (tokens[0][8] == '1');
116 /* Output enabled? Works because voltage cannot be set to 0.0 directly. */
117 devc->output_enabled = devc->voltage != 0.0;
122 static void send_sample(struct sr_dev_inst *sdi)
124 struct dev_context *devc;
125 struct sr_datafeed_packet packet;
126 struct sr_datafeed_analog_old analog;
130 packet.type = SR_DF_ANALOG_OLD;
131 packet.payload = &analog;
132 analog.channels = sdi->channels;
133 analog.num_samples = 1;
135 analog.mq = SR_MQ_VOLTAGE;
136 analog.unit = SR_UNIT_VOLT;
137 analog.mqflags = SR_MQFLAG_DC;
138 analog.data = &devc->voltage;
139 sr_session_send(sdi, &packet);
141 analog.mq = SR_MQ_CURRENT;
142 analog.unit = SR_UNIT_AMPERE;
144 analog.data = &devc->current;
145 sr_session_send(sdi, &packet);
148 sr_sw_limits_update_samples_read(&devc->limits, 1);
151 static int parse_reply(struct sr_dev_inst *sdi)
153 struct dev_context *devc;
154 char *reply_esc, **tokens;
159 reply_esc = g_strescape(devc->buf, NULL);
160 sr_dbg("Received '%s'.", reply_esc);
163 tokens = g_strsplit(devc->buf, "\r", 0);
164 retc = hcs_parse_volt_curr_mode(sdi, tokens);
174 static int handle_new_data(struct sr_dev_inst *sdi)
177 struct dev_context *devc;
178 struct sr_serial_dev_inst *serial;
183 len = serial_read_blocking(serial, devc->buf + devc->buflen, 1, 0);
188 devc->buf[devc->buflen] = '\0';
190 /* Wait until we received an "OK\r" (among other bytes). */
191 if (!g_str_has_suffix(devc->buf, "OK\r"))
199 devc->reply_pending = FALSE;
204 /** Driver/serial data reception function. */
205 SR_PRIV int hcs_receive_data(int fd, int revents, void *cb_data)
207 struct sr_dev_inst *sdi;
208 struct dev_context *devc;
209 struct sr_serial_dev_inst *serial;
214 if (!(sdi = cb_data))
217 if (!(devc = sdi->priv))
222 if (revents == G_IO_IN) {
223 /* New data arrived. */
224 handle_new_data(sdi);
229 if (sr_sw_limits_check(&devc->limits)) {
230 sdi->driver->dev_acquisition_stop(sdi);
234 /* Request next packet, if required. */
235 if (sdi->status == SR_ST_ACTIVE) {
236 if (devc->reply_pending) {
237 elapsed_us = g_get_monotonic_time() - devc->req_sent_at;
238 if (elapsed_us > (REQ_TIMEOUT_MS * 1000))
239 devc->reply_pending = FALSE;
243 /* Send command to get voltage, current, and mode (CC or CV). */
244 if (hcs_send_cmd(serial, "GETD\r") < 0)
247 devc->req_sent_at = g_get_monotonic_time();
248 devc->reply_pending = TRUE;