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 reading blocks writing even using readyRead()
Qt 6.11 is out! See what's new in the release blog

QSerialport reading blocks writing even using readyRead()

Scheduled Pinned Locked Moved Unsolved General and Desktop
qserialport
2 Posts 2 Posters 1.3k Views 1 Watching
  • 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.
  • R Offline
    R Offline
    RolBri
    wrote on last edited by
    #1

    Hi,

    I am having a problem with QSerialPort and sadly I have no clue to solve it.
    So it would be very great if you could help me ;-)

    I am using the readyRead-signal for calling the following function:

    void SerialPort::readSerialData()
    {
        char ch;
        while(device->bytesAvailable())
        {
            device->getChar(&ch);
            serialDataBuffer+=ch;
            if(ch == '\r')
            {
                dateTime= QDateTime::currentDateTime();
                message= serialDataBuffer;
                emit newMessage(message,dateTime);
                serialDataBuffer.clear();
            }
        }
    }
    

    This is how my writing function looks:

    void SerialPort::writeString(QByteArray message)
    {
        if(device->isOpen() && device->isWritable())
        {
            device->write(message);
            device->flush();
            if (!device->waitForBytesWritten(100)) std::cerr << "Error writing message" << std::endl;
        }
        else
            std::cerr << "Device is not writeable or not open! "<< std::endl;
    
    }
    

    I need to send 5 different messages every second.
    Therefore I have another thread with a QTimer. Whenever the timer is elapsed the write function above is called with a Qt::QueuedConnection.

    Whenever I now have a data input this is blocking the writing.
    I tried to share the pointer for the QSerialPort-instance with two threads (one for writing and one for reading), but it says that QSerialPort cannot have different parents.

    How can I solve this?

    Thank you very much :-)

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What about reading the whole buffer and then search for the first occurrence of '\r' ? That way you avoid to loop on the input

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      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