Accessing QByteArray from QSerialPort



  • Hi everyone,

    got a new problem. I set up the serial port und let it just read:
    @QByteArray readData = serialPort.readAll();
    while(serialPort.waitForReadyRead(5000))
    readData.append(serialPort.readAll());@

    Now I want to access the array to go on with my program. How am I able to do so? Anyone got an idea?

    The ongoing of my program proceeds with an if-loop in which I want to query something within the array.
    Will be thankful for every advice.


  • Moderators

    Qt, including serial ports, works using an event loop. There is no need to create a reading loop on your own, as you can leverage the signals and slots to get the new data when it's available. This asynchronous approach keeps your UI responsive and can although it's harder to implement, it results in a better structure of the code later on.

    Please check out "this":http://qt-project.org/doc/qt-5/eventsandfilters.html, "this":http://qt-project.org/doc/qt-5/signalsandslots.html and "this":http://qt-project.org/doc/qt-5/qiodevice.html#readyRead (QSerialPort is based on QIODevice). Alternatively, you can periodically scan the output using QTimer.



  • But when I use readyRead() does the cpp-code not go on with its progress? I instead want to let the cpp-code go on when it reads something specific in the QByteArray, so I wanted to know how to access it.. The reading is working with an QString test (readData) I made.
    I tried something like:
    @if(test.compare("go1OK")==0)@
    The accessing works and he goes on with my cpp-code but I'm not sure if it's the right way to do it.

    Because I have to use the codepart in my first post more often in the cpp-code, the debug console gives me an error with multiple definition. Because of that I wanted to use a statemachine but still have to figure out how it works.

    I'm sorry for not mention that I'm very new to Qt and QML and that I'm using Qt Quick 2.

    Do you maybe have an example for the method you mentioned with QTimer? Tried QTimer several times before but it did not work.


Log in to reply
 

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