Solved QSerialPort::readyRead() emiting not working?
-
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?
-
@shokarta I'm pretty sure, your port has to remain open, for it to emit readyRead
-
@shokarta I'm pretty sure, your port has to remain open, for it to emit readyRead
-
@shokarta said in QSerialPort::readyRead() emiting not working?:
m_serial.waitForBytesWritten(1500);
Don't use
waitFor...
in programs that also use Signals&Slots.Regards