How to display received float values via rs232.
-
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 .
-
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();
}
-
@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.
-
@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
0x40C00000public:
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(); -
- 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); }
- 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 :)
-
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".