2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011 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 "../serialport.h"
34 #include "../baudrate.h"
35 #include "../timeutils.h"
40 void FormatWindowsError(int iErrorCode
, string
&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 CSerialPort::~CSerialPort(void)
66 bool CSerialPort::Open(string name
, int baudrate
, int databits
, int stopbits
, int parity
)
68 m_handle
= CreateFile(name
.c_str(), GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
69 if (m_handle
== INVALID_HANDLE_VALUE
)
71 m_error
= "Unable to open COM port";
72 FormatWindowsError(GetLastError(), m_error
);
76 COMMCONFIG commConfig
= {0};
77 DWORD dwSize
= sizeof(commConfig
);
78 commConfig
.dwSize
= dwSize
;
79 if (GetDefaultCommConfig(name
.c_str(), &commConfig
,&dwSize
))
81 if (!SetCommConfig(m_handle
, &commConfig
,dwSize
))
83 m_error
= "unable to set default config";
84 FormatWindowsError(GetLastError(), m_error
);
89 m_error
= "unable to get default config";
90 FormatWindowsError(GetLastError(), m_error
);
93 if (!SetupComm(m_handle
, 64, 64))
95 m_error
= "unable to set up the com port";
96 FormatWindowsError(GetLastError(), m_error
);
99 m_iDatabits
= databits
;
100 m_iStopbits
= stopbits
;
102 if (!SetBaudRate(baudrate
))
104 m_error
= "unable to set baud rate";
105 FormatWindowsError(GetLastError(), m_error
);
110 if (!SetTimeouts(false))
112 m_error
= "unable to set timeouts";
113 FormatWindowsError(GetLastError(), m_error
);
122 bool CSerialPort::SetTimeouts(bool bBlocking
)
124 if (m_handle
== INVALID_HANDLE_VALUE
)
128 if (!GetCommTimeouts(m_handle
, &cto
))
130 m_error
= "GetCommTimeouts failed";
131 FormatWindowsError(GetLastError(), m_error
);
137 cto
.ReadIntervalTimeout
= 0;
138 cto
.ReadTotalTimeoutConstant
= 0;
139 cto
.ReadTotalTimeoutMultiplier
= 0;
143 cto
.ReadIntervalTimeout
= MAXDWORD
;
144 cto
.ReadTotalTimeoutConstant
= 0;
145 cto
.ReadTotalTimeoutMultiplier
= 0;
148 if (!SetCommTimeouts(m_handle
, &cto
))
150 m_error
= "SetCommTimeouts failed";
151 FormatWindowsError(GetLastError(), m_error
);
158 void CSerialPort::Close(void)
162 CloseHandle(m_handle
);
167 int CSerialPort::Write(uint8_t* data
, int len
)
169 DWORD iBytesWritten
= 0;
173 if (!WriteFile(m_handle
, data
, len
, &iBytesWritten
, NULL
))
175 m_error
= "Error while writing to COM port";
176 FormatWindowsError(GetLastError(), m_error
);
180 return (int) iBytesWritten
;
183 int CSerialPort::Read(uint8_t* data
, int len
, int iTimeoutMs
/* = -1 */)
185 DWORD iBytesRead
= 0;
188 m_error
= "Error while reading from COM port: invalid handle";
192 if(!ReadFile(m_handle
, data
, len
, &iBytesRead
, NULL
) != 0)
194 m_error
= "unable to read from device";
195 FormatWindowsError(GetLastError(), m_error
);
199 return (int) iBytesRead
;
202 bool CSerialPort::SetBaudRate(int baudrate
)
204 m_iBaudrate
= baudrate
;
207 memset(&dcb
,0,sizeof(dcb
));
208 dcb
.DCBlength
= sizeof(dcb
);
209 dcb
.BaudRate
= IntToBaudrate(m_iBaudrate
);
211 dcb
.fDtrControl
= DTR_CONTROL_DISABLE
;
212 dcb
.fRtsControl
= RTS_CONTROL_DISABLE
;
213 dcb
.fOutxCtsFlow
= false;
214 dcb
.fOutxDsrFlow
= false;
217 dcb
.fAbortOnError
= true;
219 if (m_iParity
== PAR_NONE
)
220 dcb
.Parity
= NOPARITY
;
221 else if (m_iParity
== PAR_EVEN
)
222 dcb
.Parity
= EVENPARITY
;
224 dcb
.Parity
= ODDPARITY
;
226 if (m_iStopbits
== 2)
227 dcb
.StopBits
= TWOSTOPBITS
;
229 dcb
.StopBits
= ONESTOPBIT
;
231 dcb
.ByteSize
= m_iDatabits
;
233 if(!SetCommState(m_handle
,&dcb
))
235 m_error
= "SetCommState failed";
236 FormatWindowsError(GetLastError(), m_error
);
243 bool CSerialPort::IsOpen() const