Serial Communication > String to Array of Floats
Posted: Thu Dec 17, 2015 11:14 am
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.
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.