PDA

View Full Version : Reading Bytes from Serial



muykim
19th December 2017, 02:32
Is there a way to read a certain amount of bytes from the serial and type cast it into int or float? I've read through the QByteArray document, but I don't really get it.

high_flyer
19th December 2017, 14:06
Why not simply use QByteArray::toFloat() or toInt()?


I've read through the QByteArray document, but I don't really get it.
What exactly didn't you get?

muykim
20th December 2017, 03:08
I've tried using toFloat(), but kept on getting 0

d_stranz
20th December 2017, 03:53
You're going to have to show some code if you want help in finding what's wrong.

muykim
20th December 2017, 05:20
i.e.

QByteArray a("425AE78F");
float serial = a.toFloat();
qDebug() << serial;

I'm getting 0 instead of 54.72613. I don't think I'm doing it correctly.

Lesiok
20th December 2017, 07:21
This is example from QByteArray::toDouble doc :
QByteArray string("1234.56");
double a = string.toDouble(); // a == 1234.56Do you see the difference ?

muykim
20th December 2017, 07:46
Are you saying that the string a is not a float that's why it fails to convert?

Lesiok
20th December 2017, 07:58
Of course. The number of float type and its text representation is two different things. You must go back to the basics of C ++.

muykim
20th December 2017, 08:55
As you can tell, I'm still learning c++. If you don't mind, could you give me an example of how does the float conversion works?

"4h\xB2\xC9\xBC'\xB2\xC9<\xBC\xF9K;\xD1\xEB\x7F?"

The above is a chunk of my code, and I'm trying to convert it to float, but I couldn't get it to work. I really appreciate your help.

Lesiok
20th December 2017, 09:19
How is the data sent? The order of bytes is important.

muykim
20th December 2017, 09:50
Actually I'm reading the data from the serial. I have 9 values print out in arduino as double, so I want to read it 36 bytes at a time and convert it to float.

Lesiok
20th December 2017, 10:16
What does it mean "print out in arduino as double" ? You must do the opposite operation to "print out".

d_stranz
20th December 2017, 17:37
"4h\xB2\xC9\xBC'\xB2\xC9<\xBC\xF9K;\xD1\xEB\x7 F?"

Are these the actual bytes you get from the arduino, or have you converted them to a string? This could almost be an array of hexadecimal numbers except for all of the "punctuation / non-hex" characters in it.


QByteArray a("425AE78F");

And this is a hexadecimal number, represented as a string literal. You can't use the toDouble() or toFloat() conversions on such as string, because it doesn't represent the string form of a floating point number (ie. "1234.567").

You can convert this to a long or a long long using something like this:



QByteArray a("425AE78F");
long b = a.toLong( 0, 16 );

muykim
21st December 2017, 02:24
[QUOTE=d_stranz;301620]Are these the actual bytes you get from the arduino, or have you converted them to a string? This could almost be an array of hexadecimal numbers except for all of the "punctuation / non-hex" characters in it.

Yes, they are the actual bytes. This is kind of unrelated with the topic, but if I want to send a character 'a' to the serial, can I use serial -> write("a")?