X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fscpi%2Fscpi_serial.c;h=6c4e63c7b9122756c7bfaaf2983d9e80203f4489;hb=09f24ef2a98d430717929f0476843d3f788ff36a;hp=cad41381595fb5f4739c88a9d6e7428adcfcc759;hpb=aff94d065e3ef50fa022b3e79b0dc435ae2c5b0a;p=libsigrok.git
diff --git a/src/scpi/scpi_serial.c b/src/scpi/scpi_serial.c
index cad41381..6c4e63c7 100644
--- a/src/scpi/scpi_serial.c
+++ b/src/scpi/scpi_serial.c
@@ -18,6 +18,7 @@
* along with this program. If not, see .
*/
+#include
#include
#include
#include
@@ -27,13 +28,9 @@
#define LOG_PREFIX "scpi_serial"
-#define BUFFER_SIZE 1024
-
struct scpi_serial {
struct sr_serial_dev_inst *serial;
- char buffer[BUFFER_SIZE];
- size_t count;
- size_t read;
+ gboolean got_newline;
};
static const struct {
@@ -43,6 +40,7 @@ static const struct {
} scpi_serial_usb_ids[] = {
{ 0x0403, 0xed72, "115200/8n1/flow=1" }, /* Hameg HO720 */
{ 0x0403, 0xed73, "115200/8n1/flow=1" }, /* Hameg HO730 */
+ { 0x0aad, 0x0118, "115200/8n1" }, /* R&S HMO1002 */
};
static GSList *scpi_serial_scan(struct drv_context *drvc)
@@ -84,9 +82,9 @@ static int scpi_serial_dev_inst_new(void *priv, struct drv_context *drvc,
return SR_OK;
}
-static int scpi_serial_open(void *priv)
+static int scpi_serial_open(struct sr_scpi_dev_inst *scpi)
{
- struct scpi_serial *sscpi = priv;
+ struct scpi_serial *sscpi = scpi->priv;
struct sr_serial_dev_inst *serial = sscpi->serial;
if (serial_open(serial, SERIAL_RDWR) != SR_OK)
@@ -95,8 +93,7 @@ static int scpi_serial_open(void *priv)
if (serial_flush(serial) != SR_OK)
return SR_ERR;
- sscpi->count = 0;
- sscpi->read = 0;
+ sscpi->got_newline = FALSE;
return SR_OK;
}
@@ -121,26 +118,21 @@ static int scpi_serial_source_remove(struct sr_session *session, void *priv)
static int scpi_serial_send(void *priv, const char *command)
{
int len, result, written;
- gchar *terminated_command;
struct scpi_serial *sscpi = priv;
struct sr_serial_dev_inst *serial = sscpi->serial;
- terminated_command = g_strconcat(command, "\n", NULL);
- len = strlen(terminated_command);
+ len = strlen(command);
written = 0;
while (written < len) {
result = serial_write_nonblocking(serial,
- terminated_command + written, len - written);
+ command + written, len - written);
if (result < 0) {
sr_err("Error while sending SCPI command: '%s'.", command);
- g_free(terminated_command);
return SR_ERR;
}
written += result;
}
- g_free(terminated_command);
-
sr_spew("Successfully sent SCPI command: '%s'.", command);
return SR_OK;
@@ -148,7 +140,8 @@ static int scpi_serial_send(void *priv, const char *command)
static int scpi_serial_read_begin(void *priv)
{
- (void) priv;
+ struct scpi_serial *sscpi = priv;
+ sscpi->got_newline = FALSE;
return SR_OK;
}
@@ -156,61 +149,38 @@ static int scpi_serial_read_begin(void *priv)
static int scpi_serial_read_data(void *priv, char *buf, int maxlen)
{
struct scpi_serial *sscpi = priv;
- int len, ret;
-
- len = BUFFER_SIZE - sscpi->count;
+ int ret;
- /* Try to read new data into the buffer if there is space. */
- if (len > 0) {
- ret = serial_read_nonblocking(sscpi->serial, sscpi->buffer + sscpi->count,
- BUFFER_SIZE - sscpi->count);
+ /* Try to read new data into the buffer. */
+ ret = serial_read_nonblocking(sscpi->serial, buf, maxlen);
- if (ret < 0)
- return ret;
+ if (ret < 0)
+ return ret;
- sscpi->count += ret;
+ if (ret > 0) {
+ sr_spew("Read %d bytes into buffer.", ret);
- if (ret > 0)
- sr_spew("Read %d bytes into buffer.", ret);
- }
-
- /* Return as many bytes as possible from buffer, excluding any trailing newline. */
- if (sscpi->read < sscpi->count) {
- len = sscpi->count - sscpi->read;
- if (len > maxlen)
- len = maxlen;
- if (sscpi->buffer[sscpi->read + len - 1] == '\n')
- len--;
- sr_spew("Returning %d bytes from buffer.", len);
- memcpy(buf, sscpi->buffer + sscpi->read, len);
- sscpi->read += len;
- if (sscpi->read == BUFFER_SIZE) {
- sr_spew("Resetting buffer.");
- sscpi->count = 0;
- sscpi->read = 0;
+ if (buf[ret - 1] == '\n') {
+ sscpi->got_newline = TRUE;
+ sr_spew("Received terminator");
+ } else {
+ sscpi->got_newline = FALSE;
}
- return len;
}
- return 0;
+ return ret;
}
static int scpi_serial_read_complete(void *priv)
{
struct scpi_serial *sscpi = priv;
- /* If the next character is a newline, discard it and report complete. */
- if (sscpi->read < sscpi->count && sscpi->buffer[sscpi->read] == '\n') {
- sscpi->read++;
- return 1;
- } else {
- return 0;
- }
+ return sscpi->got_newline;
}
-static int scpi_serial_close(void *priv)
+static int scpi_serial_close(struct sr_scpi_dev_inst *scpi)
{
- struct scpi_serial *sscpi = priv;
+ struct scpi_serial *sscpi = scpi->priv;
return serial_close(sscpi->serial);
}