예제 #1
0
static int scpi_usbtmc_libusb_read_data(void *priv, char *buf, int maxlen)
{
	struct scpi_usbtmc_libusb *uscpi = priv;
	int read_length;

	if (uscpi->response_bytes_read >= uscpi->response_length) {
		if (uscpi->remaining_length > 0) {
			if (scpi_usbtmc_bulkin_continue(uscpi, uscpi->buffer,
			                                sizeof(uscpi->buffer)) <= 0)
				return SR_ERR;
		} else {
			if (uscpi->bulkin_attributes & EOM)
				return SR_ERR;
			if (scpi_usbtmc_libusb_read_begin(uscpi) < 0)
				return SR_ERR;
		}
	}

	read_length = MIN(uscpi->response_length - uscpi->response_bytes_read, maxlen);

	memcpy(buf, uscpi->buffer + uscpi->response_bytes_read, read_length);

	uscpi->response_bytes_read += read_length;

	return read_length;
}
QByteArray USBTMC::read_answer() {
    assert(uscpi.usb.devhdl);
    int ret;
    QByteArray response;

    if (scpi_usbtmc_libusb_read_begin(&uscpi) != SR_OK) {
        return {};
        //SR_ERR;
    }

    auto timeout_until = std::chrono::high_resolution_clock::now() + this->timeout;

    while (!scpi_usbtmc_libusb_read_complete(&uscpi)) {
        /* Read another chunk of the response. */
        ret = sr_scpi_read_response(response, timeout_until);

        if (ret < 0) {
            return {};
        }
        if (ret > 0) {
            timeout_until = std::chrono::high_resolution_clock::now() + this->timeout;
        }
    }

    return response;
}