2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2012 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/
33 #include "../sockets/serialport.h"
34 #include "../util/baudrate.h"
35 #include "../util/timeutils.h"
38 using namespace PLATFORM
;
40 void CSerialPort::FormatWindowsError(int iErrorCode
, CStdString
&strMessage
)
42 if (iErrorCode
!= ERROR_SUCCESS
)
44 char strAddMessage
[1024];
45 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS
, NULL
, iErrorCode
, MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
), strAddMessage
, 1024, NULL
);
46 strMessage
.append(": ");
47 strMessage
.append(strAddMessage
);
51 CSerialPort::CSerialPort(void) :
52 m_handle(INVALID_HANDLE_VALUE
),
61 bool CSerialPort::Open(string name
, uint32_t baudrate
, uint8_t databits
, uint8_t stopbits
, uint8_t parity
)
63 CStdString strComPath
= "\\\\.\\" + name
;
64 CLockObject
lock(m_mutex
);
65 m_handle
= CreateFile(strComPath
.c_str(), GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
66 if (m_handle
== INVALID_HANDLE_VALUE
)
68 m_strError
= "Unable to open COM port";
69 FormatWindowsError(GetLastError(), m_strError
);
73 COMMCONFIG commConfig
= {0};
74 DWORD dwSize
= sizeof(commConfig
);
75 commConfig
.dwSize
= dwSize
;
76 if (GetDefaultCommConfig(strComPath
.c_str(), &commConfig
,&dwSize
))
78 if (!SetCommConfig(m_handle
, &commConfig
,dwSize
))
80 m_strError
= "unable to set default config";
81 FormatWindowsError(GetLastError(), m_strError
);
86 m_strError
= "unable to get default config";
87 FormatWindowsError(GetLastError(), m_strError
);
90 if (!SetupComm(m_handle
, 64, 64))
92 m_strError
= "unable to set up the com port";
93 FormatWindowsError(GetLastError(), m_strError
);
96 m_iDatabits
= databits
;
97 m_iStopbits
= stopbits
;
99 if (!SetBaudRate(baudrate
))
101 m_strError
= "unable to set baud rate";
102 FormatWindowsError(GetLastError(), m_strError
);
107 if (!SetTimeouts(false))
109 m_strError
= "unable to set timeouts";
110 FormatWindowsError(GetLastError(), m_strError
);
119 bool CSerialPort::SetTimeouts(bool bBlocking
)
121 if (m_handle
== INVALID_HANDLE_VALUE
)
125 if (!GetCommTimeouts(m_handle
, &cto
))
127 m_strError
= "GetCommTimeouts failed";
128 FormatWindowsError(GetLastError(), m_strError
);
134 cto
.ReadIntervalTimeout
= 0;
135 cto
.ReadTotalTimeoutConstant
= 0;
136 cto
.ReadTotalTimeoutMultiplier
= 0;
140 cto
.ReadIntervalTimeout
= MAXDWORD
;
141 cto
.ReadTotalTimeoutConstant
= 0;
142 cto
.ReadTotalTimeoutMultiplier
= 0;
145 if (!SetCommTimeouts(m_handle
, &cto
))
147 m_strError
= "SetCommTimeouts failed";
148 FormatWindowsError(GetLastError(), m_strError
);
155 void CSerialPort::Close(void)
157 CLockObject
lock(m_mutex
);
160 CloseHandle(m_handle
);
165 int64_t CSerialPort::Write(uint8_t* data
, uint32_t len
)
167 CLockObject
lock(m_mutex
);
168 DWORD iBytesWritten
= 0;
172 if (!WriteFile(m_handle
, data
, len
, &iBytesWritten
, NULL
))
174 m_strError
= "Error while writing to COM port";
175 FormatWindowsError(GetLastError(), m_strError
);
179 return (int64_t)iBytesWritten
;
182 int32_t CSerialPort::Read(uint8_t* data
, uint32_t len
, uint64_t iTimeoutMs
/* = 0 */)
184 CLockObject
lock(m_mutex
);
186 DWORD iBytesRead
= 0;
189 m_strError
= "Error while reading from COM port: invalid handle";
193 if(!ReadFile(m_handle
, data
, len
, &iBytesRead
, NULL
) != 0)
195 m_strError
= "unable to read from device";
196 FormatWindowsError(GetLastError(), m_strError
);
201 iReturn
= (int32_t) iBytesRead
;
207 bool CSerialPort::SetBaudRate(uint32_t baudrate
)
209 int32_t rate
= IntToBaudrate(baudrate
);
211 m_iBaudrate
= baudrate
> 0 ? baudrate
: 0;
216 memset(&dcb
,0,sizeof(dcb
));
217 dcb
.DCBlength
= sizeof(dcb
);
218 dcb
.BaudRate
= IntToBaudrate(m_iBaudrate
);
220 dcb
.fDtrControl
= DTR_CONTROL_DISABLE
;
221 dcb
.fRtsControl
= RTS_CONTROL_DISABLE
;
222 dcb
.fOutxCtsFlow
= false;
223 dcb
.fOutxDsrFlow
= false;
226 dcb
.fAbortOnError
= true;
228 if (m_iParity
== PAR_NONE
)
229 dcb
.Parity
= NOPARITY
;
230 else if (m_iParity
== PAR_EVEN
)
231 dcb
.Parity
= EVENPARITY
;
233 dcb
.Parity
= ODDPARITY
;
235 if (m_iStopbits
== 2)
236 dcb
.StopBits
= TWOSTOPBITS
;
238 dcb
.StopBits
= ONESTOPBIT
;
240 dcb
.ByteSize
= m_iDatabits
;
242 if(!SetCommState(m_handle
,&dcb
))
244 m_strError
= "SetCommState failed";
245 FormatWindowsError(GetLastError(), m_strError
);
252 bool CSerialPort::IsOpen(void)
254 CLockObject
lock(m_mutex
);