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.
Serial Communication > String to Array of Floats
Re: Serial Communication > String to Array of Floats
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
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?
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?
- slime
- Solid Snayke
- Posts: 3166
- Joined: Mon Aug 23, 2010 6:45 am
- Location: Nova Scotia, Canada
- Contact:
Re: Serial Communication > String to Array of Floats
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
Doesn't seem to be working
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.
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.
- slime
- Solid Snayke
- Posts: 3166
- Joined: Mon Aug 23, 2010 6:45 am
- Location: Nova Scotia, Canada
- Contact:
Re: Serial Communication > String to Array of Floats
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.)
If you do care about Big-Endian machines you can check which architecture the current system is with ffi.abi.
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
Damn, how that I didn't try this before
Thank you
Thank you
Who is online
Users browsing this forum: Bing [Bot] and 10 guests