[SOLVED] QObject::startTimer error when using QSerialPort::write()



  • Hi,
    I'm using QSerialPort to manage a RS232 connection. I connected two serial port on my PC (two serial adapter) and I'm trying to read and write data between them.
    I read the various helps and tutorials, and at now I successfull open the QSerialPort connection and read data from a common terminal (putty).
    The problems starts when I try to write data. I tried to echo a character received:
    @
    int main(int argc, char *argv[])
    {
    QSerialPortInfo temp;
    QList<QSerialPortInfo> lista = QSerialPortInfo::availablePorts();
    //...choose the right port.
    QSerialPort *port = new QSerialPort(temp);
    if (!port->open(QSerialPort::ReadWrite)){
    //manage the fault and close the port.
    err = port->error();
    port->close();
    return err;
    }
    //set the serial port
    port->setBaudRate(QSerialPort::Baud115200); //115200
    port->setDataBits(QSerialPort::Data8); //8
    port->setParity(QSerialPort::NoParity); //N
    port->setStopBits(QSerialPort::OneStop); //1
    //buffer
    QByteArray dataW;

    //read from the port
    while(true){
        if (port->waitForReadyRead(-1));
        //read
        dataW = port->readAll();
        //echo
        port->write(dataW);
        port->flush();
        //show in terminal
        qDebug() << dataW;
    }
    
    port->close();
    return 0;
    

    @

    When I write a character in the terminal, it is correctly show in the qDebug and in the terminal (so the character is read and written on the terminal and the serial port), but after the write command I read:

    QObject::startTimer: Timers can only be used with threads started with QThread

    I did all the application in main() function, so I don't use threads. What's goes wrong?



  • I think you need to add waitForBytesWritten() after of flush() see
    "QSerialPort::flush()":http://qt-project.org/doc/qt-5/qserialport.html#flush about absent event loop for details.

    [EDIT] Of course "use waitForBytesWritten() instead of flush()". Thanks kuzulis.



  • Do not use flush() at all, use waitForBytesWritten(-1) instead.

    Also, using of QCoreapplication is mandatory option.



  • Thanks andreyc and kuzulis,

    using waitForBytesWritten(-1) instead of flush() and creating a QApplication object, the problem is solved.

    Thanks for your helpful reply.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.