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. [SOLVED] Nasty String Ending
QtWS25 Last Chance

[SOLVED] Nasty String Ending

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 3.0k 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.
  • D Offline
    D Offline
    david.luggen
    wrote on last edited by
    #1

    Hi there

    I need to transmit an int16 over a serial port. To do this in a fast way, I split the variable into two byte. Now, when I want to read these two byte into a QString or a QByteArray because the read() - function of the qserialdevice library allows no other type to write in, I get a problem. When the value of my int16 is below 256, the first byte is 0. And unfortunatly 0 is the string ending constant. So my string stops at this point and I don't gat any more information in that String. Does anyone have an idea how to bypass this string ending rule?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #2

      You may want to consider using a QDataStream to send your data, if possible. I'm not sure that sticking raw data into a QString is the best idea.

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        QString::number() and QString::toInt() could be of use here.
        Another option is a QDataStream, as suggested by mlong already. You can open it on a QBuffer that is built on top of a QByteArray.

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • D Offline
          D Offline
          david.luggen
          wrote on last edited by
          #4

          Obviously I need a QDataStream. I tried to play a bit around with QDataStream and QBuffer, but I wasn't able to set this up. It is the first time I use these classes and I haven't found any useful examples. If somebody could give me a short example how I can write a QByteArray with a QDataStream into a QBuffer and read this out again, that would be great!

          1 Reply Last reply
          0
          • T Offline
            T Offline
            tobias.hunger
            wrote on last edited by
            #5

            I would not use QDataStream as that does add marshalling information into its output stream which is most likely not expected on the other side... provided you do not use a QDataStream there, too.

            You are aware that you can use QIODevice::readAll() does put all the bytes available into the QByteArray? Check the count() of the byte array: I bet it says more than 0 in your case. You just can not interpret the first bytes as a string as you did not put a string there but an integer.

            @qint32 = reinterpret_cast<qint32>(data.constData());@

            should get the 32bit integer out of the first 4 bytes of the QByteArray data though. Make sure to not go past the end of the QByteArray;-)

            PS: When moving data between devices you should use qu?int(8|16|32|64) as data types instead of a simple int. Int is defined as >=32 bit, and thus might differ between your sender and your receiver.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              david.luggen
              wrote on last edited by
              #6

              You're right. The data is in the QByteArray. QDebug is just not able to show what is in there but the size is obviously 4. These 4 Bytes consist of an STX (0x02) Byte then 2 Byte for the uint16 and finally the ETX (0x03) Byte. So first I have to check the QByteArray if it was transmittet correctly with the STX-ETX Chain and extract the 2 Bytes in the mid. Finally I get a QByteArray with my 2 Bytes.

              I put them together in an traditional way like this:

              @uint16 myValue = valueArray[1] | (valueArray[0] << 8);@

              This works so far altough I get 2 warnings I don't understand for this and a similar tag:

              @while(valueArray[0] != 0x02) valueArray.remove(0,1);@

              bq. warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:

              If I try to convert the 2 Bytes with your method like this:

              @quint16 = reinterpret_cast<quint16>(valueArray.constData());@

              I get this error:

              bq. error: cast from 'const char*' to 'quint16' loses precision

              But anyway, thx for the help until here!

              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