tekpower-dmm: use new serial API

This commit is contained in:
Bert Vermeulen 2012-11-12 12:50:42 +01:00
parent 401476daaf
commit d35afa87c9
3 changed files with 20 additions and 30 deletions

View File

@ -28,6 +28,8 @@
#include "libsigrok-internal.h" #include "libsigrok-internal.h"
#include "protocol.h" #include "protocol.h"
#define SERIALCOMM "2400/8n1"
static const int hwopts[] = { static const int hwopts[] = {
SR_HWOPT_CONN, SR_HWOPT_CONN,
SR_HWOPT_SERIALCOMM, SR_HWOPT_SERIALCOMM,
@ -95,18 +97,16 @@ static GSList *lcd14_scan(const char *conn, const char *serialcomm)
struct drv_context *drvc; struct drv_context *drvc;
struct dev_context *devc; struct dev_context *devc;
struct sr_probe *probe; struct sr_probe *probe;
struct sr_serial_dev_inst *serial;
GSList *devices; GSList *devices;
int i, len, fd, retry, good_packets = 0, dropped, ret; int i, len, retry, good_packets = 0, dropped;
uint8_t buf[128], *b; uint8_t buf[128], *b;
if ((fd = serial_open(conn, O_RDONLY | O_NONBLOCK)) == -1) { if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
sr_err("Unable to open %s: %s.", conn, strerror(errno));
return NULL; return NULL;
}
if ((ret = serial_set_paramstr(fd, serialcomm)) != SR_OK) { if (serial_open(serial, O_RDONLY|O_NONBLOCK) != SR_OK)
sr_err("Unable to set serial parameters: %d", ret);
return NULL; return NULL;
}
sr_info("Probing port %s readonly.", conn); sr_info("Probing port %s readonly.", conn);
@ -114,7 +114,7 @@ static GSList *lcd14_scan(const char *conn, const char *serialcomm)
b = buf; b = buf;
retry = 0; retry = 0;
devices = NULL; devices = NULL;
serial_flush(fd); serial_flush(serial);
/* /*
* There's no way to get an ID from the multimeter. It just sends data * There's no way to get an ID from the multimeter. It just sends data
@ -126,7 +126,7 @@ static GSList *lcd14_scan(const char *conn, const char *serialcomm)
/* Let's get a bit of data and see if we can find a packet. */ /* Let's get a bit of data and see if we can find a packet. */
len = sizeof(buf); len = sizeof(buf);
serial_readline(fd, (char **)&b, &len, 500); serial_readline(serial, (char **)&b, &len, 500);
if ((len == 0) || (len < FS9721_PACKET_SIZE)) { if ((len == 0) || (len < FS9721_PACKET_SIZE)) {
/* Not enough data received, is the DMM connected? */ /* Not enough data received, is the DMM connected? */
continue; continue;
@ -165,8 +165,7 @@ static GSList *lcd14_scan(const char *conn, const char *serialcomm)
return NULL; return NULL;
} }
devc->serial = sr_serial_dev_inst_new(conn, -1); devc->serial = serial;
devc->serialcomm = g_strdup(serialcomm);
sdi->priv = devc; sdi->priv = devc;
sdi->driver = di; sdi->driver = di;
@ -178,7 +177,7 @@ static GSList *lcd14_scan(const char *conn, const char *serialcomm)
break; break;
} }
serial_close(fd); serial_close(serial);
return devices; return devices;
} }
@ -207,8 +206,8 @@ static GSList *hw_scan(GSList *options)
/* Use the provided comm specs. */ /* Use the provided comm specs. */
devices = lcd14_scan(conn, serialcomm); devices = lcd14_scan(conn, serialcomm);
} else { } else {
/* Try the default 2400/8n1. */ /* Try the default. */
devices = lcd14_scan(conn, "2400/8n1"); devices = lcd14_scan(conn, SERIALCOMM);
} }
return devices; return devices;
@ -225,7 +224,6 @@ static GSList *hw_dev_list(void)
static int hw_dev_open(struct sr_dev_inst *sdi) static int hw_dev_open(struct sr_dev_inst *sdi)
{ {
int ret;
struct dev_context *devc; struct dev_context *devc;
if (!(devc = sdi->priv)) { if (!(devc = sdi->priv)) {
@ -233,17 +231,9 @@ static int hw_dev_open(struct sr_dev_inst *sdi)
return SR_ERR_BUG; return SR_ERR_BUG;
} }
devc->serial->fd = serial_open(devc->serial->port, O_RDONLY); if (serial_open(devc->serial, O_RDONLY) != SR_OK)
if (devc->serial->fd == -1) {
sr_err("Couldn't open serial port '%s'.", devc->serial->port);
return SR_ERR; return SR_ERR;
}
ret = serial_set_paramstr(devc->serial->fd, devc->serialcomm);
if (ret != SR_OK) {
sr_err("Unable to set serial parameters: %d.", ret);
return SR_ERR;
}
sdi->status = SR_ST_ACTIVE; sdi->status = SR_ST_ACTIVE;
return SR_OK; return SR_OK;
@ -259,8 +249,7 @@ static int hw_dev_close(struct sr_dev_inst *sdi)
} }
if (devc->serial && devc->serial->fd != -1) { if (devc->serial && devc->serial->fd != -1) {
serial_close(devc->serial->fd); serial_close(devc->serial);
devc->serial->fd = -1;
sdi->status = SR_ST_INACTIVE; sdi->status = SR_ST_INACTIVE;
} }

View File

@ -73,13 +73,13 @@ static void fs9721_serial_handle_packet(const uint8_t *buf,
g_free(analog); g_free(analog);
} }
static void handle_new_data(struct dev_context *devc, int fd) static void handle_new_data(struct dev_context *devc)
{ {
int len, i, offset = 0; int len, i, offset = 0;
/* Try to get as much data as the buffer can hold. */ /* Try to get as much data as the buffer can hold. */
len = DMM_BUFSIZE - devc->buflen; len = DMM_BUFSIZE - devc->buflen;
len = serial_read(fd, devc->buf + devc->buflen, len); len = serial_read(devc->serial, devc->buf + devc->buflen, len);
if (len < 1) { if (len < 1) {
sr_err("Serial port read error: %d.", len); sr_err("Serial port read error: %d.", len);
return; return;
@ -107,6 +107,8 @@ SR_PRIV int tekpower_dmm_receive_data(int fd, int revents, void *cb_data)
struct sr_dev_inst *sdi; struct sr_dev_inst *sdi;
struct dev_context *devc; struct dev_context *devc;
(void)fd;
if (!(sdi = cb_data)) if (!(sdi = cb_data))
return TRUE; return TRUE;
@ -115,7 +117,7 @@ SR_PRIV int tekpower_dmm_receive_data(int fd, int revents, void *cb_data)
if (revents == G_IO_IN) { if (revents == G_IO_IN) {
/* Serial data arrived. */ /* Serial data arrived. */
handle_new_data(devc, fd); handle_new_data(devc);
} }
if (devc->num_samples >= devc->limit_samples) { if (devc->num_samples >= devc->limit_samples) {

View File

@ -43,7 +43,6 @@ struct dev_context {
uint64_t num_samples; uint64_t num_samples;
struct sr_serial_dev_inst *serial; struct sr_serial_dev_inst *serial;
char *serialcomm;
uint8_t buf[DMM_BUFSIZE]; uint8_t buf[DMM_BUFSIZE];
int bufoffset; int bufoffset;