X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=serialport.c;h=9d2a50440046fd67642cd91431f3e55398aa4f29;hb=0765af56440614d73c26944f811720d3a7124f29;hp=fd42034f9b44700221f24d29d373b21a1425ecc3;hpb=33d5ff47b59c4a0d35a6794daf7733406b4f2080;p=libserialport.git diff --git a/serialport.c b/serialport.c index fd42034..9d2a504 100644 --- a/serialport.c +++ b/serialport.c @@ -63,6 +63,9 @@ struct sp_port { int nonblocking; #ifdef _WIN32 HANDLE hdl; + OVERLAPPED write_ovl; + BYTE pending_byte; + BOOL writing; #else int fd; #endif @@ -154,7 +157,7 @@ void (*sp_debug_handler)(const char *format, ...) = sp_default_debug_handler; #define RETURN_FAIL(msg) do { DEBUG_FAIL(msg); return SP_ERR_FAIL; } while (0) #define RETURN_VALUE(fmt, x) do { DEBUG("%s returning " fmt, __func__, x); return x; } while (0) #define SET_ERROR(val, err, msg) do { DEBUG_ERROR(err, msg); val = err; } while (0) -#define SET_FAIL(val, msg) do { DEBUG_FAIL(msg); val = err; } while (0) +#define SET_FAIL(val, msg) do { DEBUG_FAIL(msg); val = SP_ERR_FAIL; } while (0) #define TRACE(fmt, ...) DEBUG("%s(" fmt ") called", __func__, ##__VA_ARGS__) #define TRY(x) do { int ret = x; if (ret != SP_OK) RETURN_CODEVAL(ret); } while (0) @@ -583,6 +586,7 @@ enum sp_return sp_open(struct sp_port *port, enum sp_mode flags) #ifdef _WIN32 DWORD desired_access = 0, flags_and_attributes = 0; + COMMTIMEOUTS timeouts; char *escaped_port_name; /* Prefix port name with '\\.\' to work with ports above COM9. */ @@ -606,6 +610,30 @@ enum sp_return sp_open(struct sp_port *port, enum sp_mode flags) if (port->hdl == INVALID_HANDLE_VALUE) RETURN_FAIL("CreateFile() failed"); + + /* All timeouts disabled. */ + timeouts.ReadIntervalTimeout = 0; + timeouts.ReadTotalTimeoutMultiplier = 0; + timeouts.ReadTotalTimeoutConstant = 0; + timeouts.WriteTotalTimeoutMultiplier = 0; + timeouts.WriteTotalTimeoutConstant = 0; + + if (port->nonblocking) { + /* Set read timeout such that all reads return immediately. */ + timeouts.ReadIntervalTimeout = MAXDWORD; + /* Prepare OVERLAPPED structure for non-blocking writes. */ + memset(&port->write_ovl, 0, sizeof(port->write_ovl)); + if (!(port->write_ovl.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL))) { + sp_close(port); + RETURN_FAIL("CreateEvent() failed"); + } + port->writing = FALSE; + } + + if (SetCommTimeouts(port->hdl, &timeouts) == 0) { + sp_close(port); + RETURN_FAIL("SetCommTimeouts() failed"); + } #else int flags_local = 0; struct port_data data; @@ -668,6 +696,11 @@ enum sp_return sp_close(struct sp_port *port) if (CloseHandle(port->hdl) == 0) RETURN_FAIL("CloseHandle() failed"); port->hdl = INVALID_HANDLE_VALUE; + if (port->nonblocking) { + /* Close event handle created for overlapped writes. */ + if (CloseHandle(port->write_ovl.hEvent) == 0) + RETURN_FAIL("CloseHandle() failed"); + } #else /* Returns 0 upon success, -1 upon failure. */ if (close(port->fd) == -1) @@ -749,12 +782,60 @@ enum sp_return sp_write(struct sp_port *port, const void *buf, size_t count) DEBUG("Writing up to %d bytes to port %s", count, port->name); + if (count == 0) + RETURN_VALUE("0", 0); + #ifdef _WIN32 DWORD written = 0; + BYTE *ptr = (BYTE *) buf; + + if (port->nonblocking) { + /* Non-blocking write. */ + + /* Check whether previous write is complete. */ + if (port->writing) { + if (HasOverlappedIoCompleted(&port->write_ovl)) { + DEBUG("Previous write completed"); + port->writing = 0; + } else { + DEBUG("Previous write not complete"); + /* Can't take a new write until the previous one finishes. */ + RETURN_VALUE("0", 0); + } + } + + /* Keep writing data until the OS has to actually start an async IO for it. + * At that point we know the buffer is full. */ + while (written < count) + { + /* Copy first byte of user buffer. */ + port->pending_byte = *ptr++; + + /* Start asynchronous write. */ + if (WriteFile(port->hdl, &port->pending_byte, 1, NULL, &port->write_ovl) == 0) { + if (GetLastError() == ERROR_IO_PENDING) { + DEBUG("Asynchronous write started"); + port->writing = 1; + RETURN_VALUE("%d", ++written); + } else { + /* Actual failure of some kind. */ + RETURN_FAIL("WriteFile() failed"); + } + } else { + DEBUG("Single byte written immediately."); + written++; + } + } + + DEBUG("All bytes written immediately."); + + } else { + /* Blocking write. */ + if (WriteFile(port->hdl, buf, count, &written, NULL) == 0) { + RETURN_FAIL("WriteFile() failed"); + } + } - /* Returns non-zero upon success, 0 upon failure. */ - if (WriteFile(port->hdl, buf, count, &written, NULL) == 0) - RETURN_FAIL("WriteFile() failed"); RETURN_VALUE("%d", written); #else /* Returns the number of bytes written, or -1 upon failure. */