manson-hcs-3xxx: Cleanup, improved error handling, docs.
This commit is contained in:
parent
25abc8dd13
commit
9740d9bf8c
|
@ -19,6 +19,11 @@
|
|||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
/** @file
|
||||
* <em>Manson HCS-3xxx Series</em> power supply driver
|
||||
* @internal
|
||||
*/
|
||||
|
||||
#include "protocol.h"
|
||||
|
||||
static const int32_t hwopts[] = {
|
||||
|
@ -87,6 +92,7 @@ static GSList *scan(GSList *options)
|
|||
devices = NULL;
|
||||
conn = NULL;
|
||||
serialcomm = NULL;
|
||||
devc = NULL;
|
||||
|
||||
for (l = options; l; l = l->next) {
|
||||
src = l->data;
|
||||
|
@ -120,8 +126,9 @@ static GSList *scan(GSList *options)
|
|||
|
||||
/* Get the device model. */
|
||||
memset(&reply, 0, sizeof(reply));
|
||||
serial_write_blocking(serial, "GMOD\r", 5);
|
||||
serial_read_blocking(serial, &reply, 8);
|
||||
if ((hcs_send_cmd(serial, "GMOD\r") < 0) ||
|
||||
(hcs_read_reply(serial, 2, reply, sizeof(reply)) < 0))
|
||||
return NULL;
|
||||
tokens = g_strsplit((const gchar *)&reply, "\r", 2);
|
||||
|
||||
model_id = -1;
|
||||
|
@ -129,26 +136,27 @@ static GSList *scan(GSList *options)
|
|||
if (!strcmp(models[i].id, tokens[0]))
|
||||
model_id = i;
|
||||
}
|
||||
g_strfreev(tokens);
|
||||
|
||||
if (model_id < 0) {
|
||||
sr_err("Unknown model id '%s' detected, aborting.", tokens[0]);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Init device instance, etc. */
|
||||
if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Manson",
|
||||
models[model_id].name, NULL))) {
|
||||
sr_err("Failed to create device instance.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
g_strfreev(tokens);
|
||||
|
||||
sdi->inst_type = SR_INST_SERIAL;
|
||||
sdi->conn = serial;
|
||||
sdi->driver = di;
|
||||
|
||||
if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "CH1"))) {
|
||||
sr_err("Failed to create channel.");
|
||||
return NULL;
|
||||
goto exit_err;
|
||||
}
|
||||
sdi->channels = g_slist_append(sdi->channels, ch);
|
||||
|
||||
|
@ -157,6 +165,14 @@ static GSList *scan(GSList *options)
|
|||
|
||||
sdi->priv = devc;
|
||||
|
||||
/* Get current device status. */
|
||||
if ((hcs_send_cmd(serial, "GETD\r") < 0) ||
|
||||
(hcs_read_reply(serial, 2, reply, sizeof(reply)) < 0))
|
||||
return NULL;
|
||||
tokens = g_strsplit((const gchar *)&reply, "\r", 2);
|
||||
if (hcs_parse_volt_curr_mode(sdi, tokens) < 0)
|
||||
goto exit_err;
|
||||
|
||||
drvc->instances = g_slist_append(drvc->instances, sdi);
|
||||
devices = g_slist_append(devices, sdi);
|
||||
|
||||
|
@ -165,6 +181,12 @@ static GSList *scan(GSList *options)
|
|||
sr_serial_dev_inst_free(serial);
|
||||
|
||||
return devices;
|
||||
|
||||
exit_err:
|
||||
sr_dev_inst_free(sdi);
|
||||
if (devc)
|
||||
g_free(devc);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static GSList *dev_list(void)
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
* This file is part of the libsigrok project.
|
||||
*
|
||||
* Copyright (C) 2014 Uwe Hermann <uwe@hermann-uwe.de>
|
||||
* Copyright (C) 2014 Matthias Heidbrink <m-sigrok@heidbrink.biz>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
|
@ -18,30 +19,76 @@
|
|||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
/** @file
|
||||
* <em>Manson HCS-3xxx Series</em> power supply driver
|
||||
* @internal
|
||||
*/
|
||||
|
||||
#include "protocol.h"
|
||||
|
||||
#define REQ_TIMEOUT_MS 500
|
||||
|
||||
static int send_cmd(struct sr_serial_dev_inst *serial, const char *cmd)
|
||||
SR_PRIV int hcs_send_cmd(struct sr_serial_dev_inst *serial, const char *cmd, ...)
|
||||
{
|
||||
int ret;
|
||||
char cmdbuf[50];
|
||||
char *cmd_esc;
|
||||
va_list args;
|
||||
|
||||
cmd_esc = g_strescape(cmd, NULL);
|
||||
va_start(args, cmd);
|
||||
vsnprintf(cmdbuf, sizeof(cmdbuf), cmd, args);
|
||||
va_end(args);
|
||||
|
||||
cmd_esc = g_strescape(cmdbuf, NULL);
|
||||
sr_dbg("Sending '%s'.", cmd_esc);
|
||||
g_free(cmd_esc);
|
||||
|
||||
if ((ret = serial_write_blocking(serial, cmd, strlen(cmd))) < 0) {
|
||||
if ((ret = serial_write_blocking(serial, cmdbuf, strlen(cmdbuf))) < 0) {
|
||||
sr_err("Error sending command: %d.", ret);
|
||||
g_free(cmd_esc);
|
||||
return ret;
|
||||
}
|
||||
|
||||
g_free(cmd_esc);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int parse_volt_curr_mode(struct sr_dev_inst *sdi, char **tokens)
|
||||
/**
|
||||
* Read data from interface into buffer blocking until @a lines number of \\r chars
|
||||
* received.
|
||||
* @param serial Previously initialized serial port structure.
|
||||
* @param[in] lines Number of \\r-terminated lines to read (1-n).
|
||||
* @param buf Buffer for result. Contents is NUL-terminated on success.
|
||||
* @param[in] buflen Buffer length (>0).
|
||||
* @retval SR_OK Lines received and ending with "OK\r" (success).
|
||||
* @retval SR_ERR Error.
|
||||
* @retval SR_ERR_ARG Invalid argument.
|
||||
*/
|
||||
SR_PRIV int hcs_read_reply(struct sr_serial_dev_inst *serial, int lines, char* buf, int buflen)
|
||||
{
|
||||
int l_recv = 0;
|
||||
int bufpos = 0;
|
||||
int retc;
|
||||
|
||||
if (!serial || (lines <= 0) || !buf || (buflen <= 0))
|
||||
return SR_ERR_ARG;
|
||||
|
||||
while ((l_recv < lines) && (bufpos < (buflen + 1))) {
|
||||
retc = serial_read_blocking(serial, &buf[bufpos], 1);
|
||||
if (retc != 1)
|
||||
return SR_ERR;
|
||||
if (buf[bufpos] == '\r')
|
||||
l_recv++;
|
||||
bufpos++;
|
||||
}
|
||||
buf[bufpos] = '\0';
|
||||
|
||||
if ((l_recv == lines) && (g_str_has_suffix(buf, "OK\r")))
|
||||
return SR_OK;
|
||||
else
|
||||
return SR_ERR;
|
||||
}
|
||||
|
||||
/** Interpret result of GETD command. */
|
||||
SR_PRIV int hcs_parse_volt_curr_mode(struct sr_dev_inst *sdi, char **tokens)
|
||||
{
|
||||
char *str;
|
||||
double val;
|
||||
|
@ -64,6 +111,9 @@ static int parse_volt_curr_mode(struct sr_dev_inst *sdi, char **tokens)
|
|||
/* Byte 8: Mode ('0' means CV, '1' means CC). */
|
||||
devc->cc_mode = (tokens[0][8] == '1');
|
||||
|
||||
/* Output enabled? Works because voltage cannot be set to 0.0 directly. */
|
||||
devc->output_enabled = devc->voltage != 0.0;
|
||||
|
||||
return SR_OK;
|
||||
}
|
||||
|
||||
|
@ -99,20 +149,21 @@ static int parse_reply(struct sr_dev_inst *sdi)
|
|||
{
|
||||
struct dev_context *devc;
|
||||
char *reply_esc, **tokens;
|
||||
int retc;
|
||||
|
||||
devc = sdi->priv;
|
||||
|
||||
reply_esc = g_strescape(devc->buf, NULL);
|
||||
sr_dbg("Received '%s'.", reply_esc);
|
||||
g_free(reply_esc);
|
||||
|
||||
tokens = g_strsplit(devc->buf, "\r", 0);
|
||||
|
||||
if (parse_volt_curr_mode(sdi, tokens) < 0)
|
||||
return SR_ERR;
|
||||
send_sample(sdi);
|
||||
|
||||
g_free(reply_esc);
|
||||
retc = hcs_parse_volt_curr_mode(sdi, tokens);
|
||||
g_strfreev(tokens);
|
||||
if (retc < 0)
|
||||
return SR_ERR;
|
||||
|
||||
send_sample(sdi);
|
||||
|
||||
return SR_OK;
|
||||
}
|
||||
|
@ -147,6 +198,7 @@ static int handle_new_data(struct sr_dev_inst *sdi)
|
|||
return SR_OK;
|
||||
}
|
||||
|
||||
/** Driver/serial data reception function. */
|
||||
SR_PRIV int hcs_receive_data(int fd, int revents, void *cb_data)
|
||||
{
|
||||
struct sr_dev_inst *sdi;
|
||||
|
@ -196,7 +248,7 @@ SR_PRIV int hcs_receive_data(int fd, int revents, void *cb_data)
|
|||
}
|
||||
|
||||
/* Send command to get voltage, current, and mode (CC or CV). */
|
||||
if (send_cmd(serial, "GETD\r") < 0)
|
||||
if (hcs_send_cmd(serial, "GETD\r") < 0)
|
||||
return TRUE;
|
||||
|
||||
devc->req_sent_at = g_get_monotonic_time();
|
||||
|
|
|
@ -19,6 +19,11 @@
|
|||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
/** @file
|
||||
* <em>Manson HCS-3xxx Series</em> power supply driver
|
||||
* @internal
|
||||
*/
|
||||
|
||||
#ifndef LIBSIGROK_HARDWARE_MANSON_HCS_3XXX_PROTOCOL_H
|
||||
#define LIBSIGROK_HARDWARE_MANSON_HCS_3XXX_PROTOCOL_H
|
||||
|
||||
|
@ -50,6 +55,7 @@ enum {
|
|||
MANSON_HCS_3604,
|
||||
};
|
||||
|
||||
/** Information on a single model. */
|
||||
struct hcs_model {
|
||||
int model_id; /**< Model info */
|
||||
char *name; /**< Model name */
|
||||
|
@ -60,7 +66,7 @@ struct hcs_model {
|
|||
|
||||
/** Private, per-device-instance driver context. */
|
||||
struct dev_context {
|
||||
struct hcs_model *model;
|
||||
struct hcs_model *model; /**< Model informaion. */
|
||||
|
||||
uint64_t limit_samples;
|
||||
uint64_t limit_msec;
|
||||
|
@ -71,14 +77,19 @@ struct dev_context {
|
|||
|
||||
void *cb_data;
|
||||
|
||||
float voltage;
|
||||
float current;
|
||||
gboolean cc_mode;
|
||||
float voltage; /**< Last voltage value [V] read from device. */
|
||||
float current; /**< Last current value [A] read from device. */
|
||||
gboolean cc_mode; /**< Device is in constant current mode (otherwise constant voltage). */
|
||||
|
||||
gboolean output_enabled; /**< Is the output enabled? */
|
||||
|
||||
char buf[50];
|
||||
int buflen;
|
||||
};
|
||||
|
||||
SR_PRIV int hcs_parse_volt_curr_mode(struct sr_dev_inst *sdi, char **tokens);
|
||||
SR_PRIV int hcs_read_reply(struct sr_serial_dev_inst *serial, int lines, char* buf, int buflen);
|
||||
SR_PRIV int hcs_send_cmd(struct sr_serial_dev_inst *serial, const char *cmd, ...);
|
||||
SR_PRIV int hcs_receive_data(int fd, int revents, void *cb_data);
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue