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. How to display received float values via rs232.

How to display received float values via rs232.

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 5 Posters 3.7k Views 3 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.
  • ? A Former User

    There are multiple ways to do it, e.g:

    const float f = 23.f;
    const QString s1 = QString::number(f); 
    const QString s2 = QString("%1").arg(f);
    
    AnilReddyA Offline
    AnilReddyA Offline
    AnilReddy
    wrote on last edited by
    #5

    @Wieland
    thank you very much for the reply . My project is to display the float values which we will get from FPGA.
    we will receive 6 float values, each consisting of 4 bytes. These bytes are equal to the representation of the float values in the memory. In detail: If you define a float variable in your program, then this variable will occupy 32 bit (4 byte) in the main memory. You will have to create a variable for each engine (or an array) and write the received bytes into the memory of these variables. How the memory representation of the float values looks like, For example a float value of 1 is represented with the 32 bit word 0x3f800000.

    This the c++ code i have implemented , but i am getting errors . please kindly help me to change this logic .

    void SerialConnect::read() {

    const float a = 0x3f800000;
    const float b = 0x40000000;
    const float c = 0x40400000;
    const float d = 0x40800000;
    const float e = 0x40A00000;
    const float f = 0x40C00000;

    newSerialDataString = serial->readAll();
    QString data=newSerialDataString.trimmed();
    
    if (data == a) {
        serialDataString.append("0x3f800000");
    }
    else if (data == b)
    {
        serialDataString.append("0x40000000");
    }
    else if (data == c)
    {
        serialDataString.append("0x40400000");
    }
    else if (data == d)
    {
        serialDataString.append("0x40800000");
    }
    else if (data == e)
    {
        serialDataString.append("0x40A00000");
    }
    else if (data == f)
    {
        serialDataString.append("0x40C00000");
    }
    serialDataChanged();
    

    }

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #6

      @AnilReddy said:

      but i am getting errors

      Hi
      You should list those errors :)

      maybe insert qDebug() << "reading:" << data;

      so you can see what data comes in.

      AnilReddyA 1 Reply Last reply
      0
      • mrjjM mrjj

        @AnilReddy said:

        but i am getting errors

        Hi
        You should list those errors :)

        maybe insert qDebug() << "reading:" << data;

        so you can see what data comes in.

        AnilReddyA Offline
        AnilReddyA Offline
        AnilReddy
        wrote on last edited by
        #7

        @mrjj

        this what occurring for all if statements.
        no match for operation == (operand types are QString and const float)

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #8

          you are doing a lot of invalid stuff

          what do you receive in serial->readAll()?
          is it 24 bytes (6 floats) or 4 bytes (1 float at a time)?
          does it have headers in front of it?
          could you show an example of message?

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          AnilReddyA 2 Replies Last reply
          2
          • VRoninV VRonin

            you are doing a lot of invalid stuff

            what do you receive in serial->readAll()?
            is it 24 bytes (6 floats) or 4 bytes (1 float at a time)?
            does it have headers in front of it?
            could you show an example of message?

            AnilReddyA Offline
            AnilReddyA Offline
            AnilReddy
            wrote on last edited by
            #9

            @VRonin

            4 bytes for 1 float value at a time.
            Actually i am modifying the program which can receive the one byte at a time. I am unable to understand how to change to my requirement now

            1 Reply Last reply
            0
            • VRoninV VRonin

              you are doing a lot of invalid stuff

              what do you receive in serial->readAll()?
              is it 24 bytes (6 floats) or 4 bytes (1 float at a time)?
              does it have headers in front of it?
              could you show an example of message?

              AnilReddyA Offline
              AnilReddyA Offline
              AnilReddy
              wrote on last edited by
              #10

              @VRonin
              and these are the float points i will get . each one comes separately . it means one value at a time.
              but i do not know how define in header file. could you please tell me how to mention.
              0x40000000
              0x40400000
              0x40800000
              0x40A00000
              0x40C00000

              public:
              explicit SerialConnect(QObject *parent = 0);

              Q_PROPERTY(QString newSerialData MEMBER newSerialDataString NOTIFY serialDataChanged);
              Q_PROPERTY(QString serialData MEMBER serialDataString NOTIFY serialDataChanged);
              Q_PROPERTY(QStringList availablePorts MEMBER availablePortsList NOTIFY availablePortsChanged)
              

              private:
              QSerialPort *serial;
              QString serialDataString;
              QString newSerialDataString;
              QString a;
              QStringList availablePortsList;
              QBasicTimer timer;
              void connectSerialPort();

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kuzulis
                Qt Champions 2020
                wrote on last edited by kuzulis
                #11
                1. Do not use the "string" representation of a float in your code (as for your signal) as it is overhead, just use:
                class Foo : public QObject
                {
                ...
                signals:
                    void valueChanged(float value);
                }
                
                1. Your received code can be looks like:
                void Foo::onReadyRead()
                {
                    while (serial->bytesAvailable() >= sizeof(float)) {
                        QDataStream in(serial);
                        // setup another QDataStream' properties, as endianless, float precision and etc.
                        float value = 0.0;
                        in >> value;
                        emit valueChanged(value);
                    }
                }
                

                Of course, you should know the start/stop bytes of your data sequence (your float value)... usually for this purposes are used protocols, where, e.g. with the:

                <start_byte><data_length><data><crc><stop_byte>

                frames format, or something else...

                Otherwise, if you start to read data from the FPGA (if your FPGA sends data continuously) in a spontaneous timepoint, then you can miss from the beginning for float... and to read 4 bytes of garbage and so on.

                PS: A simple way, it is when your FPGA sends the float values in the "text" form with the end of each string, e.g.: "123.456\n".. in this case it is simple to parse it.. But this "text" form spent bigger traffic and additional code in FPGA :)

                AnilReddyA 1 Reply Last reply
                4
                • K kuzulis
                  1. Do not use the "string" representation of a float in your code (as for your signal) as it is overhead, just use:
                  class Foo : public QObject
                  {
                  ...
                  signals:
                      void valueChanged(float value);
                  }
                  
                  1. Your received code can be looks like:
                  void Foo::onReadyRead()
                  {
                      while (serial->bytesAvailable() >= sizeof(float)) {
                          QDataStream in(serial);
                          // setup another QDataStream' properties, as endianless, float precision and etc.
                          float value = 0.0;
                          in >> value;
                          emit valueChanged(value);
                      }
                  }
                  

                  Of course, you should know the start/stop bytes of your data sequence (your float value)... usually for this purposes are used protocols, where, e.g. with the:

                  <start_byte><data_length><data><crc><stop_byte>

                  frames format, or something else...

                  Otherwise, if you start to read data from the FPGA (if your FPGA sends data continuously) in a spontaneous timepoint, then you can miss from the beginning for float... and to read 4 bytes of garbage and so on.

                  PS: A simple way, it is when your FPGA sends the float values in the "text" form with the end of each string, e.g.: "123.456\n".. in this case it is simple to parse it.. But this "text" form spent bigger traffic and additional code in FPGA :)

                  AnilReddyA Offline
                  AnilReddyA Offline
                  AnilReddy
                  wrote on last edited by
                  #12
                  This post is deleted!
                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    kuzulis
                    Qt Champions 2020
                    wrote on last edited by kuzulis
                    #13

                    I have already told everything that I wanted to tell. I don't understand, what you don't understand.

                    may i know is this the right way please kindly check it.

                    No no no, please check it yourself. I do not want to check your "monkey's-code".

                    AnilReddyA 1 Reply Last reply
                    0
                    • K kuzulis

                      I have already told everything that I wanted to tell. I don't understand, what you don't understand.

                      may i know is this the right way please kindly check it.

                      No no no, please check it yourself. I do not want to check your "monkey's-code".

                      AnilReddyA Offline
                      AnilReddyA Offline
                      AnilReddy
                      wrote on last edited by
                      #14

                      @kuzulis
                      I really appreciate for help . you have mentioned a lot for me to understand . As a fresher i am not good at programming . But this not polite to say like that.

                      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