RTU Serial port isn't opening
-
Hello! I can't figure out why my QSerialPort object isn't opening. I am on Linux.
It merely outputs "QIODevice::read (QSerialPort): device not open" in the console whenever I try to read the readAll() function.
I need to connect to an RTU modbus device..cpp file:
#include <QSerialPort>
#include <QtCore>
#include <iostream>int main(int argc, char** argv)
{
QByteArray temp;
char data[8];qint64 size = 8; QSerialPort *solarCharger = new QSerialPort; solarCharger->setPortName("/dev/ttyUSB0"); solarCharger->setDataBits(QSerialPort::Data8); solarCharger->setStopBits(QSerialPort::OneStop); solarCharger->setBaudRate(QSerialPort::Baud9600); solarCharger->setParity(QSerialPort::NoParity); solarCharger->setFlowControl(QSerialPort::NoFlowControl); std::cout << solarCharger->isOpen() << std::endl; solarCharger->open(QIODevice::ReadOnly); std::cout << solarCharger->isOpen() << std::endl; temp = solarCharger->readAll(); std::cout << temp[0] << std::endl; delete solarCharger; solarCharger = nullptr; return 0;
}
.pro file:
TEMPLATE = app
TARGET = Qt
INCLUDEPATH += .DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += simple.cpp
QT += serialport core
-
@Tague-Carlyon Does your open() call succeed?
Connect something to the QSerialPort::errorOccurred() signal to see what sort of error you have.
You never execute the application event loop so it is unlikely that there will ever be data to read.
-
@ChrisW67 When I had access to the RTU port on the MPPT solar charger, I don't think the open() call succeeded. I will update you tomorrow with more information when I will have access to the MPPT solar charger again. In the meantime, I apologize but I don't fully understand how to connect something to the QSerialPort::errorOccurred(). I understand the QObject::connect() function loosely but not enough to do what you are asking from scratch.
-
QSerialPort is, like most Qt I/O, designed to be used asynchronously. Setting it up that way and connecting the errorOccurred() signal to a slot that can handle it goes something like this:
#include <QCoreApplication> #include <QDebug> #include <QSerialPort> #include <QTimer> class SerialReceiver: public QObject { Q_OBJECT public: explicit SerialReceiver(QObject *p = nullptr) : QObject(p) , m_solarCharger(nullptr) { m_solarCharger = new QSerialPort(this); connect(m_solarCharger, &QSerialPort::readyRead, this, &SerialReceiver::handleReadyRead); connect(m_solarCharger, &QSerialPort::errorOccurred, this, &SerialReceiver::handleErrorOccurred); m_solarCharger->setPortName("/dev/ttyUSB0"); m_solarCharger->setDataBits(QSerialPort::Data8); m_solarCharger->setStopBits(QSerialPort::OneStop); m_solarCharger->setBaudRate(QSerialPort::Baud9600); m_solarCharger->setParity(QSerialPort::NoParity); m_solarCharger->setFlowControl(QSerialPort::NoFlowControl); if (!m_solarCharger->open(QSerialPort::ReadOnly)) { qDebug() << Q_FUNC_INFO << "Failed to open serial port"; } } ~SerialReceiver() { } private slots: void handleReadyRead() { QByteArray temp = m_solarCharger->readAll(); qDebug() << temp.size() << "bytes read"; } void handleErrorOccurred(QSerialPort::SerialPortError error) { if (error != QSerialPort::NoError) { qDebug() << Q_FUNC_INFO << error; } } private: QSerialPort *m_solarCharger ; }; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); SerialReceiver receiver; // arrange for the program to end itself after 15 seconds QTimer::singleShot(15000, &a, &QCoreApplication::quit); return a.exec(); } #include "main.moc"
-
the event loop of QCoreApplications needs to be running, otherwise this won't work at all.