Commit | Line | Data |
---|---|---|
abbca718 LOK |
1 | /* |
2 | * This file is part of the libCEC(R) library. | |
3 | * | |
4 | * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved. | |
5 | * libCEC(R) is an original work, containing original code. | |
6 | * | |
7 | * libCEC(R) is a trademark of Pulse-Eight Limited. | |
8 | * | |
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. | |
13 | * | |
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. | |
18 | * | |
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. | |
22 | * | |
23 | * | |
24 | * Alternatively, you can license this library under a commercial license, | |
25 | * please contact Pulse-Eight Licensing for more information. | |
26 | * | |
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/ | |
31 | */ | |
32 | ||
33 | #include "../serialport.h" | |
34 | #include "../baudrate.h" | |
35 | #include "../../util/timeutils.h" | |
36 | ||
37 | using namespace std; | |
38 | ||
39 | void FormatWindowsError(int iErrorCode, string &strMessage) | |
40 | { | |
41 | if (iErrorCode != ERROR_SUCCESS) | |
42 | { | |
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); | |
47 | } | |
48 | } | |
49 | ||
50 | CSerialPort::CSerialPort(void) : | |
51 | m_handle(INVALID_HANDLE_VALUE), | |
52 | m_bIsOpen(false), | |
53 | m_iBaudrate(0), | |
54 | m_iDatabits(0), | |
55 | m_iStopbits(0), | |
56 | m_iParity(0) | |
57 | { | |
58 | } | |
59 | ||
60 | CSerialPort::~CSerialPort(void) | |
61 | { | |
62 | Close(); | |
63 | } | |
64 | ||
65 | bool CSerialPort::Open(string name, int baudrate, int databits, int stopbits, int parity) | |
66 | { | |
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) | |
69 | { | |
70 | m_error = "Unable to open COM port"; | |
71 | FormatWindowsError(GetLastError(), m_error); | |
72 | return false; | |
73 | } | |
74 | ||
75 | COMMCONFIG commConfig = {0}; | |
76 | DWORD dwSize = sizeof(commConfig); | |
77 | commConfig.dwSize = dwSize; | |
78 | if (GetDefaultCommConfig(name.c_str(), &commConfig,&dwSize)) | |
79 | { | |
80 | if (!SetCommConfig(m_handle, &commConfig,dwSize)) | |
81 | { | |
82 | m_error = "unable to set default config"; | |
83 | FormatWindowsError(GetLastError(), m_error); | |
84 | } | |
85 | } | |
86 | else | |
87 | { | |
88 | m_error = "unable to get default config"; | |
89 | FormatWindowsError(GetLastError(), m_error); | |
90 | } | |
91 | ||
92 | if (!SetupComm(m_handle, 64, 64)) | |
93 | { | |
94 | m_error = "unable to set up the com port"; | |
95 | FormatWindowsError(GetLastError(), m_error); | |
96 | } | |
97 | ||
98 | m_iDatabits = databits; | |
99 | m_iStopbits = stopbits; | |
100 | m_iParity = parity; | |
101 | if (!SetBaudRate(baudrate)) | |
102 | { | |
103 | m_error = "unable to set baud rate"; | |
104 | FormatWindowsError(GetLastError(), m_error); | |
105 | Close(); | |
106 | return false; | |
107 | } | |
108 | ||
109 | if (!SetTimeouts(false)) | |
110 | { | |
111 | m_error = "unable to set timeouts"; | |
112 | FormatWindowsError(GetLastError(), m_error); | |
113 | Close(); | |
114 | return false; | |
115 | } | |
116 | ||
117 | m_bIsOpen = true; | |
118 | return m_bIsOpen; | |
119 | } | |
120 | ||
121 | bool CSerialPort::SetTimeouts(bool bBlocking) | |
122 | { | |
123 | if (m_handle == INVALID_HANDLE_VALUE) | |
124 | return false; | |
125 | ||
126 | COMMTIMEOUTS cto; | |
127 | if (!GetCommTimeouts(m_handle, &cto)) | |
128 | { | |
129 | m_error = "GetCommTimeouts failed"; | |
130 | FormatWindowsError(GetLastError(), m_error); | |
131 | return false; | |
132 | } | |
133 | ||
134 | if (bBlocking) | |
135 | { | |
136 | cto.ReadIntervalTimeout = 0; | |
137 | cto.ReadTotalTimeoutConstant = 0; | |
138 | cto.ReadTotalTimeoutMultiplier = 0; | |
139 | } | |
140 | else | |
141 | { | |
142 | cto.ReadIntervalTimeout = MAXDWORD; | |
143 | cto.ReadTotalTimeoutConstant = 0; | |
144 | cto.ReadTotalTimeoutMultiplier = 0; | |
145 | } | |
146 | ||
147 | if (!SetCommTimeouts(m_handle, &cto)) | |
148 | { | |
149 | m_error = "SetCommTimeouts failed"; | |
150 | FormatWindowsError(GetLastError(), m_error); | |
151 | return false; | |
152 | } | |
153 | ||
154 | return true; | |
155 | } | |
156 | ||
157 | void CSerialPort::Close(void) | |
158 | { | |
159 | if (m_bIsOpen) | |
160 | { | |
161 | CloseHandle(m_handle); | |
162 | m_bIsOpen = false; | |
163 | } | |
164 | } | |
165 | ||
166 | int CSerialPort::Write(uint8_t* data, int len) | |
167 | { | |
168 | DWORD iBytesWritten = 0; | |
169 | if (!m_bIsOpen) | |
170 | return -1; | |
171 | ||
172 | if (!WriteFile(m_handle, data, len, &iBytesWritten, NULL)) | |
173 | { | |
174 | m_error = "Error while writing to COM port"; | |
175 | FormatWindowsError(GetLastError(), m_error); | |
176 | return -1; | |
177 | } | |
178 | ||
179 | return (int) iBytesWritten; | |
180 | } | |
181 | ||
182 | int CSerialPort::Read(uint8_t* data, int len, int iTimeoutMs /* = -1 */) | |
183 | { | |
184 | DWORD iBytesRead = 0; | |
185 | if (m_handle == 0) | |
186 | { | |
187 | m_error = "Error while reading from COM port: invalid handle"; | |
188 | return -1; | |
189 | } | |
190 | ||
191 | if(!ReadFile(m_handle, data, len, &iBytesRead, NULL) != 0) | |
192 | { | |
193 | m_error = "unable to read from device"; | |
194 | FormatWindowsError(GetLastError(), m_error); | |
195 | iBytesRead = -1; | |
196 | } | |
197 | ||
198 | return (int) iBytesRead; | |
199 | } | |
200 | ||
201 | bool CSerialPort::SetBaudRate(int baudrate) | |
202 | { | |
203 | m_iBaudrate = baudrate; | |
204 | ||
205 | DCB dcb; | |
206 | memset(&dcb,0,sizeof(dcb)); | |
207 | dcb.DCBlength = sizeof(dcb); | |
208 | dcb.BaudRate = IntToRate(m_iBaudrate); | |
209 | dcb.fBinary = true; | |
210 | dcb.fDtrControl = DTR_CONTROL_DISABLE; | |
211 | dcb.fRtsControl = RTS_CONTROL_DISABLE; | |
212 | dcb.fOutxCtsFlow = false; | |
213 | dcb.fOutxDsrFlow = false; | |
214 | dcb.fOutX = false; | |
215 | dcb.fInX = false; | |
216 | dcb.fAbortOnError = true; | |
217 | ||
218 | if (m_iParity == PAR_NONE) | |
219 | dcb.Parity = NOPARITY; | |
220 | else if (m_iParity == PAR_EVEN) | |
221 | dcb.Parity = EVENPARITY; | |
222 | else | |
223 | dcb.Parity = ODDPARITY; | |
224 | ||
225 | if (m_iStopbits == 2) | |
226 | dcb.StopBits = TWOSTOPBITS; | |
227 | else | |
228 | dcb.StopBits = ONESTOPBIT; | |
229 | ||
230 | dcb.ByteSize = m_iDatabits; | |
231 | ||
232 | if(!SetCommState(m_handle,&dcb)) | |
233 | { | |
234 | m_error = "SetCommState failed"; | |
235 | FormatWindowsError(GetLastError(), m_error); | |
236 | return false; | |
237 | } | |
238 | ||
239 | return true; | |
240 | } |