X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Flib%2Fplatform%2Fwindows%2Fserialport.cpp;h=22c65d76e6a4162d3bb7f026450afcb6b83a9afb;hb=ba65909d0a9c43a1bac71c6182c53f202285cec5;hp=bfdc8d53d71917095b444a527f907c0ce8352fb9;hpb=b9187cc6999276ce37a5c9852655fd558ea76b8e;p=deb_libcec.git diff --git a/src/lib/platform/windows/serialport.cpp b/src/lib/platform/windows/serialport.cpp index bfdc8d5..22c65d7 100644 --- a/src/lib/platform/windows/serialport.cpp +++ b/src/lib/platform/windows/serialport.cpp @@ -30,12 +30,12 @@ * http://www.pulse-eight.net/ */ -#include "../serialport.h" -#include "../baudrate.h" -#include "../timeutils.h" +#include "../sockets/serialport.h" +#include "../util/baudrate.h" +#include "../util/timeutils.h" using namespace std; -using namespace CEC; +using namespace PLATFORM; void FormatWindowsError(int iErrorCode, string &strMessage) { @@ -63,9 +63,11 @@ CSerialPort::~CSerialPort(void) Close(); } -bool CSerialPort::Open(string name, int baudrate, int databits, int stopbits, int parity) +bool CSerialPort::Open(string name, uint32_t baudrate, uint8_t databits, uint8_t stopbits, uint8_t parity) { - m_handle = CreateFile(name.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); + CStdString strComPath = "\\\\.\\" + name; + CLockObject lock(m_mutex); + m_handle = CreateFile(strComPath.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); if (m_handle == INVALID_HANDLE_VALUE) { m_error = "Unable to open COM port"; @@ -76,7 +78,7 @@ bool CSerialPort::Open(string name, int baudrate, int databits, int stopbits, in COMMCONFIG commConfig = {0}; DWORD dwSize = sizeof(commConfig); commConfig.dwSize = dwSize; - if (GetDefaultCommConfig(name.c_str(), &commConfig,&dwSize)) + if (GetDefaultCommConfig(strComPath.c_str(), &commConfig,&dwSize)) { if (!SetCommConfig(m_handle, &commConfig,dwSize)) { @@ -157,6 +159,7 @@ bool CSerialPort::SetTimeouts(bool bBlocking) void CSerialPort::Close(void) { + CLockObject lock(m_mutex); if (m_bIsOpen) { CloseHandle(m_handle); @@ -164,8 +167,9 @@ void CSerialPort::Close(void) } } -int CSerialPort::Write(uint8_t* data, int len) +int64_t CSerialPort::Write(uint8_t* data, uint32_t len) { + CLockObject lock(m_mutex); DWORD iBytesWritten = 0; if (!m_bIsOpen) return -1; @@ -177,31 +181,41 @@ int CSerialPort::Write(uint8_t* data, int len) return -1; } - return (int) iBytesWritten; + return (int64_t)iBytesWritten; } -int CSerialPort::Read(uint8_t* data, int len, int iTimeoutMs /* = -1 */) +int32_t CSerialPort::Read(uint8_t* data, uint32_t len, uint64_t iTimeoutMs /* = 0 */) { + CLockObject lock(m_mutex); + int32_t iReturn(-1); DWORD iBytesRead = 0; if (m_handle == 0) { m_error = "Error while reading from COM port: invalid handle"; - return -1; + return iReturn; } if(!ReadFile(m_handle, data, len, &iBytesRead, NULL) != 0) { m_error = "unable to read from device"; FormatWindowsError(GetLastError(), m_error); - iBytesRead = -1; + iReturn = -1; + } + else + { + iReturn = (int32_t) iBytesRead; } - return (int) iBytesRead; + return iReturn; } -bool CSerialPort::SetBaudRate(int baudrate) +bool CSerialPort::SetBaudRate(uint32_t baudrate) { - m_iBaudrate = baudrate; + int32_t rate = IntToBaudrate(baudrate); + if (rate < 0) + m_iBaudrate = baudrate > 0 ? baudrate : 0; + else + m_iBaudrate = rate; DCB dcb; memset(&dcb,0,sizeof(dcb)); @@ -240,7 +254,8 @@ bool CSerialPort::SetBaudRate(int baudrate) return true; } -bool CSerialPort::IsOpen() const +bool CSerialPort::IsOpen() { + CLockObject lock(m_mutex); return m_bIsOpen; }