Serial Communication > String to Array of Floats

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
rok
Prole
Posts: 36
Joined: Wed Dec 24, 2014 9:12 am
Location: Slovenia

Serial Communication > String to Array of Floats

Post 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.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Serial Communication > String to Array of Floats

Post by s-ol »

You could probably use ffi to concert the data in a single step via a cast if I unterstand it correctly

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
rok
Prole
Posts: 36
Joined: Wed Dec 24, 2014 9:12 am
Location: Slovenia

Re: Serial Communication > String to Array of Floats

Post 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?
User avatar
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

Post 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]
User avatar
rok
Prole
Posts: 36
Joined: Wed Dec 24, 2014 9:12 am
Location: Slovenia

Re: Serial Communication > String to Array of Floats

Post 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.
User avatar
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

Post 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.
User avatar
rok
Prole
Posts: 36
Joined: Wed Dec 24, 2014 9:12 am
Location: Slovenia

Re: Serial Communication > String to Array of Floats

Post by rok »

Damn, how that I didn't try this before :o:

Thank you :ultrahappy:
Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests