public:
CSerialSocket(const CStdString &strName, uint32_t iBaudrate, SerialDataBits iDatabits = SERIAL_DATA_BITS_EIGHT, SerialStopBits iStopbits = SERIAL_STOP_BITS_ONE, SerialParity iParity = SERIAL_PARITY_NONE) :
CCommonSocket<serial_socket_t>(INVALID_SERIAL_SOCKET_VALUE, strName),
+#ifdef __WINDOWS__
+ m_iCurrentTimeout(0),
+#endif
m_bIsOpen(false),
m_iBaudrate(iBaudrate),
m_iDatabits(iDatabits),
protected:
#ifndef __WINDOWS__
struct termios m_options;
+ #else
+ bool SetTimeouts(serial_socket_t socket, int* iError, DWORD iTimeout);
+ DWORD m_iCurrentTimeout;
#endif
bool m_bIsOpen;
}
}
-bool SetTimeouts(serial_socket_t socket, int* iError, bool bBlocking)
+bool CSerialSocket::SetTimeouts(serial_socket_t socket, int* iError, DWORD iTimeout)
{
if (socket == INVALID_HANDLE_VALUE)
return false;
+ if (iTimeout == m_iCurrentTimeout)
+ return true;
+
COMMTIMEOUTS cto;
if (!GetCommTimeouts(socket, &cto))
{
return false;
}
- if (bBlocking)
- {
- cto.ReadIntervalTimeout = 0;
- cto.ReadTotalTimeoutConstant = 0;
- cto.ReadTotalTimeoutMultiplier = 0;
- }
- else
- {
- cto.ReadIntervalTimeout = MAXDWORD;
- cto.ReadTotalTimeoutConstant = 0;
- cto.ReadTotalTimeoutMultiplier = 0;
- }
+ cto.ReadIntervalTimeout = 0;
+ cto.ReadTotalTimeoutConstant = iTimeout;
+ cto.ReadTotalTimeoutMultiplier = 0;
if (!SetCommTimeouts(socket, &cto))
{
return false;
}
+ m_iCurrentTimeout = iTimeout;
return true;
}
ssize_t CSerialSocket::Read(void* data, size_t len, uint64_t iTimeoutMs /* = 0 */)
{
- return IsOpen() ? SerialSocketRead(m_socket, &m_iError, data, len, iTimeoutMs) : -1;
+ if (IsOpen())
+ {
+ DWORD iTimeout((DWORD)iTimeoutMs);
+ if (iTimeout != iTimeoutMs)
+ return -1;
+
+ int iError(0);
+ if (!SetTimeouts(m_socket, &iError, iTimeout))
+ return -1;
+
+ return SerialSocketRead(m_socket, &m_iError, data, len, iTimeoutMs);
+ }
+
+ return -1;
}
bool CSerialSocket::Open(uint64_t iTimeoutMs /* = 0 */)
return false;
}
- if (!SetTimeouts(m_socket, &m_iError, false))
+ if (!SetTimeouts(m_socket, &m_iError, MAXDWORD))
{
m_strError = "unable to set timeouts";
FormatWindowsError(GetLastError(), m_strError);