bool getResponse(int fd, ZO_PROTOCOL_PACKET* p) { // tcflush(fd, TCIFLUSH); while( !(getPacketSerial(fd, p)) ) { if (serialTimeout(fd, ZO_PROTOCOL_COMMAND_RESPONSE_TIMEOUT_US) == 0) { // printf("DEBUG: timeout\n"); CommSuccess = false; Warning = ZO_WARNING_RESPONSE_TIMEOUT; break; } } if( CommSuccess == true ) { if( p->lrc != calcLRC(p) ) { CommSuccess = false; Warning = ZO_WARNING_WRONG_LRC; } if( p->commandID == ZO_PROTOCOL_ERROR_ID) { CommSuccess = false; Warning = p->data[0]; } } return CommSuccess; }
void eCtune::run() { qDebug("## eCtune dataloging engine started.\n"); m_handShakeStatus = false; m_portOK = false; m_busy = false; m_refreshTimer.setInterval(10); m_refreshTimer.setSingleShot(true); connect(this,SIGNAL(startRefreshTimer()),&m_refreshTimer,SLOT(start())); connect(this,SIGNAL(stopRefreshTimer()),&m_refreshTimer,SLOT(stop())); connect(&m_refreshTimer,SIGNAL(timeout()),this,SLOT(refresh())); m_timeoutTimer.setInterval(150); m_timeoutTimer.setSingleShot(true); connect(this,SIGNAL(startTimeoutTimer()),&m_timeoutTimer,SLOT(start())); connect(this,SIGNAL(stopTimeoutTimer()),&m_timeoutTimer,SLOT(stop())); connect(&m_timeoutTimer,SIGNAL(timeout()),this,SLOT(serialTimeout())); m_portOK = this->portSetup(this); this->m_refreshTimer.start(); }
void SerialThread::run() { QSerialPort serial; int cr; serial.setPortName(m_portName); if (!serial.setBaudRate(115200)) { qDebug() << "Serial set baud rate failed!"; emit this->serialError(tr("Can't set baud rate for %1, error code %2. %3.") .arg(m_portName).arg(serial.error()).arg(serial.errorString())); return; } if (!serial.setDataBits(QSerialPort::Data8)) { qDebug() << "Serial set data bits failed!"; emit this->serialError(tr("Can't set data bits for %1, error code %2. %3.") .arg(m_portName).arg(serial.error()).arg(serial.errorString())); return; } if (!serial.setParity(QSerialPort::NoParity)) { qDebug() << "Serial set parity failed!"; emit this->serialError(tr("Can't set parity for %1, error code %2. %3.") .arg(m_portName).arg(serial.error()).arg(serial.errorString())); return; } if (!serial.setStopBits(QSerialPort::OneStop)) { qDebug() << "Serial set stop bits failed!"; emit this->serialError(tr("Can't set stop bits for %1, error code %2. %3.") .arg(m_portName).arg(serial.error()).arg(serial.errorString())); return; } if (!serial.setFlowControl(QSerialPort::NoFlowControl)) { qDebug() << "Serial set flow control failed!"; emit this->serialError(tr("Can't set flow control for %1, error code %2. %3.") .arg(m_portName).arg(serial.error()).arg(serial.errorString())); return; } for (cr = 0; cr < m_connectAttempts; cr++) { if (serial.open(QIODevice::ReadWrite)) { break; } sleep(1); qDebug() << "Serial connect retry..."; } if (cr == m_connectAttempts) { qDebug() << "Connection failed!"; emit this->serialError(tr("Can't open %1, error code %2. %3.") .arg(m_portName).arg(serial.error()).arg(serial.errorString())); return; } emit this->serialConnected(); qDebug() << "Serial Thread is ready..."; m_mutex.lock(); /* Unlock resources and wait for the first job. */ m_cond.wait(&m_mutex); m_mutex.unlock(); while (!m_quit) { /* Protect shared resources while thread is working. */ m_mutex.lock(); if (m_txBuf.size() > 0) { qint64 bytesWritten = serial.write(m_txBuf); if (serial.waitForBytesWritten(SERIAL_WRITE_TIMEOUT_MS)) { m_txBuf.remove(0, bytesWritten); } else { qDebug() << "Write request timeout!"; /* Unlock resources and exit. */ m_mutex.unlock(); emit serialTimeout(tr("Write request timeout!")); break; } } if (serial.waitForReadyRead(SERIAL_READ_TIMEOUT_MS)) { m_rxBuf += serial.readAll(); while (serial.waitForReadyRead(SERIAL_READ_TIMEOUT_EXTRA_MS)) { m_rxBuf += serial.readAll(); } if (m_rxBuf.size() >= TELEMETRY_MSG_SIZE_BYTES) { processInputData(); } } else { qDebug() << "Read response timeout!"; /* Unlock resources and exit. */ m_mutex.unlock(); emit serialTimeout(tr("Read response timeout!")); break; } //qDebug() << "Serial Thread is sleeping..."; /* Unlock resources and wait for the next job. */ m_cond.wait(&m_mutex); m_mutex.unlock(); } qDebug() << "Serial Thread is terminating..."; serial.close(); }