Page 1 of 1

Serial Communication > String to Array of Floats

Posted: Thu Dec 17, 2015 11:14 am
by rok
I'm using https://github.com/vsergeev/lua-periphery to read serial data received from a microcontroller which sends float data. However lua-periphery only enables me to receive data in a string format (as far as I know).

Currently I'm
1. receiving those characters
2. spliting them in strings of 4 (as floats have 4 bytes)
3. then I get their numerical ASCII values with string.byte
4. convert those to a binary string made of zeros and ones
5. concat all those bits together
6. convert them to float with the IEEE 754 algorithm

Not quite working as expected at moment.

Is there any other way I could get those characters into floats without doing that much?

By the way, I'm using löve to draw graphs of the received data and the serial reading runs on a separate thread (I'm passing the received string via a channel over to the main thread).

It works alright when I send the data as a string of numbers and parse with gsub, instead of raw bytes, but I'd like to get this working too.

Re: Serial Communication > String to Array of Floats

Posted: Thu Dec 17, 2015 1:25 pm
by s-ol
You could probably use ffi to concert the data in a single step via a cast if I unterstand it correctly

Re: Serial Communication > String to Array of Floats

Posted: Thu Dec 17, 2015 4:41 pm
by rok
Could work. I'm trying something with ffi.cast.
For example I'd like to convert "aabb" to a float.

ffi.cast("float", "aabb") does not work, ffi.cast("float*", "aabb") gives me a cdata float pointer, for which I don't know how to get the value from.

Someone knows how to do it correctly?

Re: Serial Communication > String to Array of Floats

Posted: Thu Dec 17, 2015 5:21 pm
by slime
You can treat a pointer as an array in C, so you should be able to get values like this:

Code: Select all

floatstring = "aabbccdd"

local floatpointer = ffi.cast("float *", floatstring)

local float1 = floatpointer[0] -- Arrays in C are 0-based, rather than 1-based.
local float2 = floatpointer[1]

Re: Serial Communication > String to Array of Floats

Posted: Thu Dec 17, 2015 8:00 pm
by rok
Doesn't seem to be working :cry:

I took an example from Wikipedia which should give 0.15625

00111110 00100000 00000000 00000000 or in character numbers 62 32 0 0

My own function gives me the right number, ffi.cast("float *", x) does not.

Re: Serial Communication > String to Array of Floats

Posted: Thu Dec 17, 2015 8:27 pm
by slime
Most CPU architectures (the notable exception being PowerPC, which most people don't care about anymore) are Little-Endian, which means you'd interpret 00111110 00100000 00000000 00000000 as string.char(0, 0, 32, 62) (and then cast that to a float-pointer.)

Image

If you do care about Big-Endian machines you can check which architecture the current system is with ffi.abi.

Re: Serial Communication > String to Array of Floats

Posted: Thu Dec 17, 2015 8:36 pm
by rok
Damn, how that I didn't try this before :o:

Thank you :ultrahappy: