Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Brainstorm
  4. QByteArray read only 4 Byte from Arduino
Forum Updated to NodeBB v4.3 + New Features

QByteArray read only 4 Byte from Arduino

Scheduled Pinned Locked Moved Brainstorm
12 Posts 5 Posters 7.1k 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.
  • N Offline
    N Offline
    nickvan86
    wrote on last edited by
    #3

    Hello,

    i have test this code:

    @if(serial.bytesAvailable() >= 4 || serial.waitForReadyRead(10))
    {
    QByteArray reading = serial.read(4);
    qDebug() << reading;
    qDebug() << reading.size();@

    the "4" from read is the size from 4 byte. is this correct?
    The Debug give me some information:

    @4
    "27.8"
    QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
    QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
    QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
    "ff"
    2
    "?A"
    2
    "ff?A"
    4
    "27.8"
    QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
    QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
    QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
    "f"
    1
    "f?A"
    3
    "ff?A"
    4
    "27.8"
    QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
    QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
    QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
    "f"
    1
    "f?A"
    3 @

    Qt 5.2.1 Creator 3.0.1 Windows 7 64bit

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

      It is indeed correct. But it can't read 4 bytes if only 2 are available, right?

      (Z(:^

      1 Reply Last reply
      0
      • N Offline
        N Offline
        nickvan86
        wrote on last edited by
        #5

        That is correct. But i send from Arduino 4 Bytes to the Serial port. But how must i do read the 4 Byte´s?

        Qt 5.2.1 Creator 3.0.1 Windows 7 64bit

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

          I think "serial.waitForReadyRead(10)" is causing you to read the packets too soon. But it might be that there are also some problems where sending, some additional buffering, etc. That is why it is always good to add some kind of packet glueing and checking mechanism, even in very simple systems.

          (Z(:^

          1 Reply Last reply
          0
          • N Offline
            N Offline
            nickvan86
            wrote on last edited by
            #7

            When i delete the "serial.waitForReadyRead(10)" instruction the if do nothing. How can i send or do a checking mechanism?

            Qt 5.2.1 Creator 3.0.1 Windows 7 64bit

            1 Reply Last reply
            0
            • N Offline
              N Offline
              nickvan86
              wrote on last edited by
              #8

              I have change my if and this works better:

              @if(serial.bytesAvailable() >= 4 || (serial.waitForReadyRead(10) && serial.size() == 4))
              {
              QByteArray reading = serial.read(4);@

              When i send more data´s over the serial port. how must change the code?
              For example i send from arduino following data´s:

              Temp1
              23.4
              Temp2
              45
              Temp3
              12.5

              My if take every 4 byte and change it into a float. What must i do when i send more datas?

              Qt 5.2.1 Creator 3.0.1 Windows 7 64bit

              1 Reply Last reply
              0
              • Q Offline
                Q Offline
                qxoz
                wrote on last edited by
                #9

                [quote author="nickvan86" date="1404856945"]What must i do when i send more datas?[/quote]
                Use "readyRead":http://qt-project.org/doc/qt-5/qiodevice.html#readyRead signal.
                Also take a look to "examples":http://qt-project.org/doc/qt-5/qtserialport-terminal-example.html

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  nickvan86
                  wrote on last edited by
                  #10

                  Hello,

                  bq. Also take a look to examples [qt-project.org]

                  I have check the example Terminal. When i send "27.0000" and "Hallo" the terminal show the right data, but when i Debug the readFunction i become this:

                  @"27.0000HALLO"
                  "27.00"
                  "00HALLO"
                  "27.0000HALLO"
                  "27."
                  "0000HALLO"
                  "27.0000HAL"
                  "LO"
                  "2"
                  "7.0000HALLO"
                  "27.0000H"
                  "ALLO"@

                  The Terminal puts it together and than is the right order.
                  I must show how solve this problem.
                  But thanks for the tipp.

                  Bye Alex

                  Qt 5.2.1 Creator 3.0.1 Windows 7 64bit

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

                    bq. I have check the example Terminal. When i send “27.0000” and “Hallo”

                    The serial port reads your data stream by a some chunks. Size of those chunks can be randomly, depends on state of Windows (Linux, etc) scheduler and so on...

                    The qDebug() to end of each message adds the \n\r symbols, but the Terminal do not adds any additional symbols.

                    Thus, the qDebug() print out a received data (chunks of data) on the new line.

                    bq. I must show how solve this problem.

                    This is not a problem.

                    1 Reply Last reply
                    0
                    • jensen82J Offline
                      jensen82J Offline
                      jensen82
                      wrote on last edited by
                      #12

                      I recommend to use the RS232-Protocol and use STX and EOT to define data packets.

                      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