From d730f70e06653c60d65c668cce02f358decce991 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?poljar=20=28Damir=20Jeli=C4=87=29?= Date: Fri, 1 Nov 2013 19:27:44 +0100 Subject: [PATCH] scpi: Add more functions (getting int/bool/float/double). This patch adds helper functions to read an SCPI response and parse the response as an integer, boolean, floating-point or double-precision floating-point number. --- hardware/common/scpi.c | 128 +++++++++++++++++++++++++++++++++++++++++ libsigrok-internal.h | 8 +++ 2 files changed, 136 insertions(+) diff --git a/hardware/common/scpi.c b/hardware/common/scpi.c index f932854f..03eea3ee 100644 --- a/hardware/common/scpi.c +++ b/hardware/common/scpi.c @@ -179,6 +179,134 @@ SR_PRIV int sr_scpi_get_string(struct sr_serial_dev_inst *serial, return ret; } +/** + * Send a SCPI command, read the reply, parse it as a bool value and store the + * result in scpi_response. + * + * @param serial Previously initialized serial port structure. + * @param command The SCPI command to send to the device (can be NULL). + * @param scpi_response Pointer where to store the parsed result. + * + * @return SR_OK on success, SR_ERR on failure. + */ +SR_PRIV int sr_scpi_get_bool(struct sr_serial_dev_inst *serial, + const char *command, gboolean *scpi_response) +{ + int ret; + char *response; + + response = NULL; + + if (sr_scpi_get_string(serial, command, &response) != SR_OK) + if (!response) + return SR_ERR; + + if (sr_parse_strict_bool(response, scpi_response) == SR_OK) + ret = SR_OK; + else + ret = SR_ERR; + + g_free(response); + + return ret; +} + +/** + * Send a SCPI command, read the reply, parse it as an integer and store the + * result in scpi_response. + * + * @param serial Previously initialized serial port structure. + * @param command The SCPI command to send to the device (can be NULL). + * @param scpi_response Pointer where to store the parsed result. + * + * @return SR_OK on success, SR_ERR on failure. + */ +SR_PRIV int sr_scpi_get_int(struct sr_serial_dev_inst *serial, + const char *command, int *scpi_response) +{ + int ret; + char *response; + + response = NULL; + + if (sr_scpi_get_string(serial, command, &response) != SR_OK) + if (!response) + return SR_ERR; + + if (sr_atoi(response, scpi_response) == SR_OK) + ret = SR_OK; + else + ret = SR_ERR; + + g_free(response); + + return ret; +} + +/** + * Send a SCPI command, read the reply, parse it as a float and store the + * result in scpi_response. + * + * @param serial Previously initialized serial port structure. + * @param command The SCPI command to send to the device (can be NULL). + * @param scpi_response Pointer where to store the parsed result. + * + * @return SR_OK on success, SR_ERR on failure. + */ +SR_PRIV int sr_scpi_get_float(struct sr_serial_dev_inst *serial, + const char *command, float *scpi_response) +{ + int ret; + char *response; + + response = NULL; + + if (sr_scpi_get_string(serial, command, &response) != SR_OK) + if (!response) + return SR_ERR; + + if (sr_atof(response, scpi_response) == SR_OK) + ret = SR_OK; + else + ret = SR_ERR; + + g_free(response); + + return ret; +} + +/** + * Send a SCPI command, read the reply, parse it as a double and store the + * result in scpi_response. + * + * @param serial Previously initialized serial port structure. + * @param command The SCPI command to send to the device (can be NULL). + * @param scpi_response Pointer where to store the parsed result. + * + * @return SR_OK on success, SR_ERR on failure. + */ +SR_PRIV int sr_scpi_get_double(struct sr_serial_dev_inst *serial, + const char *command, double *scpi_response) +{ + int ret; + char *response; + + response = NULL; + + if (sr_scpi_get_string(serial, command, &response) != SR_OK) + if (!response) + return SR_ERR; + + if (sr_atod(response, scpi_response) == SR_OK) + ret = SR_OK; + else + ret = SR_ERR; + + g_free(response); + + return ret; +} + /** * Send the *IDN? SCPI command, receive the reply, parse it and store the * reply as a sr_scpi_hw_info structure in the supplied scpi_response pointer. diff --git a/libsigrok-internal.h b/libsigrok-internal.h index be0114d6..fa270b4e 100644 --- a/libsigrok-internal.h +++ b/libsigrok-internal.h @@ -280,6 +280,14 @@ SR_PRIV int sr_scpi_send(struct sr_serial_dev_inst *serial, const char *command); SR_PRIV int sr_scpi_get_string(struct sr_serial_dev_inst *serial, const char *command, char **scpi_response); +SR_PRIV int sr_scpi_get_bool(struct sr_serial_dev_inst *serial, + const char *command, gboolean *scpi_response); +SR_PRIV int sr_scpi_get_int(struct sr_serial_dev_inst *serial, + const char *command, int *scpi_response); +SR_PRIV int sr_scpi_get_float(struct sr_serial_dev_inst *serial, + const char *command, float *scpi_response); +SR_PRIV int sr_scpi_get_double(struct sr_serial_dev_inst *serial, + const char *command, double *scpi_response); SR_PRIV int sr_scpi_get_hw_id(struct sr_serial_dev_inst *serial, struct sr_scpi_hw_info **scpi_reponse); SR_PRIV void sr_scpi_hw_info_free(struct sr_scpi_hw_info *hw_info);