+ CLockObject lock(m_mutex);
+
+ if (!m_port)
+ {
+ CLibCEC::AddLog(CEC_LOG_ERROR, "port is NULL");
+ return false;
+ }
+
+ if (IsOpen())
+ {
+ CLibCEC::AddLog(CEC_LOG_ERROR, "port is already open");
+ return true;
+ }
+
+ m_callback = cb;
+ CStdString strError;
+ bool bConnected(false);
+ while (!bConnected && iNow < iTimeout)
+ {
+ if ((bConnected = m_port->Open(iTimeout)) == false)
+ {
+ strError.Format("error opening serial port '%s': %s", m_port->GetName().c_str(), m_port->GetError().c_str());
+ Sleep(250);
+ iNow = GetTimeMs();
+ }
+ }
+
+ if (!bConnected)
+ {
+ CLibCEC::AddLog(CEC_LOG_ERROR, strError);
+ return false;
+ }
+
+ CLibCEC::AddLog(CEC_LOG_DEBUG, "connection opened, clearing any previous input and waiting for active transmissions to end before starting");
+
+ //clear any input bytes
+ uint8_t buff[1024];
+ while (m_port->Read(buff, 1024, 100) > 0)
+ {
+ CLibCEC::AddLog(CEC_LOG_DEBUG, "data received, clearing it");
+ Sleep(250);
+ }