#include "lts_serial_port_querry.h" #include namespace LtsSerialPortQuerry { LtsSerialPortQuerry::LtsSerialPortQuerry(QObject *parent) : QObject(parent) , m_currentQuerry(0) , m_baudRate(QSerialPort::Baud115200) , m_openModeFlag(QSerialPort::ReadWrite) , m_dataBits(QSerialPort::Data8) , m_flowControll(QSerialPort::NoFlowControl) , m_parity(QSerialPort::NoParity) , m_stopBits(QSerialPort::OneStop) , m_currentSerialPort(nullptr) , m_logger(getLogger("LtsSerialPortQuerry")) { } void LtsSerialPortQuerry::setSerialPortSettings(const QSerialPort::BaudRate &baudRate, const QIODevice::OpenModeFlag &openModeFlag, const QSerialPort::DataBits &dataBits, const QSerialPort::FlowControl &flowControll, const QSerialPort::Parity &parity, const QSerialPort::StopBits &stopBits) { m_baudRate = baudRate; m_openModeFlag = openModeFlag; m_dataBits = dataBits; m_flowControll = flowControll; m_parity = parity; m_stopBits = stopBits; } void LtsSerialPortQuerry::continueQuerry() { m_currentQuerry++; if (m_currentQuerry>= m_serialPortInfos.size()) { restartQuerry(); return; } querrySerialPort(m_serialPortInfos.at(m_currentQuerry)); } void LtsSerialPortQuerry::restartQuerry() { DEBUG_LOG(m_logger) << "restart querry "; m_serialPortInfos = QSerialPortInfo::availablePorts(); m_currentQuerry = 0; if (m_serialPortInfos.size()) querrySerialPort(m_serialPortInfos.at(m_currentQuerry)); else QTimer::singleShot(1000, this, &LtsSerialPortQuerry::restartQuerry); } QSerialPort *LtsSerialPortQuerry::currentSerialPort() { return m_currentSerialPort; } void LtsSerialPortQuerry::querrySerialPort(const QSerialPortInfo &info) { closeAndDeleteCurrentPort(); m_currentSerialPort = new QSerialPort(info, this); m_currentSerialPort->setBaudRate(m_baudRate); m_currentSerialPort->setDataBits(m_dataBits); m_currentSerialPort->setParity(m_parity); m_currentSerialPort->setStopBits(m_stopBits); m_currentSerialPort->setFlowControl(m_flowControll); DEBUG_LOG(m_logger) << "Try opening Comport:" << m_currentSerialPort->portName() << " index is: " << m_currentQuerry; if (!m_currentSerialPort->open(QSerialPort::ReadWrite)) { DEBUG_LOG(m_logger) << "Oppening failed query next"; continueQuerry(); }else { connect(m_currentSerialPort, &QSerialPort::errorOccurred, this, &LtsSerialPortQuerry::onSerialPortError, Qt::UniqueConnection); DEBUG_LOG(m_logger) << "Oppened comport" << m_currentSerialPort->portName(); emit connectionEstablished(); } } void LtsSerialPortQuerry::closeAndDeleteCurrentPort() { if (m_currentSerialPort) { m_currentSerialPort->disconnect(); if (m_currentSerialPort->isOpen()) m_currentSerialPort->close(); m_currentSerialPort->deleteLater(); m_currentSerialPort = nullptr; } } void LtsSerialPortQuerry::onSerialPortError(QSerialPort::SerialPortError serialPortError) { if (!m_currentSerialPort) return; DEBUG_LOG(m_logger) << "Spontaneous error " << serialPortError << " doesn't have to be an error ..."; const QString errStr(QStringLiteral("QSerialPort::SerialPortError ") + QString::number(serialPortError) + QStringLiteral(" from serial port ") + m_currentSerialPort->portName() + QStringLiteral(" error: ") + m_currentSerialPort->errorString()); switch (serialPortError) { case QSerialPort::SerialPortError::NoError: { break; } case QSerialPort::SerialPortError::DeviceNotFoundError: case QSerialPort::SerialPortError::PermissionError: case QSerialPort::SerialPortError::OpenError: case QSerialPort::SerialPortError::NotOpenError: case QSerialPort::SerialPortError::UnknownError: { ERROR_LOG(m_logger) << "Closing port and try to reopen it. " << m_currentSerialPort->portName(); continueQuerry(); Q_FALLTHROUGH(); } case QSerialPort::SerialPortError::UnsupportedOperationError: { ERROR_LOG(m_logger) << errStr; break; } default: { ERROR_LOG(m_logger) << "Got an Error> " << errStr << "restarting"; continueQuerry(); break; } } } }