From 2c56dd5189b47478350c84917c420670598ca06b Mon Sep 17 00:00:00 2001 From: Lars Op den Kamp Date: Sun, 4 Dec 2011 10:11:13 +0100 Subject: [PATCH] posix: set the timeout when writing to the serial port --- src/lib/platform/linux/serialport.cpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/lib/platform/linux/serialport.cpp b/src/lib/platform/linux/serialport.cpp index 2e4560d..f040857 100644 --- a/src/lib/platform/linux/serialport.cpp +++ b/src/lib/platform/linux/serialport.cpp @@ -74,16 +74,33 @@ int8_t CSerialPort::Write(CCECAdapterMessage *data) int32_t byteswritten = 0; + struct timeval timeout, *tv; + if (data->transmit_timeout <= 0) + { + tv = NULL; + } + else + { + timeout.tv_sec = (long int)data->transmit_timeout / (long int)1000.; + timeout.tv_usec = (long int)data->transmit_timeout % (long int)1000.; + tv = &timeout; + } + while (byteswritten < (int32_t) data->size()) { FD_ZERO(&port); FD_SET(m_fd, &port); - int returnv = select(m_fd + 1, NULL, &port, NULL, NULL); - if (returnv == -1) + int returnv = select(m_fd + 1, NULL, &port, NULL, tv); + if (returnv < 0) { m_error = strerror(errno); return -1; } + else if (returnv == 0) + { + m_error = "timeout"; + return -1; + } returnv = write(m_fd, data->packet.data + byteswritten, data->size() - byteswritten); if (returnv == -1) -- 2.34.1