Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

try to read data from QSerialPort



  • Hi there,
    I'm new to QT and try to read data from an serial port. I use the QSerialPort.

    I have a connect statement like that:

    Comcontroller::Comcontroller(QSerialPort *serialPort, QObject *parent)
        : QObject(parent)
        , m_serialPort(serialPort){
    
        connect(m_serialPort,SIGNAL(readyRead()),this, SLOT(ReceiveData()));
        qDebug() << "Start reading COM:" << m_serialPort->portName();
        if (!m_serialPort->open(QIODevice::ReadOnly)) {
                qDebug() << QObject::tr("Failed to open port %1, error: %2").arg(m_serialPort->portName()).arg(m_serialPort->errorString()) << endl;
        }
    }
    

    and my SLOT (ReceiveData()) is never reached..

    I know that the data is coming in because when i try to do it synchronously with code like that:

        QByteArray readData = serialPort.readAll();
            while (serialPort.waitForReadyRead(5000))
                readData.append(serialPort.readAll());
    
            if (serialPort.error() == QSerialPort::ReadError) {
                qDebug() << QObject::tr("Failed to read from port %1, error: %2").arg(serialPort.portName()).arg(serialPort.errorString()) << endl;
            } else if (serialPort.error() == QSerialPort::TimeoutError && readData.isEmpty()) {
                qDebug() << QObject::tr("No data was currently available for reading from port %1").arg(serialPort.portName()) << endl;
                       }
    
           qDebug() << QObject::tr("Data successfully received from port %1").arg(serialPort.portName()) << endl;
           qDebug() << readData << endl;
    

    the data is shown correctly. And i really dont know why ReceiveData() is never reached..

    Maybe someone of you have an idea why its like that.

    If you need further information please let me know!

    Greetings
    Felix



  • @fehe
    Is your Comcontroller object kept in existence? Your slot is in the instance.



  • @JonB
    Hi,

    i call it in my mainwindow like that:

     Comcontroller controller(&serialPort);
    

    so i guess it should be kept in existence



  • @fehe
    Well from that I can see it's a local variable. But where is that code?



  • @JonB
    Well in my MainWindow.. so not in my "Comcontroller".. but maybe thats the problem, that its no member of it, so it will be created and terminated right away?



  • @fehe
    It's a bit hard to be sure without seeing the code, but if you mean in your QMainWindow instance then that sounds OK. What you cannot do is put it in some function which has exited, like:

    func()
    {
        ...
        Comcontroller controller(&serialPort);
       ...
    }
    

    After you have called func(), you're not left with a Comcontroller to have slots for signals from the serial port. So long as you understand that. Other questions I have seen let the local variable go out of scope without realising that will lose their slot code.

    The other thing is, unlike the synchronous code, you must allow the main Qt event loop to be running for the readyRead() to be signalled. I take it you are doing that?


Log in to reply