Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Trying to get usable information from QByteArray data
Forum Updated to NodeBB v4.3 + New Features

Trying to get usable information from QByteArray data

Scheduled Pinned Locked Moved Solved C++ Gurus
10 Posts 5 Posters 7.4k 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.
  • C Offline
    C Offline
    CynicalPenguin
    wrote on last edited by CynicalPenguin
    #1

    So I have a terminal that prints out whatever my MCU is telling it to through serial communication, but I can't actually get that data and use it for anything.

    I do

    QSerialPort* serial; //assume this is initialized and opened already
    QByteArray data = serial->readAll();
    terminal->putData(data); //this makes the terminal read it out to me
    
    

    I'm trying to take this data and use it to do something with it, but I can't because it is in QByteArray format, or bytes.
    I have each value separated by a comma, but I can't parse it because for some reason when I initialize it as a QString, it's actually a bunch of QStrings or something.

    See:

    QString newData = QString(data); //data from before
    qDebug() << newData;
    

    This gives me the string but every character (sometimes every other character) is on a new line?
    Output on terminal (What my debug should say):
    Output

    Output at debug console:
    alt text

    Shouldn't the string be on one line? When I go to parse this I can't because it's like every character is a string or something.

    Quick update: I cheated by using the QPlainTextEdit class's function called "toPlainText()" which simply does exactly what I want. I still think QString probably has some function that does this because the fix that I found isn't very versatile. So my problem isn't solved, but I've found a bandaid for it.

    jsulmJ J.HilkJ JonBJ 4 Replies Last reply
    0
    • 6thC6 Offline
      6thC6 Offline
      6thC
      wrote on last edited by 6thC
      #2

      [edit]http://doc.qt.io/qt-5/qstring.html#QString-8 and [/edit]
      https://wiki.qt.io/Using_QByteArray may help

      1 Reply Last reply
      2
      • C Offline
        C Offline
        CynicalPenguin
        wrote on last edited by
        #3

        I've been looking at those pages and others for hours and I understand it, but it doesn't make sense in practice. I've already converted the QByteArray to a QString, but it's in a weird format that I can't find any information on anywhere.

        1 Reply Last reply
        0
        • 6thC6 Offline
          6thC6 Offline
          6thC
          wrote on last edited by 6thC
          #4

          https://en.wikipedia.org/wiki/Null-terminated_string
          Just me eyeballing that - '\0' looks like you have multiple c style strings going on in that stream/buffer.
          [edit] looks like it's just \n's nevermind me.

          Also \n
          https://en.wikipedia.org/wiki/Escape_sequences_in_C
          also "For example, \n is an escape sequence that denotes a newline character."

          1 Reply Last reply
          0
          • C CynicalPenguin

            So I have a terminal that prints out whatever my MCU is telling it to through serial communication, but I can't actually get that data and use it for anything.

            I do

            QSerialPort* serial; //assume this is initialized and opened already
            QByteArray data = serial->readAll();
            terminal->putData(data); //this makes the terminal read it out to me
            
            

            I'm trying to take this data and use it to do something with it, but I can't because it is in QByteArray format, or bytes.
            I have each value separated by a comma, but I can't parse it because for some reason when I initialize it as a QString, it's actually a bunch of QStrings or something.

            See:

            QString newData = QString(data); //data from before
            qDebug() << newData;
            

            This gives me the string but every character (sometimes every other character) is on a new line?
            Output on terminal (What my debug should say):
            Output

            Output at debug console:
            alt text

            Shouldn't the string be on one line? When I go to parse this I can't because it's like every character is a string or something.

            Quick update: I cheated by using the QPlainTextEdit class's function called "toPlainText()" which simply does exactly what I want. I still think QString probably has some function that does this because the fix that I found isn't very versatile. So my problem isn't solved, but I've found a bandaid for it.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @CynicalPenguin What does

            qDebug() << data;
            

            output?

            See here: http://doc.qt.io/qt-5/qstring.html#QString-8
            "Constructs a string initialized with the byte array ba. The given byte array is converted to Unicode using fromUtf8()".

            If the data you get is Latin1 you should use http://doc.qt.io/qt-5/qstring.html#fromLatin1-1:

            QString newData = QString::fromLatin1(data);
            

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • C CynicalPenguin

              So I have a terminal that prints out whatever my MCU is telling it to through serial communication, but I can't actually get that data and use it for anything.

              I do

              QSerialPort* serial; //assume this is initialized and opened already
              QByteArray data = serial->readAll();
              terminal->putData(data); //this makes the terminal read it out to me
              
              

              I'm trying to take this data and use it to do something with it, but I can't because it is in QByteArray format, or bytes.
              I have each value separated by a comma, but I can't parse it because for some reason when I initialize it as a QString, it's actually a bunch of QStrings or something.

              See:

              QString newData = QString(data); //data from before
              qDebug() << newData;
              

              This gives me the string but every character (sometimes every other character) is on a new line?
              Output on terminal (What my debug should say):
              Output

              Output at debug console:
              alt text

              Shouldn't the string be on one line? When I go to parse this I can't because it's like every character is a string or something.

              Quick update: I cheated by using the QPlainTextEdit class's function called "toPlainText()" which simply does exactly what I want. I still think QString probably has some function that does this because the fix that I found isn't very versatile. So my problem isn't solved, but I've found a bandaid for it.

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @CynicalPenguin
              if you're having trouble with QByteArray, you can easily convert it into an char array and work with that:

              QByteArray ba(someData);
              char *data = ba.data();
              while (*data) {
                  cout << "[" << *data << "]" << endl;
                  ++data;
              }
              

              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              0
              • C CynicalPenguin

                So I have a terminal that prints out whatever my MCU is telling it to through serial communication, but I can't actually get that data and use it for anything.

                I do

                QSerialPort* serial; //assume this is initialized and opened already
                QByteArray data = serial->readAll();
                terminal->putData(data); //this makes the terminal read it out to me
                
                

                I'm trying to take this data and use it to do something with it, but I can't because it is in QByteArray format, or bytes.
                I have each value separated by a comma, but I can't parse it because for some reason when I initialize it as a QString, it's actually a bunch of QStrings or something.

                See:

                QString newData = QString(data); //data from before
                qDebug() << newData;
                

                This gives me the string but every character (sometimes every other character) is on a new line?
                Output on terminal (What my debug should say):
                Output

                Output at debug console:
                alt text

                Shouldn't the string be on one line? When I go to parse this I can't because it's like every character is a string or something.

                Quick update: I cheated by using the QPlainTextEdit class's function called "toPlainText()" which simply does exactly what I want. I still think QString probably has some function that does this because the fix that I found isn't very versatile. So my problem isn't solved, but I've found a bandaid for it.

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by JonB
                #7

                @CynicalPenguin
                The first thing you should do is look at what the raw data actually is byte-by-byte in the QByteArray, before you try to convert it to a string. For example, if perchance it has a byte of value \00 in every other byte, it's likely UTF-16 rather than UTF-8 ("This gives me the string but every character (sometimes every other character) is on a new line") ? ... Once you know for sure what's in there you'll know what you need to do with it to get it into a QString.

                1 Reply Last reply
                0
                • C CynicalPenguin

                  So I have a terminal that prints out whatever my MCU is telling it to through serial communication, but I can't actually get that data and use it for anything.

                  I do

                  QSerialPort* serial; //assume this is initialized and opened already
                  QByteArray data = serial->readAll();
                  terminal->putData(data); //this makes the terminal read it out to me
                  
                  

                  I'm trying to take this data and use it to do something with it, but I can't because it is in QByteArray format, or bytes.
                  I have each value separated by a comma, but I can't parse it because for some reason when I initialize it as a QString, it's actually a bunch of QStrings or something.

                  See:

                  QString newData = QString(data); //data from before
                  qDebug() << newData;
                  

                  This gives me the string but every character (sometimes every other character) is on a new line?
                  Output on terminal (What my debug should say):
                  Output

                  Output at debug console:
                  alt text

                  Shouldn't the string be on one line? When I go to parse this I can't because it's like every character is a string or something.

                  Quick update: I cheated by using the QPlainTextEdit class's function called "toPlainText()" which simply does exactly what I want. I still think QString probably has some function that does this because the fix that I found isn't very versatile. So my problem isn't solved, but I've found a bandaid for it.

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by J.Hilk
                  #8

                  @CynicalPenguin said in Trying to get usable information from QByteArray data:

                  alt text

                  => 99,90 \n
                  => 20,109,24,99,90
                  => 20,109,24,99,90
                  this actually looks like you're sending the data from the other end in a loop, and don't wait for everything to arrive

                  You seem to terminate your data with a '\n' -char,
                  so try the following:

                  if(serial->canReadLine()){
                     QByteArray data = serial->readLine();
                      QString myData = QString(data);
                  }
                  

                  Edit: fixed the example, the ';' made a lot of it useless.


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  C 1 Reply Last reply
                  2
                  • C Offline
                    C Offline
                    CynicalPenguin
                    wrote on last edited by CynicalPenguin
                    #9

                    Hi all. I thought the thread died so I didn't come back to it. I've solved (sorta) the problem with this code:

                        serialData = serial->read(serial->bytesAvailable()); //QByteArray defined in header
                        serialBuffer += serialData; //QString defined in header
                        if(serialBuffer.endsWith("\n"))
                        {
                            qDebug() << serial->bytesAvailable();
                            QStringList serialList = serialBuffer.split(",", QString::SkipEmptyParts);
                            gsTerminal->putData(serialBuffer.toLocal8Bit()); //gsTerminal is a class derived from QPlainTextEdit
                                                                             //all putData(const QByteArray &data) does is inserts the data into the terminal
                            qDebug() << serialList;
                            serialBuffer.clear();
                        }
                    

                    I did this two different ways and found this one was probably the most efficient out of the two I was trying. What was happening was for some reason, using the same data, when I did this:

                    serialData = serial->readAll();
                    gsTerminal->putData(serialData);
                    

                    This added the data to my QPlainTextEdit class with no problem. BUT when I did this in the same line using the SAME data

                    QString s = QString(serialData);
                    qDebug() << s;
                    

                    So I found out what was happening was the bytes were coming a bit inconsistently. More specifically, the putData added all of the data in that order and formatted all of the data to look like it was preformatted, whereas qDebug() would take whatever was available and put it out to its own line. This is what confused me and threw me off. I'm going to try some of the things mentioned in the responses and report back to see if they work (because they're almost 100% more efficient than what I'm doing I believe).

                    1 Reply Last reply
                    0
                    • J.HilkJ J.Hilk

                      @CynicalPenguin said in Trying to get usable information from QByteArray data:

                      alt text

                      => 99,90 \n
                      => 20,109,24,99,90
                      => 20,109,24,99,90
                      this actually looks like you're sending the data from the other end in a loop, and don't wait for everything to arrive

                      You seem to terminate your data with a '\n' -char,
                      so try the following:

                      if(serial->canReadLine()){
                         QByteArray data = serial->readLine();
                          QString myData = QString(data);
                      }
                      

                      Edit: fixed the example, the ';' made a lot of it useless.

                      C Offline
                      C Offline
                      CynicalPenguin
                      wrote on last edited by
                      #10

                      @J.Hilk said in Trying to get usable information from QByteArray data:

                      @CynicalPenguin said in Trying to get usable information from QByteArray data:

                      alt text

                      => 99,90 \n
                      => 20,109,24,99,90
                      => 20,109,24,99,90
                      this actually looks like you're sending the data from the other end in a loop, and don't wait for everything to arrive

                      You seem to terminate your data with a '\n' -char,
                      so try the following:

                      if(serial->canReadLine());{
                         QByteArray data = serial->readLine();
                          QString myData = QString(data);
                      }
                      

                      This actually worked perfectly... I was trying to work out a fix for hours and eventually found one, but this is MUCH better than mine. Thanks

                      1 Reply Last reply
                      1

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved