2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
9 * This program is dual-licensed; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
27 * For more information contact:
28 * Pulse-Eight Licensing <license@pulse-eight.com>
29 * http://www.pulse-eight.com/
30 * http://www.pulse-eight.net/
34 #include "lib/platform/sockets/serialport.h"
35 #include "lib/platform/util/baudrate.h"
36 #include "lib/platform/util/timeutils.h"
39 using namespace PLATFORM
;
41 void FormatWindowsError(int iErrorCode
, std::string
&strMessage
)
43 if (iErrorCode
!= ERROR_SUCCESS
)
45 char strAddMessage
[1024];
46 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS
, NULL
, iErrorCode
, MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
), strAddMessage
, 1024, NULL
);
47 strMessage
.append(": ");
48 strMessage
.append(strAddMessage
);
52 bool CSerialSocket::SetTimeouts(serial_socket_t socket
, int* iError
, DWORD iTimeoutMs
)
54 if (socket
== INVALID_HANDLE_VALUE
)
57 if (iTimeoutMs
== m_iCurrentReadTimeout
)
63 cto
.ReadIntervalTimeout
= MAXDWORD
;
64 cto
.ReadTotalTimeoutConstant
= 0;
65 cto
.ReadTotalTimeoutMultiplier
= 0;
69 cto
.ReadIntervalTimeout
= 0;
70 cto
.ReadTotalTimeoutConstant
= iTimeoutMs
;
71 cto
.ReadTotalTimeoutMultiplier
= 0;
74 if (!SetCommTimeouts(socket
, &cto
))
76 *iError
= GetLastError();
81 m_iCurrentReadTimeout
= iTimeoutMs
;
87 void CSerialSocket::Close(void)
90 SerialSocketClose(m_socket
);
91 m_socket
= INVALID_SERIAL_SOCKET_VALUE
;
94 void CSerialSocket::Shutdown(void)
97 SerialSocketClose(m_socket
);
98 m_socket
= INVALID_SERIAL_SOCKET_VALUE
;
101 ssize_t
CSerialSocket::Write(void* data
, size_t len
)
105 ssize_t iReturn
= SerialSocketWrite(m_socket
, &m_iError
, data
, len
);
106 if (iReturn
!= (ssize_t
)len
)
108 m_strError
= "unable to write to the serial port";
109 FormatWindowsError(GetLastError(), m_strError
);
116 ssize_t
CSerialSocket::Read(void* data
, size_t len
, uint64_t iTimeoutMs
/* = 0 */)
118 DWORD
dwTimeoutMs((DWORD
)iTimeoutMs
);
119 if (iTimeoutMs
!= (uint64_t)iTimeoutMs
)
120 dwTimeoutMs
= MAXDWORD
;
122 return IsOpen() && SetTimeouts(m_socket
, &m_iError
, dwTimeoutMs
) ?
123 SerialSocketRead(m_socket
, &m_iError
, data
, len
, iTimeoutMs
) :
127 bool CSerialSocket::Open(uint64_t iTimeoutMs
/* = 0 */)
133 std::string strComPath
= "\\\\.\\" + m_strName
;
134 CLockObject
lock(m_mutex
);
135 m_socket
= CreateFile(strComPath
.c_str(), GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
136 if (m_socket
== INVALID_HANDLE_VALUE
)
138 m_strError
= "Unable to open COM port";
139 FormatWindowsError(GetLastError(), m_strError
);
143 COMMCONFIG commConfig
= {0};
144 DWORD dwSize
= sizeof(commConfig
);
145 commConfig
.dwSize
= dwSize
;
146 if (GetDefaultCommConfig(strComPath
.c_str(), &commConfig
,&dwSize
))
148 if (!SetCommConfig(m_socket
, &commConfig
,dwSize
))
150 m_strError
= "unable to set default config";
151 FormatWindowsError(GetLastError(), m_strError
);
156 m_strError
= "unable to get default config";
157 FormatWindowsError(GetLastError(), m_strError
);
160 if (!SetupComm(m_socket
, 64, 64))
162 m_strError
= "unable to set up the com port";
163 FormatWindowsError(GetLastError(), m_strError
);
166 if (!SetBaudRate(m_iBaudrate
))
168 m_strError
= "unable to set baud rate";
169 FormatWindowsError(GetLastError(), m_strError
);
174 if (!SetTimeouts(m_socket
, &m_iError
, 0))
176 m_strError
= "unable to set timeouts";
177 FormatWindowsError(GetLastError(), m_strError
);
187 bool CSerialSocket::SetBaudRate(uint32_t baudrate
)
189 int32_t rate
= IntToBaudrate(baudrate
);
191 m_iBaudrate
= baudrate
> 0 ? baudrate
: 0;
196 memset(&dcb
,0,sizeof(dcb
));
197 dcb
.DCBlength
= sizeof(dcb
);
198 dcb
.BaudRate
= IntToBaudrate(m_iBaudrate
);
200 dcb
.fDtrControl
= DTR_CONTROL_DISABLE
;
201 dcb
.fRtsControl
= RTS_CONTROL_DISABLE
;
202 dcb
.fOutxCtsFlow
= false;
203 dcb
.fOutxDsrFlow
= false;
206 dcb
.fAbortOnError
= true;
208 if (m_iParity
== SERIAL_PARITY_NONE
)
209 dcb
.Parity
= NOPARITY
;
210 else if (m_iParity
== SERIAL_PARITY_EVEN
)
211 dcb
.Parity
= EVENPARITY
;
213 dcb
.Parity
= ODDPARITY
;
215 if (m_iStopbits
== SERIAL_STOP_BITS_TWO
)
216 dcb
.StopBits
= TWOSTOPBITS
;
218 dcb
.StopBits
= ONESTOPBIT
;
220 dcb
.ByteSize
= (BYTE
)m_iDatabits
;
222 if(!SetCommState(m_socket
,&dcb
))
224 m_strError
= "SetCommState failed";
225 FormatWindowsError(GetLastError(), m_strError
);