Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. RTU Serial port isn't opening
QtWS25 Last Chance

RTU Serial port isn't opening

Scheduled Pinned Locked Moved Unsolved General and Desktop
qserialportrtu client
5 Posts 3 Posters 720 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tague Carlyon
    wrote on last edited by
    #1

    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

    J.HilkJ 1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      @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.

      1 Reply Last reply
      2
      • T Offline
        T Offline
        Tague Carlyon
        wrote on last edited by Tague Carlyon
        #3

        @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.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          ChrisW67
          wrote on last edited by ChrisW67
          #4

          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"
          
          1 Reply Last reply
          3
          • T Tague Carlyon

            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

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @Tague-Carlyon

            the event loop of QCoreApplications needs to be running, otherwise this won't work at all.


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            3

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved