MCPcopy Create free account
hub / github.com/Pulse-Eight/libcec / Open

Method Open

src/libcec/platform/windows/serialport.cpp:109–166  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

107}
108
109bool CSerialSocket::Open(uint64_t iTimeoutMs /* = 0 */)
110{
111 iTimeoutMs = 0;
112 if (IsOpen())
113 return false;
114
115 std::string strComPath = "\\\\.\\" + m_strName;
116 CLockObject lock(m_mutex);
117 m_socket = CreateFile(strComPath.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
118 if (m_socket == INVALID_HANDLE_VALUE)
119 {
120 m_strError = "Unable to open COM port";
121 FormatWindowsError(GetLastError(), m_strError);
122 return false;
123 }
124
125 COMMCONFIG commConfig = {0};
126 DWORD dwSize = sizeof(commConfig);
127 commConfig.dwSize = dwSize;
128 if (GetDefaultCommConfig(strComPath.c_str(), &commConfig,&dwSize))
129 {
130 if (!SetCommConfig(m_socket, &commConfig,dwSize))
131 {
132 m_strError = "unable to set default config";
133 FormatWindowsError(GetLastError(), m_strError);
134 }
135 }
136 else
137 {
138 m_strError = "unable to get default config";
139 FormatWindowsError(GetLastError(), m_strError);
140 }
141
142 if (!SetupComm(m_socket, 64, 64))
143 {
144 m_strError = "unable to set up the com port";
145 FormatWindowsError(GetLastError(), m_strError);
146 }
147
148 if (!SetBaudRate(m_iBaudrate))
149 {
150 m_strError = "unable to set baud rate";
151 FormatWindowsError(GetLastError(), m_strError);
152 Close();
153 return false;
154 }
155
156 if (!SetTimeouts(m_socket, &m_iError, false))
157 {
158 m_strError = "unable to set timeouts";
159 FormatWindowsError(GetLastError(), m_strError);
160 Close();
161 return false;
162 }
163
164 m_bIsOpen = true;
165 return m_bIsOpen;
166}

Callers

nothing calls this directly

Calls 2

FormatWindowsErrorFunction · 0.85
CloseFunction · 0.85

Tested by

no test coverage detected