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. QSerialPort::readyRead() emiting not working?

QSerialPort::readyRead() emiting not working?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 311 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.
  • S Offline
    S Offline
    shokarta
    wrote on last edited by
    #1

    Im trying to figure out if its because of QSerialPort bug (but was fixed in 5.13.2 and im on 5.14.0 rc2).

    led.h:

    #ifndef LED_H
    #define LED_H
    
    #include <QObject>
    #include <QSerialPort>
    #include <QCoreApplication>
    
    class LED : public QObject
    {
        Q_OBJECT
    
    public:
        explicit LED(QObject *parent = nullptr);
        Q_INVOKABLE void setColor(int red, int green, int blue);
    
    public slots:
    
    private:
        QSerialPort m_serial;
        void readReply();
    
    signals:
    
    };
    
    #endif // LED_H
    

    led.cpp:

    #include <QtDebug>
    #include "led.h"
    #include <windows.h>
    #include <QSerialPort>
    #include <QSerialPortInfo>
    #include <fileapi.h>
    #include <QDataStream>
    #include <QtSerialPort/QSerialPort>
    #include <QCoreApplication>
    
    QString m_port = "COM3";
    
    LED::LED(QObject *parent) :
        QObject(parent)
    {
        connect(&m_serial, &QSerialPort::readyRead, this, &LED::readReply);
    }
    
    void LED::readReply()
    {
        const QByteArray receivedData = m_serial.readAll();
        qDebug() << "Received:" << receivedData;
    }
    
    void LED::setColor(int red, int green, int blue)
    {
        QByteArray writeData;
        writeData.resize(15);
    	// some noniportant staff populating writeData
    
        m_serial.setPortName(m_port);
    
        if (!m_serial.open(QIODevice::ReadWrite)) {
            qDebug() << "Can not open the" << m_port << "port:" << m_serial.errorString() << "//" << m_serial.error();
            m_serial.close();
            return;
        }
        if (!m_serial.setBaudRate(QSerialPort::Baud9600)) {
            qDebug() << "Could not set baud rate to 9600, remaining value is" << m_serial.baudRate();
            m_serial.close();
            return;
        }
        if (!m_serial.setDataBits(QSerialPort::Data8)) {
            qDebug() << "Could not set data bits number, remaining value is" << m_serial.dataBits();
            m_serial.close();
            return;
        }
        if (!m_serial.setParity(QSerialPort::NoParity)) {
            qDebug() << "Could not set parity check option, remaining value is" << m_serial.parity();
            m_serial.close();
            return;
        }
        if (!m_serial.setStopBits(QSerialPort::OneStop)) {
            qDebug() << "Could not set stop bits, remaining value is" << m_serial.stopBits();
            m_serial.close();
            return;
        }
    
        m_serial.write(writeData);
        m_serial.waitForBytesWritten(1500);     // max timeout 1000ms, set -1 to wait up to 30s
        qDebug() << "Sending:" << writeData;
    
        m_serial.close();
        return;
    }
    

    first of all, writing to the COM port works fine... its a LED screen and when I write() it changes color, so I can see it works fine... also the qDebug() << "Sending:" << writeData worksfine…

    however, I have no responce whatsoever in my debug log...

    what do I do wrong?

    J.HilkJ aha_1980A 2 Replies Last reply
    0
    • S shokarta

      Im trying to figure out if its because of QSerialPort bug (but was fixed in 5.13.2 and im on 5.14.0 rc2).

      led.h:

      #ifndef LED_H
      #define LED_H
      
      #include <QObject>
      #include <QSerialPort>
      #include <QCoreApplication>
      
      class LED : public QObject
      {
          Q_OBJECT
      
      public:
          explicit LED(QObject *parent = nullptr);
          Q_INVOKABLE void setColor(int red, int green, int blue);
      
      public slots:
      
      private:
          QSerialPort m_serial;
          void readReply();
      
      signals:
      
      };
      
      #endif // LED_H
      

      led.cpp:

      #include <QtDebug>
      #include "led.h"
      #include <windows.h>
      #include <QSerialPort>
      #include <QSerialPortInfo>
      #include <fileapi.h>
      #include <QDataStream>
      #include <QtSerialPort/QSerialPort>
      #include <QCoreApplication>
      
      QString m_port = "COM3";
      
      LED::LED(QObject *parent) :
          QObject(parent)
      {
          connect(&m_serial, &QSerialPort::readyRead, this, &LED::readReply);
      }
      
      void LED::readReply()
      {
          const QByteArray receivedData = m_serial.readAll();
          qDebug() << "Received:" << receivedData;
      }
      
      void LED::setColor(int red, int green, int blue)
      {
          QByteArray writeData;
          writeData.resize(15);
      	// some noniportant staff populating writeData
      
          m_serial.setPortName(m_port);
      
          if (!m_serial.open(QIODevice::ReadWrite)) {
              qDebug() << "Can not open the" << m_port << "port:" << m_serial.errorString() << "//" << m_serial.error();
              m_serial.close();
              return;
          }
          if (!m_serial.setBaudRate(QSerialPort::Baud9600)) {
              qDebug() << "Could not set baud rate to 9600, remaining value is" << m_serial.baudRate();
              m_serial.close();
              return;
          }
          if (!m_serial.setDataBits(QSerialPort::Data8)) {
              qDebug() << "Could not set data bits number, remaining value is" << m_serial.dataBits();
              m_serial.close();
              return;
          }
          if (!m_serial.setParity(QSerialPort::NoParity)) {
              qDebug() << "Could not set parity check option, remaining value is" << m_serial.parity();
              m_serial.close();
              return;
          }
          if (!m_serial.setStopBits(QSerialPort::OneStop)) {
              qDebug() << "Could not set stop bits, remaining value is" << m_serial.stopBits();
              m_serial.close();
              return;
          }
      
          m_serial.write(writeData);
          m_serial.waitForBytesWritten(1500);     // max timeout 1000ms, set -1 to wait up to 30s
          qDebug() << "Sending:" << writeData;
      
          m_serial.close();
          return;
      }
      

      first of all, writing to the COM port works fine... its a LED screen and when I write() it changes color, so I can see it works fine... also the qDebug() << "Sending:" << writeData worksfine…

      however, I have no responce whatsoever in my debug log...

      what do I do wrong?

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

      @shokarta I'm pretty sure, your port has to remain open, for it to emit readyRead


      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
      • S shokarta

        Im trying to figure out if its because of QSerialPort bug (but was fixed in 5.13.2 and im on 5.14.0 rc2).

        led.h:

        #ifndef LED_H
        #define LED_H
        
        #include <QObject>
        #include <QSerialPort>
        #include <QCoreApplication>
        
        class LED : public QObject
        {
            Q_OBJECT
        
        public:
            explicit LED(QObject *parent = nullptr);
            Q_INVOKABLE void setColor(int red, int green, int blue);
        
        public slots:
        
        private:
            QSerialPort m_serial;
            void readReply();
        
        signals:
        
        };
        
        #endif // LED_H
        

        led.cpp:

        #include <QtDebug>
        #include "led.h"
        #include <windows.h>
        #include <QSerialPort>
        #include <QSerialPortInfo>
        #include <fileapi.h>
        #include <QDataStream>
        #include <QtSerialPort/QSerialPort>
        #include <QCoreApplication>
        
        QString m_port = "COM3";
        
        LED::LED(QObject *parent) :
            QObject(parent)
        {
            connect(&m_serial, &QSerialPort::readyRead, this, &LED::readReply);
        }
        
        void LED::readReply()
        {
            const QByteArray receivedData = m_serial.readAll();
            qDebug() << "Received:" << receivedData;
        }
        
        void LED::setColor(int red, int green, int blue)
        {
            QByteArray writeData;
            writeData.resize(15);
        	// some noniportant staff populating writeData
        
            m_serial.setPortName(m_port);
        
            if (!m_serial.open(QIODevice::ReadWrite)) {
                qDebug() << "Can not open the" << m_port << "port:" << m_serial.errorString() << "//" << m_serial.error();
                m_serial.close();
                return;
            }
            if (!m_serial.setBaudRate(QSerialPort::Baud9600)) {
                qDebug() << "Could not set baud rate to 9600, remaining value is" << m_serial.baudRate();
                m_serial.close();
                return;
            }
            if (!m_serial.setDataBits(QSerialPort::Data8)) {
                qDebug() << "Could not set data bits number, remaining value is" << m_serial.dataBits();
                m_serial.close();
                return;
            }
            if (!m_serial.setParity(QSerialPort::NoParity)) {
                qDebug() << "Could not set parity check option, remaining value is" << m_serial.parity();
                m_serial.close();
                return;
            }
            if (!m_serial.setStopBits(QSerialPort::OneStop)) {
                qDebug() << "Could not set stop bits, remaining value is" << m_serial.stopBits();
                m_serial.close();
                return;
            }
        
            m_serial.write(writeData);
            m_serial.waitForBytesWritten(1500);     // max timeout 1000ms, set -1 to wait up to 30s
            qDebug() << "Sending:" << writeData;
        
            m_serial.close();
            return;
        }
        

        first of all, writing to the COM port works fine... its a LED screen and when I write() it changes color, so I can see it works fine... also the qDebug() << "Sending:" << writeData worksfine…

        however, I have no responce whatsoever in my debug log...

        what do I do wrong?

        aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @shokarta said in QSerialPort::readyRead() emiting not working?:

        m_serial.waitForBytesWritten(1500);

        Don't use waitFor... in programs that also use Signals&Slots.

        Regards

        Qt has to stay free or it will die.

        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