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. Accessing QByteArray from QSerialPort
QtWS25 Last Chance

Accessing QByteArray from QSerialPort

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.2k 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.
  • L Offline
    L Offline
    lociluke
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      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.

      (Z(:^

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lociluke
        wrote on last edited by
        #3

        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.

        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