void SerialPort::run() { unsigned char buffer[1024]; int res = 0; int failed_reads = 0; fd_set set; timespec timeout; while (false == mAbort) { FD_ZERO(&set); /* clear the set */ FD_SET(mFd, &set); /* add our file descriptor to the set */ timeout.tv_sec = 0; timeout.tv_nsec = 10000000; res = pselect(mFd + 1, &set, NULL, NULL, &timeout, NULL); if(res < 0) { qWarning() << "Select failed in read thread"; } else if(res == 0) { // Timeout } else { res = read(mFd, buffer, 1024); if (res > 0) { failed_reads = 0; QMutexLocker locker(&mMutex); for (int i = 0;i < res;i++) { if (mCaptureBytes > 0) { if (mCaptureBuffer != 0) { mCaptureBuffer[mCaptureWrite] = buffer[i]; mCaptureWrite++; } mCaptureBytes--; if (mCaptureBytes == 0) { mCondition.wakeOne(); } } else { mReadBuffer[mBufferWrite] = buffer[i]; mBufferWrite++; //qDebug() << (quint8)buffer[i]; if (mBufferWrite == mBufferSize) { mBufferWrite = 0; } Q_EMIT serial_data_available(); } } } else { if (res < 0) { qCritical().nospace() << "Reading failed. MSG: " << strerror(errno); } else { qCritical().nospace() << "Reading serial port returned 0"; } failed_reads++; if (failed_reads > 3) { QMutexLocker locker(&mMutex); if (mIsOpen) { close(mFd); mIsOpen = false; } mAbort = true; qCritical().nospace() << "Too many consecutive failed reads. Closing port."; Q_EMIT serial_port_error(res); return; } } } } }
DiLambda::DiLambda(QObject *parent) : QObject(parent) { connect(¤t_port, SIGNAL(readyRead()), this, SLOT(data_available())); connect(¤t_port, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(serial_port_error(QSerialPort::SerialPortError))); }