How to display received float values via rs232.



  • This post is deleted!

  • Moderators

    Hi! I haven't understood yet what you problem is. Do you need help to create a string from a float or don't you know which widget to use to display a string?



  • @Wieland

    yes I need a help to create a string from a float .


  • Moderators

    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);
    


  • @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();
    

    }


  • Qt Champions 2016

    @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.



  • @mrjj

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



  • 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?



  • @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



  • @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. 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 :)



  • This post is deleted!


  • 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".



  • @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.