From: Martin Ling Date: Mon, 25 Nov 2013 10:23:13 +0000 (+0000) Subject: Handle -EAGAIN in non-blocking reads on Unix, and return 0 from sp_read(). X-Git-Tag: libserialport-0.1.0~51 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=33d5ff47b59c4a0d35a6794daf7733406b4f2080;p=libserialport.git Handle -EAGAIN in non-blocking reads on Unix, and return 0 from sp_read(). --- diff --git a/serialport.c b/serialport.c index fbeb76e..fd42034 100644 --- a/serialport.c +++ b/serialport.c @@ -60,6 +60,7 @@ struct sp_port { char *name; + int nonblocking; #ifdef _WIN32 HANDLE hdl; #else @@ -578,6 +579,8 @@ enum sp_return sp_open(struct sp_port *port, enum sp_mode flags) DEBUG("Opening port %s", port->name); + port->nonblocking = (flags & SP_MODE_NONBLOCK) ? 1 : 0; + #ifdef _WIN32 DWORD desired_access = 0, flags_and_attributes = 0; char *escaped_port_name; @@ -786,8 +789,14 @@ enum sp_return sp_read(struct sp_port *port, void *buf, size_t count) ssize_t bytes_read; /* Returns the number of bytes read, or -1 upon failure. */ - if ((bytes_read = read(port->fd, buf, count)) < 0) - RETURN_FAIL("read() failed"); + if ((bytes_read = read(port->fd, buf, count)) < 0) { + if (port->nonblocking && errno == EAGAIN) + /* Port is opened in nonblocking mode and there are no bytes available. */ + bytes_read = 0; + else + /* This is an actual failure. */ + RETURN_FAIL("read() failed"); + } RETURN_VALUE("%d", bytes_read); #endif }