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 6.7k 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 7 Jul 2014, 21:33 last edited by
    #1

    Hello Forum,

    i have a problem with my code. I send from Arduino a float (4 Byte) over Serial to my Software.
    I become the 4 Byte not clean.
    This is the code:

    @if(serial.bytesAvailable() == 4 ||serial.waitForReadyRead(10))
    {
    QByteArray reading = serial.readAll();
    serial.write(reading);@

    And later i convert it to float:

    @char *data = reading.data();
    float tempUmgewandelt = (float)data;
    QString temperatur = QString::number(tempUmgewandelt);@

    But the qDebug give me some other Bytes. The Arduino send a const float:

    @"ff?A"
    "27.8"@

    The first is the float as to Byte and the other is the convert Bytes to float:
    And usually the reading process isn´t clean:

    @"ff?"
    "A"
    "ff?A" @

    Can anybody help me with this problem?

    Sorry for my bad english.

    Bye

    Alex from Germany

    Qt 5.2.1 Creator 3.0.1 Windows 7 64bit

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 8 Jul 2014, 07:07 last edited by
      #2

      Use .toFloat() instead of your custom conversion:
      @
      float tempUmgewandelt = reading.toFloat();
      @

      Your logic is a bit strange. Why are you checking whether there are 4 bytes, but then proceed to read all data? You should read only 4 bytes if that is what you expect to be there.

      Then, why do you immediately send that data back to the board with write()?

      In general, to avoid data fragmentation problems, people use established protocols to send the data (for example, it is trivial to see if an XML document has been fully retrieved, and to glue different packets together if not), or at least use some kind of synchronization byte to make sure the communication works smoothly.

      (Z(:^

      1 Reply Last reply
      0
      • N Offline
        N Offline
        nickvan86
        wrote on 8 Jul 2014, 10:30 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
        • S Offline
          S Offline
          sierdzio
          Moderators
          wrote on 8 Jul 2014, 10:42 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 8 Jul 2014, 10:47 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
            • S Offline
              S Offline
              sierdzio
              Moderators
              wrote on 8 Jul 2014, 11:08 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 8 Jul 2014, 11:42 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 8 Jul 2014, 22:02 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 9 Jul 2014, 03:44 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 10 Jul 2014, 07:40 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 11 Jul 2014, 10:53 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 6 Aug 2014, 06:32 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