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. Help with conversions of datatype.
QtWS25 Last Chance

Help with conversions of datatype.

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 2.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.
  • I Offline
    I Offline
    iTony
    wrote on last edited by koahnig
    #1

    Hello, i'm trying to write a software which receive serial data from the USB port from an Arduino, then plot it using the custom plotwidget, i'm having issue to convert data.

    This is the code which is giving me issues

    QString datiXok;
    QString datiYok;
    QString datiZok;
     QByteArray datiX;
     QByteArray datiY;
     QByteArray datiZ;
    
    double doubleX;
    double doubleY;
    double doubleZ;
    

    Those are declared globally.

    datiX = serial->readAll();
    datiXok = QString(datiX);
    doubleX =   atof (datiX);
    
    datiY = serial->readAll();
    datiYok = QString(datiY);
    doubleY =   atof (datiY);
    
    datiZ = serial->readAll();
    datiZok = QString(datiZ);
    doubleZ =   atof (datiZ);
    

    datiX gets converted without issues, Y and Z doesn't

    if i invert the code and convert first Y or Z before X, the first conversion works the rest doesn't
    can anyone help me?
    I know this is not the best way to convert, but i tried other Qt methods to 0 avail, this way at least 1 out of 3 gets converted, but in total i need to convert 4, either Qbytearray to double or float, or Qstring to double or float.

    [edit: koahnig, code tags added]

    K 1 Reply Last reply
    0
    • I iTony

      Hello, i'm trying to write a software which receive serial data from the USB port from an Arduino, then plot it using the custom plotwidget, i'm having issue to convert data.

      This is the code which is giving me issues

      QString datiXok;
      QString datiYok;
      QString datiZok;
       QByteArray datiX;
       QByteArray datiY;
       QByteArray datiZ;
      
      double doubleX;
      double doubleY;
      double doubleZ;
      

      Those are declared globally.

      datiX = serial->readAll();
      datiXok = QString(datiX);
      doubleX =   atof (datiX);
      
      datiY = serial->readAll();
      datiYok = QString(datiY);
      doubleY =   atof (datiY);
      
      datiZ = serial->readAll();
      datiZok = QString(datiZ);
      doubleZ =   atof (datiZ);
      

      datiX gets converted without issues, Y and Z doesn't

      if i invert the code and convert first Y or Z before X, the first conversion works the rest doesn't
      can anyone help me?
      I know this is not the best way to convert, but i tried other Qt methods to 0 avail, this way at least 1 out of 3 gets converted, but in total i need to convert 4, either Qbytearray to double or float, or Qstring to double or float.

      [edit: koahnig, code tags added]

      K Offline
      K Offline
      koahnig
      wrote on last edited by koahnig
      #2

      @iTony

      Hi and welcome to devnet

      readAll reads all bytes currently in the buffer. This may one byte or more depending in which stage your buffer is at the time when the routine is called. It does not know what it is reading or what you think it should read.

      You need to chekc out what you are actually reading. E.g. with

      datiX = serial->readAll();
      qDebug() << datiX;     // requires to add #include <QDebug> at the start of the source file
      datiXok = QString(datiX);
      doubleX = atof (datiX);
      

      Possibly you are reading already all bytes at the beginning. Anyway, you need to think about how you like to handle and eventually collect the information, because it is unpredictable how much you are going receive each time.

      There are some examples for reading from QSerialPort around. They will help you to get an understanding of proper handling of serial ports with Qt.

      Vote the answer(s) that helped you to solve your issue(s)

      I 1 Reply Last reply
      1
      • K koahnig

        @iTony

        Hi and welcome to devnet

        readAll reads all bytes currently in the buffer. This may one byte or more depending in which stage your buffer is at the time when the routine is called. It does not know what it is reading or what you think it should read.

        You need to chekc out what you are actually reading. E.g. with

        datiX = serial->readAll();
        qDebug() << datiX;     // requires to add #include <QDebug> at the start of the source file
        datiXok = QString(datiX);
        doubleX = atof (datiX);
        

        Possibly you are reading already all bytes at the beginning. Anyway, you need to think about how you like to handle and eventually collect the information, because it is unpredictable how much you are going receive each time.

        There are some examples for reading from QSerialPort around. They will help you to get an understanding of proper handling of serial ports with Qt.

        I Offline
        I Offline
        iTony
        wrote on last edited by
        #3

        @koahnig thanks for the reply.
        I did check it before writing the post, the data is read correctly, just not using qDebug to visualize it, i used the text label and the lcd, the data get read correctly, is only the conversion which is always = to 0, apart from the first one.

        1 Reply Last reply
        0
        • I Offline
          I Offline
          iTony
          wrote on last edited by
          #4

          @koahnig said in Help with conversions of datatype.:

          qDebug() << datiX;

          i tried it to be sure, data is actually being read all, i don't understand tho, why it get showed correctly in the label and lcd then?

          1 Reply Last reply
          0
          • I Offline
            I Offline
            iTony
            wrote on last edited by
            #5

            I did some more testing, now i get why date was being shown correctly, the data it's ending with \r\n, that made it so it would go down on the UI, and coincidence just as i planned the layout for each value.

            How can i fix it without manipulate the string itself?
            Is there a way to read data to that precise point \r, or i'm forced to manuallyl manipulate the whole string?

            M 1 Reply Last reply
            0
            • I iTony

              I did some more testing, now i get why date was being shown correctly, the data it's ending with \r\n, that made it so it would go down on the UI, and coincidence just as i planned the layout for each value.

              How can i fix it without manipulate the string itself?
              Is there a way to read data to that precise point \r, or i'm forced to manuallyl manipulate the whole string?

              M Offline
              M Offline
              mchinand
              wrote on last edited by mchinand
              #6

              You could read the entire string into a variable and then use QString::split.
              (untested example code follows)

              QString recvdString = QString(serial->readAll());
              QStringList splitStrings = recvdString.split(QString("\r\n"));
              datiXok = splitStrings.at(0);
              doubleX = dataiXok.toDouble();
              ...
              1 Reply Last reply
              0
              • I Offline
                I Offline
                iTony
                wrote on last edited by
                #7

                Thanks a lot guys, i was able to fix it thanks to your help.

                This is the new one
                QString recvdString = QString(serial->readAll());
                QStringList splitStrings = recvdString.split(QString("\r\n"));

                datiXok = splitStrings.at(0);
                datiYok = splitStrings.at(1);
                datiZok = splitStrings.at(2);

                doubleX = datiXok.toDouble();
                doubleY = datiYok.toDouble();
                doubleZ = datiZok.toDouble();

                K 1 Reply Last reply
                0
                • I iTony

                  Thanks a lot guys, i was able to fix it thanks to your help.

                  This is the new one
                  QString recvdString = QString(serial->readAll());
                  QStringList splitStrings = recvdString.split(QString("\r\n"));

                  datiXok = splitStrings.at(0);
                  datiYok = splitStrings.at(1);
                  datiZok = splitStrings.at(2);

                  doubleX = datiXok.toDouble();
                  doubleY = datiYok.toDouble();
                  doubleZ = datiZok.toDouble();

                  K Offline
                  K Offline
                  koahnig
                  wrote on last edited by koahnig
                  #8

                  @iTony

                  Good to see that you have found a solution for your problem. Certainly the solution suggested by @mchinand seems to be the perfect solution since your parameters are separated by <CR><LF> characters. A similar solution could have been achieved by using readLine instead of readAll.

                  Anyway both solutions are somewhat dirty in the sense already mentioned above.
                  @koahnig said in Help with conversions of datatype.:

                  Possibly you are reading already all bytes at the beginning. Anyway, you need to think about how you like to handle and eventually collect the information, because it is unpredictable how much you are going receive each time.

                  There are some examples for reading from QSerialPort around. They will help you to get an understanding of proper handling of serial ports with Qt.

                  Especially when using the readyRead signal for all real-time based communications such serial, TCP, UDP or whatever you can never rely that the complete information is already in the buffer. The buffer may contain one byte to all bytes. All depends on different aspects such as CPU speed, other tasks and speed of communication link. You might even have too much data in the buffer and throwing away some of the information when using readAll. The use of readLine is therefore somewhat less dirty.

                  Vote the answer(s) that helped you to solve your issue(s)

                  1 Reply Last reply
                  2

                  • Login

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