QSerialport reading blocks writing even using readyRead()



  • 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 :-)


  • Lifetime Qt Champion

    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


Log in to reply
 

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