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. [SOLVED] QObject::startTimer error when using QSerialPort::write()
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 3.4k 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.
  • E Offline
    E Offline
    elfracca
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andreyc
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kuzulis
        Qt Champions 2020
        wrote on last edited by
        #3

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

        Also, using of QCoreapplication is mandatory option.

        1 Reply Last reply
        0
        • E Offline
          E Offline
          elfracca
          wrote on last edited by
          #4

          Thanks andreyc and kuzulis,

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

          Thanks for your helpful reply.

          1 Reply Last reply
          0

          • Login

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