MinGW/Windows: Serial port portability fixes.
Add serial_read()/serial_write() which have different implementations on MinGW/Windows. Add some more error code handling and documentation.
This commit is contained in:
parent
ba3d481bb7
commit
2119ab0364
|
@ -51,11 +51,14 @@ char *serial_port_glob[] = {
|
||||||
|
|
||||||
GSList *list_serial_ports(void)
|
GSList *list_serial_ports(void)
|
||||||
{
|
{
|
||||||
|
GSList *ports;
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
/* TODO */
|
/* TODO */
|
||||||
|
ports = NULL;
|
||||||
|
ports = g_slist_append(ports, strdup("COM1"));
|
||||||
#else
|
#else
|
||||||
glob_t g;
|
glob_t g;
|
||||||
GSList *ports;
|
|
||||||
unsigned int i, j;
|
unsigned int i, j;
|
||||||
|
|
||||||
ports = NULL;
|
ports = NULL;
|
||||||
|
@ -66,9 +69,9 @@ GSList *list_serial_ports(void)
|
||||||
ports = g_slist_append(ports, strdup(g.gl_pathv[j]));
|
ports = g_slist_append(ports, strdup(g.gl_pathv[j]));
|
||||||
globfree(&g);
|
globfree(&g);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
return ports;
|
return ports;
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int serial_open(const char *pathname, int flags)
|
int serial_open(const char *pathname, int flags)
|
||||||
|
@ -86,11 +89,17 @@ int serial_open(const char *pathname, int flags)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Close the serial port.
|
||||||
|
* Returns 0 upon success, -1 upon failure.
|
||||||
|
*/
|
||||||
int serial_close(int fd)
|
int serial_close(int fd)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
CloseHandle(hdl);
|
/* Returns non-zero upon success, 0 upon failure. */
|
||||||
|
return (CloseHandle(hdl) == 0) ? -1 : 0;
|
||||||
#else
|
#else
|
||||||
|
/* Returns 0 upon success, -1 upon failure. */
|
||||||
return close(fd);
|
return close(fd);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
@ -103,16 +112,49 @@ int serial_flush(int fd)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
/* Returns non-zero upon success, 0 upon failure. */
|
/* Returns non-zero upon success, 0 upon failure. */
|
||||||
if (PurgeComm(hdl, PURGE_RXCLEAR | PURGE_TXCLEAR) == 0)
|
return (PurgeComm(hdl, PURGE_RXCLEAR | PURGE_TXCLEAR) == 0) ? -1 : 0;
|
||||||
return -1;
|
|
||||||
else
|
|
||||||
return 0;
|
|
||||||
#else
|
#else
|
||||||
/* Returns 0 upon success, -1 upon failure. */
|
/* Returns 0 upon success, -1 upon failure. */
|
||||||
return tcflush(fd, TCIOFLUSH);
|
return tcflush(fd, TCIOFLUSH);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Write a number of bytes to the specified serial port.
|
||||||
|
* Returns the number of bytes written, or -1 upon failure.
|
||||||
|
*/
|
||||||
|
int serial_write(int fd, const void *buf, size_t count)
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
DWORD tmp = 0;
|
||||||
|
|
||||||
|
/* FIXME */
|
||||||
|
/* Returns non-zero upon success, 0 upon failure. */
|
||||||
|
WriteFile(hdl, buf, count, &tmp, NULL);
|
||||||
|
#else
|
||||||
|
/* Returns the number of bytes written, or -1 upon failure. */
|
||||||
|
return write(fd, buf, count);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Read a number of bytes from the specified serial port.
|
||||||
|
* Returns the number of bytes read, or -1 upon failure.
|
||||||
|
*/
|
||||||
|
int serial_read(int fd, void *buf, size_t count)
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
DWORD tmp = 0;
|
||||||
|
|
||||||
|
/* FIXME */
|
||||||
|
/* Returns non-zero upon success, 0 upon failure. */
|
||||||
|
return ReadFile(hdl, buf, count, &tmp, NULL);
|
||||||
|
#else
|
||||||
|
/* Returns the number of bytes read, or -1 upon failure. */
|
||||||
|
return read(fd, buf, count);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
void *serial_backup_params(int fd)
|
void *serial_backup_params(int fd)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
@ -137,8 +179,10 @@ void serial_restore_params(int fd, void *backup)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* flowcontrol 1 = rts/cts 2 = xon/xoff
|
* Set serial parameters.
|
||||||
* parity 0 = none, 1 = even, 2 = odd
|
*
|
||||||
|
* flowcontrol: 1 = rts/cts, 2 = xon/xoff
|
||||||
|
* parity: 0 = none, 1 = even, 2 = odd
|
||||||
*/
|
*/
|
||||||
int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
|
int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
|
||||||
int flowcontrol)
|
int flowcontrol)
|
||||||
|
@ -148,6 +192,7 @@ int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
|
||||||
|
|
||||||
if (!GetCommState(hdl, &dcb)) {
|
if (!GetCommState(hdl, &dcb)) {
|
||||||
/* TODO: Error handling. */
|
/* TODO: Error handling. */
|
||||||
|
return SIGROK_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TODO: Rename 'speed' to 'baudrate'. */
|
/* TODO: Rename 'speed' to 'baudrate'. */
|
||||||
|
@ -178,6 +223,7 @@ int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
|
||||||
|
|
||||||
if (!SetCommState(hdl, &dcb)) {
|
if (!SetCommState(hdl, &dcb)) {
|
||||||
/* TODO: Error handling. */
|
/* TODO: Error handling. */
|
||||||
|
return SIGROK_ERR;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
struct termios term;
|
struct termios term;
|
||||||
|
|
|
@ -83,7 +83,7 @@ static int mso_send_control_message(struct sigrok_device_instance *sdi,
|
||||||
|
|
||||||
w = 0;
|
w = 0;
|
||||||
while (w < s) {
|
while (w < s) {
|
||||||
ret = write(fd, buf + w, s - w);
|
ret = serial_write(fd, buf + w, s - w);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
ret = SIGROK_ERR;
|
ret = SIGROK_ERR;
|
||||||
goto free;
|
goto free;
|
||||||
|
@ -145,7 +145,7 @@ static int mso_check_trigger(struct sigrok_device_instance *sdi,
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
buf[0] = 0;
|
buf[0] = 0;
|
||||||
if (read(sdi->serial->fd, buf, 1) != 1) /* FIXME: Need timeout */
|
if (serial_read(sdi->serial->fd, buf, 1) != 1) /* FIXME: Need timeout */
|
||||||
ret = SIGROK_ERR;
|
ret = SIGROK_ERR;
|
||||||
*info = buf[0];
|
*info = buf[0];
|
||||||
|
|
||||||
|
@ -611,7 +611,7 @@ static int receive_data(int fd, int revents, void *user_data)
|
||||||
|
|
||||||
revents = revents;
|
revents = revents;
|
||||||
|
|
||||||
s = read(fd, in, sizeof(in));
|
s = serial_read(fd, in, sizeof(in));
|
||||||
if (s <= 0)
|
if (s <= 0)
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|
||||||
|
|
|
@ -119,7 +119,7 @@ static int send_shortcommand(int fd, uint8_t command)
|
||||||
|
|
||||||
g_debug("ols: sending cmd 0x%.2x", command);
|
g_debug("ols: sending cmd 0x%.2x", command);
|
||||||
buf[0] = command;
|
buf[0] = command;
|
||||||
if (write(fd, buf, 1) != 1)
|
if (serial_write(fd, buf, 1) != 1)
|
||||||
return SIGROK_ERR;
|
return SIGROK_ERR;
|
||||||
|
|
||||||
return SIGROK_OK;
|
return SIGROK_OK;
|
||||||
|
@ -135,7 +135,7 @@ static int send_longcommand(int fd, uint8_t command, uint32_t data)
|
||||||
buf[2] = (data & 0xff0000) >> 16;
|
buf[2] = (data & 0xff0000) >> 16;
|
||||||
buf[3] = (data & 0xff00) >> 8;
|
buf[3] = (data & 0xff00) >> 8;
|
||||||
buf[4] = data & 0xff;
|
buf[4] = data & 0xff;
|
||||||
if (write(fd, buf, 5) != 5)
|
if (serial_write(fd, buf, 5) != 5)
|
||||||
return SIGROK_ERR;
|
return SIGROK_ERR;
|
||||||
|
|
||||||
return SIGROK_OK;
|
return SIGROK_OK;
|
||||||
|
@ -280,7 +280,7 @@ static int hw_init(char *deviceinfo)
|
||||||
g_poll(fds, devcnt, 1);
|
g_poll(fds, devcnt, 1);
|
||||||
for (i = 0; i < devcnt; i++) {
|
for (i = 0; i < devcnt; i++) {
|
||||||
if (fds[i].revents == G_IO_IN) {
|
if (fds[i].revents == G_IO_IN) {
|
||||||
if (read(fds[i].fd, buf, 4) == 4) {
|
if (serial_read(fds[i].fd, buf, 4) == 4) {
|
||||||
if (!strncmp(buf, "1SLO", 4)
|
if (!strncmp(buf, "1SLO", 4)
|
||||||
|| !strncmp(buf, "1ALS", 4)) {
|
|| !strncmp(buf, "1ALS", 4)) {
|
||||||
if (!strncmp(buf, "1SLO", 4))
|
if (!strncmp(buf, "1SLO", 4))
|
||||||
|
@ -515,7 +515,7 @@ static int receive_data(int fd, int revents, void *user_data)
|
||||||
|
|
||||||
if (revents == G_IO_IN
|
if (revents == G_IO_IN
|
||||||
&& num_transfers / num_channels <= limit_samples) {
|
&& num_transfers / num_channels <= limit_samples) {
|
||||||
if (read(fd, &byte, 1) != 1)
|
if (serial_read(fd, &byte, 1) != 1)
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|
||||||
sample[num_bytes++] = byte;
|
sample[num_bytes++] = byte;
|
||||||
|
|
|
@ -123,6 +123,8 @@ GSList *list_serial_ports(void);
|
||||||
int serial_open(const char *pathname, int flags);
|
int serial_open(const char *pathname, int flags);
|
||||||
int serial_close(int fd);
|
int serial_close(int fd);
|
||||||
int serial_flush(int fd);
|
int serial_flush(int fd);
|
||||||
|
int serial_write(int fd, const void *buf, size_t count);
|
||||||
|
int serial_read(int fd, void *buf, size_t count);
|
||||||
void *serial_backup_params(int fd);
|
void *serial_backup_params(int fd);
|
||||||
void serial_restore_params(int fd, void *backup);
|
void serial_restore_params(int fd, void *backup);
|
||||||
int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
|
int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
|
||||||
|
|
Loading…
Reference in New Issue