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 "../../util/timeutils.h"
39 void FormatWindowsError(int iErrorCode
, string
&strMessage
)
41 if (iErrorCode
!= ERROR_SUCCESS
)
43 char strAddMessage
[1024];
44 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS
, NULL
, iErrorCode
, MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
), strAddMessage
, 1024, NULL
);
45 strMessage
.append(": ");
46 strMessage
.append(strAddMessage
);
50 CSerialPort::CSerialPort(void) :
51 m_handle(INVALID_HANDLE_VALUE
),
60 CSerialPort::~CSerialPort(void)
65 bool CSerialPort::Open(string name
, int baudrate
, int databits
, int stopbits
, int parity
)
67 m_handle
= CreateFile(name
.c_str(), GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
68 if (m_handle
== INVALID_HANDLE_VALUE
)
70 m_error
= "Unable to open COM port";
71 FormatWindowsError(GetLastError(), m_error
);
75 COMMCONFIG commConfig
= {0};
76 DWORD dwSize
= sizeof(commConfig
);
77 commConfig
.dwSize
= dwSize
;
78 if (GetDefaultCommConfig(name
.c_str(), &commConfig
,&dwSize
))
80 if (!SetCommConfig(m_handle
, &commConfig
,dwSize
))
82 m_error
= "unable to set default config";
83 FormatWindowsError(GetLastError(), m_error
);
88 m_error
= "unable to get default config";
89 FormatWindowsError(GetLastError(), m_error
);
92 if (!SetupComm(m_handle
, 64, 64))
94 m_error
= "unable to set up the com port";
95 FormatWindowsError(GetLastError(), m_error
);
98 m_iDatabits
= databits
;
99 m_iStopbits
= stopbits
;
101 if (!SetBaudRate(baudrate
))
103 m_error
= "unable to set baud rate";
104 FormatWindowsError(GetLastError(), m_error
);
109 if (!SetTimeouts(false))
111 m_error
= "unable to set timeouts";
112 FormatWindowsError(GetLastError(), m_error
);
121 bool CSerialPort::SetTimeouts(bool bBlocking
)
123 if (m_handle
== INVALID_HANDLE_VALUE
)
127 if (!GetCommTimeouts(m_handle
, &cto
))
129 m_error
= "GetCommTimeouts failed";
130 FormatWindowsError(GetLastError(), m_error
);
136 cto
.ReadIntervalTimeout
= 0;
137 cto
.ReadTotalTimeoutConstant
= 0;
138 cto
.ReadTotalTimeoutMultiplier
= 0;
142 cto
.ReadIntervalTimeout
= MAXDWORD
;
143 cto
.ReadTotalTimeoutConstant
= 0;
144 cto
.ReadTotalTimeoutMultiplier
= 0;
147 if (!SetCommTimeouts(m_handle
, &cto
))
149 m_error
= "SetCommTimeouts failed";
150 FormatWindowsError(GetLastError(), m_error
);
157 void CSerialPort::Close(void)
161 CloseHandle(m_handle
);
166 int CSerialPort::Write(uint8_t* data
, int len
)
168 DWORD iBytesWritten
= 0;
172 if (!WriteFile(m_handle
, data
, len
, &iBytesWritten
, NULL
))
174 m_error
= "Error while writing to COM port";
175 FormatWindowsError(GetLastError(), m_error
);
179 return (int) iBytesWritten
;
182 int CSerialPort::Read(uint8_t* data
, int len
, int iTimeoutMs
/* = -1 */)
184 DWORD iBytesRead
= 0;
187 m_error
= "Error while reading from COM port: invalid handle";
191 if(!ReadFile(m_handle
, data
, len
, &iBytesRead
, NULL
) != 0)
193 m_error
= "unable to read from device";
194 FormatWindowsError(GetLastError(), m_error
);
198 return (int) iBytesRead
;
201 bool CSerialPort::SetBaudRate(int baudrate
)
203 m_iBaudrate
= baudrate
;
206 memset(&dcb
,0,sizeof(dcb
));
207 dcb
.DCBlength
= sizeof(dcb
);
208 dcb
.BaudRate
= IntToRate(m_iBaudrate
);
210 dcb
.fDtrControl
= DTR_CONTROL_DISABLE
;
211 dcb
.fRtsControl
= RTS_CONTROL_DISABLE
;
212 dcb
.fOutxCtsFlow
= false;
213 dcb
.fOutxDsrFlow
= false;
216 dcb
.fAbortOnError
= true;
218 if (m_iParity
== PAR_NONE
)
219 dcb
.Parity
= NOPARITY
;
220 else if (m_iParity
== PAR_EVEN
)
221 dcb
.Parity
= EVENPARITY
;
223 dcb
.Parity
= ODDPARITY
;
225 if (m_iStopbits
== 2)
226 dcb
.StopBits
= TWOSTOPBITS
;
228 dcb
.StopBits
= ONESTOPBIT
;
230 dcb
.ByteSize
= m_iDatabits
;
232 if(!SetCommState(m_handle
,&dcb
))
234 m_error
= "SetCommState failed";
235 FormatWindowsError(GetLastError(), m_error
);