tondaj-sl-814: use new serial API

This commit is contained in:
Bert Vermeulen 2012-11-12 03:00:32 +01:00
parent f306ca61f6
commit 19ee7dff78
3 changed files with 17 additions and 44 deletions

View File

@ -24,6 +24,8 @@
#include "libsigrok-internal.h" #include "libsigrok-internal.h"
#include "protocol.h" #include "protocol.h"
#define SERIALCOMM "9600/8e1"
static const int hwopts[] = { static const int hwopts[] = {
SR_HWOPT_CONN, SR_HWOPT_CONN,
SR_HWOPT_SERIALCOMM, SR_HWOPT_SERIALCOMM,
@ -121,6 +123,8 @@ static GSList *hw_scan(GSList *options)
sr_dbg("Couldn't determine connection options."); sr_dbg("Couldn't determine connection options.");
return NULL; return NULL;
} }
if (!serialcomm)
serialcomm = SERIALCOMM;
if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Tondaj", if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Tondaj",
"SL-814", NULL))) { "SL-814", NULL))) {
@ -133,14 +137,12 @@ static GSList *hw_scan(GSList *options)
return NULL; return NULL;
} }
if (!serialcomm) if (!(devc->serial = sr_serial_dev_inst_new(conn, serialcomm)))
serialcomm = "9600/8e1";
if (!(devc->serial = sr_serial_dev_inst_new(conn, -1))) {
sr_err("Failed to create serial device instance.");
return NULL; return NULL;
}
devc->serialcomm = g_strdup(serialcomm); if (serial_open(devc->serial, O_RDWR|O_NONBLOCK) != SR_OK)
return NULL;
sdi->priv = devc; sdi->priv = devc;
sdi->driver = di; sdi->driver = di;
probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, probe_names[0]); probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, probe_names[0]);
@ -166,25 +168,12 @@ 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;
devc = sdi->priv; devc = sdi->priv;
sr_dbg("Opening '%s' with '%s'.", devc->serial->port, devc->serialcomm); if (serial_open(devc->serial, O_RDWR|O_NONBLOCK) != SR_OK)
ret = serial_open(devc->serial->port, O_RDWR | O_NONBLOCK);
if (ret < 0) {
sr_err("Unable to open serial port: %d.", ret);
return SR_ERR; return SR_ERR;
}
devc->serial->fd = ret;
ret = serial_set_paramstr(devc->serial->fd, devc->serialcomm);
if (ret < 0) {
sr_err("Unable to set serial parameters: %d.", ret);
return SR_ERR;
}
sdi->status = SR_ST_ACTIVE; sdi->status = SR_ST_ACTIVE;
@ -193,29 +182,15 @@ static int hw_dev_open(struct sr_dev_inst *sdi)
static int hw_dev_close(struct sr_dev_inst *sdi) static int hw_dev_close(struct sr_dev_inst *sdi)
{ {
int ret;
struct dev_context *devc; struct dev_context *devc;
devc = sdi->priv; devc = sdi->priv;
if (!devc->serial) { if (devc->serial && devc->serial->fd != -1) {
sr_err("Invalid serial device instance, cannot close."); serial_close(devc->serial);
return SR_ERR_BUG; sdi->status = SR_ST_INACTIVE;
} }
if (devc->serial->fd == -1) {
sr_dbg("Serial device instance FD was -1, no need to close.");
return SR_OK;
}
if ((ret = serial_close(devc->serial->fd)) < 0) {
sr_err("Error closing serial port: %d.", ret);
return SR_ERR;
}
devc->serial->fd = -1;
sdi->status = SR_ST_INACTIVE;
return SR_OK; return SR_OK;
} }

View File

@ -127,14 +127,14 @@ int tondaj_sl_814_receive_data(int fd, int revents, void *cb_data)
buf[2] = 0x0d; buf[2] = 0x0d;
sr_spew("Sending init command: %02x %02x %02x.", sr_spew("Sending init command: %02x %02x %02x.",
buf[0], buf[1], buf[2]); buf[0], buf[1], buf[2]);
if ((ret = serial_write(fd, buf, 3)) < 0) { if ((ret = serial_write(devc->serial, buf, 3)) < 0) {
sr_err("Error sending init command: %d.", ret); sr_err("Error sending init command: %d.", ret);
return FALSE; return FALSE;
} }
devc->state = GET_INIT_REPLY; devc->state = GET_INIT_REPLY;
} else if (devc->state == GET_INIT_REPLY) { } else if (devc->state == GET_INIT_REPLY) {
/* If we just sent the "init" command, get its reply. */ /* If we just sent the "init" command, get its reply. */
if ((ret = serial_read(fd, buf, 2)) < 0) { if ((ret = serial_read(devc->serial, buf, 2)) < 0) {
sr_err("Error reading init reply: %d.", ret); sr_err("Error reading init reply: %d.", ret);
return FALSE; return FALSE;
} }
@ -153,7 +153,7 @@ int tondaj_sl_814_receive_data(int fd, int revents, void *cb_data)
buf[2] = 0x0d; buf[2] = 0x0d;
sr_spew("Sending data request command: %02x %02x %02x.", sr_spew("Sending data request command: %02x %02x %02x.",
buf[0], buf[1], buf[2]); buf[0], buf[1], buf[2]);
if ((ret = serial_write(fd, buf, 3)) < 0) { if ((ret = serial_write(devc->serial, buf, 3)) < 0) {
sr_err("Error sending request command: %d.", ret); sr_err("Error sending request command: %d.", ret);
return FALSE; return FALSE;
} }
@ -161,7 +161,7 @@ int tondaj_sl_814_receive_data(int fd, int revents, void *cb_data)
devc->state = GET_PACKET; devc->state = GET_PACKET;
} else if (devc->state == GET_PACKET) { } else if (devc->state == GET_PACKET) {
/* Read a packet from the device. */ /* Read a packet from the device. */
ret = serial_read(fd, devc->buf + devc->buflen, ret = serial_read(devc->serial, devc->buf + devc->buflen,
4 - devc->buflen); 4 - devc->buflen);
if (ret < 0) { if (ret < 0) {
sr_err("Error reading packet: %d.", ret); sr_err("Error reading packet: %d.", ret);

View File

@ -50,8 +50,6 @@ struct dev_context {
struct sr_serial_dev_inst *serial; struct sr_serial_dev_inst *serial;
char *serialcomm;
int state; int state;
uint8_t buf[4]; uint8_t buf[4];