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 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 bool SetTimeouts(serial_socket_t socket
, int* iError
, bool bBlocking
)
53 if (socket
== INVALID_HANDLE_VALUE
)
57 if (!GetCommTimeouts(socket
, &cto
))
59 *iError
= GetLastError();
65 cto
.ReadIntervalTimeout
= 0;
66 cto
.ReadTotalTimeoutConstant
= 0;
67 cto
.ReadTotalTimeoutMultiplier
= 0;
71 cto
.ReadIntervalTimeout
= MAXDWORD
;
72 cto
.ReadTotalTimeoutConstant
= 0;
73 cto
.ReadTotalTimeoutMultiplier
= 0;
76 if (!SetCommTimeouts(socket
, &cto
))
78 *iError
= GetLastError();
85 void CSerialSocket::Close(void)
87 SerialSocketClose(m_socket
);
90 void CSerialSocket::Shutdown(void)
92 SerialSocketClose(m_socket
);
95 ssize_t
CSerialSocket::Write(void* data
, size_t len
)
97 return SerialSocketWrite(m_socket
, &m_iError
, data
, len
);
100 ssize_t
CSerialSocket::Read(void* data
, size_t len
, uint64_t iTimeoutMs
/* = 0 */)
102 return SerialSocketRead(m_socket
, &m_iError
, data
, len
, iTimeoutMs
);
105 bool CSerialSocket::Open(uint64_t iTimeoutMs
/* = 0 */)
108 CStdString strComPath
= "\\\\.\\" + m_strName
;
109 CLockObject
lock(m_mutex
);
110 m_socket
= CreateFile(strComPath
.c_str(), GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
111 if (m_socket
== INVALID_HANDLE_VALUE
)
113 m_strError
= "Unable to open COM port";
114 FormatWindowsError(GetLastError(), m_strError
);
118 COMMCONFIG commConfig
= {0};
119 DWORD dwSize
= sizeof(commConfig
);
120 commConfig
.dwSize
= dwSize
;
121 if (GetDefaultCommConfig(strComPath
.c_str(), &commConfig
,&dwSize
))
123 if (!SetCommConfig(m_socket
, &commConfig
,dwSize
))
125 m_strError
= "unable to set default config";
126 FormatWindowsError(GetLastError(), m_strError
);
131 m_strError
= "unable to get default config";
132 FormatWindowsError(GetLastError(), m_strError
);
135 if (!SetupComm(m_socket
, 64, 64))
137 m_strError
= "unable to set up the com port";
138 FormatWindowsError(GetLastError(), m_strError
);
141 if (!SetBaudRate(m_iBaudrate
))
143 m_strError
= "unable to set baud rate";
144 FormatWindowsError(GetLastError(), m_strError
);
149 if (!SetTimeouts(m_socket
, &m_iError
, false))
151 m_strError
= "unable to set timeouts";
152 FormatWindowsError(GetLastError(), m_strError
);
161 bool CSerialSocket::SetBaudRate(uint32_t baudrate
)
163 int32_t rate
= IntToBaudrate(baudrate
);
165 m_iBaudrate
= baudrate
> 0 ? baudrate
: 0;
170 memset(&dcb
,0,sizeof(dcb
));
171 dcb
.DCBlength
= sizeof(dcb
);
172 dcb
.BaudRate
= IntToBaudrate(m_iBaudrate
);
174 dcb
.fDtrControl
= DTR_CONTROL_DISABLE
;
175 dcb
.fRtsControl
= RTS_CONTROL_DISABLE
;
176 dcb
.fOutxCtsFlow
= false;
177 dcb
.fOutxDsrFlow
= false;
180 dcb
.fAbortOnError
= true;
182 if (m_iParity
== SERIAL_PARITY_NONE
)
183 dcb
.Parity
= NOPARITY
;
184 else if (m_iParity
== SERIAL_PARITY_EVEN
)
185 dcb
.Parity
= EVENPARITY
;
187 dcb
.Parity
= ODDPARITY
;
189 if (m_iStopbits
== SERIAL_STOP_BITS_TWO
)
190 dcb
.StopBits
= TWOSTOPBITS
;
192 dcb
.StopBits
= ONESTOPBIT
;
194 dcb
.ByteSize
= (BYTE
)m_iDatabits
;
196 if(!SetCommState(m_socket
,&dcb
))
198 m_strError
= "SetCommState failed";
199 FormatWindowsError(GetLastError(), m_strError
);