/** * @private * * Convert a string representation of a numeric value to a float. The * conversion is strict and will fail if the complete string does not represent * a valid float. The function sets errno according to the details of the * failure. * * @param str The string representation to convert. * @param ret Pointer to float where the result of the conversion will be stored. * * @retval SR_OK Conversion successful. * @retval SR_ERR Failure. * * @since 0.3.0 */ SR_PRIV int sr_atof(const char *str, float *ret) { double tmp; if (sr_atod(str, &tmp) != SR_OK) return SR_ERR; if ((float) tmp != tmp) { errno = ERANGE; return SR_ERR; } *ret = (float) tmp; return SR_OK; }
/** * Send a SCPI command, read the reply, parse it as a double and store the * result in scpi_response. * * @param scpi Previously initialised SCPI device 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_scpi_dev_inst *scpi, const char *command, double *scpi_response) { int ret; char *response; response = NULL; ret = sr_scpi_get_string(scpi, command, &response); if (ret != SR_OK && !response) return ret; if (sr_atod(response, scpi_response) == SR_OK) ret = SR_OK; else ret = SR_ERR_DATA; g_free(response); return ret; }
/** Process a complete line (without CR/LF) in buf. */ static void process_line(struct sr_dev_inst *sdi) { struct dev_context *devc; double dbl; int auxint; devc = sdi->priv; switch (devc->acq_req_pending) { case 0: /* Should not happen... */ break; case 1: /* Waiting for data reply to request */ /* Convert numbers */ switch (devc->acq_req) { case AQ_U1: case AQ_U2: case AQ_I1: case AQ_I2: if (sr_atod(devc->buf, &dbl) != SR_OK) { sr_err("Failed to convert '%s' to double, errno=%d %s", devc->buf, errno, g_strerror(errno)); dbl = 0.0; } break; case AQ_STATUS: if (sr_atoi(devc->buf, &auxint) != SR_OK) { sr_err("Failed to convert '%s' to int, errno=%d %s", devc->buf, errno, g_strerror(errno)); auxint = 0; } break; default: break; } switch (devc->acq_req) { case AQ_U1: devc->channel_status[0].output_voltage_last = dbl; break; case AQ_I1: devc->channel_status[0].output_current_last = dbl; break; case AQ_U2: devc->channel_status[1].output_voltage_last = dbl; break; case AQ_I2: devc->channel_status[1].output_current_last = dbl; break; case AQ_STATUS: /* Process status and generate data. */ if (lps_process_status(sdi, auxint) == SR_OK) { send_data(sdi); } break; default: break; } devc->acq_req_pending = 2; break; case 2: /* Waiting for OK after request */ if (strcmp(devc->buf, "OK")) { sr_err("Unexpected reply while waiting for OK: '%s'", devc->buf); } devc->acq_req_pending = 0; break; } devc->buf[0] = '\0'; devc->buflen = 0; }