I am trying to send float data from STM32 board to computer via UART. Im sending float data via UART as 4 frames - 4x8bits. I have already made a function that converts those frames back to float.
My question is what should I do to stack like 1000 floats into array so I can later use it - outside of Handler.
class Program
{
static void Main()
{
SerialPort com = new SerialPort("Com3");
com.BaudRate = 9600;
com.Parity = Parity.None;
com.StopBits = StopBits.One;
com.DataBits = 8;
com.Handshake = Handshake.None;
com.RtsEnable = true;
com.DataReceived += new SerialDataReceivedEventHandler(RxHandler);
com.Open();
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
com.Close();
}
private static void RxHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
int bytes = sp.BytesToRead;
byte[] buffer = new byte[bytes];
sp.Read(buffer, 0, bytes);
Console.WriteLine(ToFloat(new byte[] { buffer[0], buffer[1], buffer[2], buffer[3] }));
}
static float ToFloat(byte[] input)
{
byte[] fArray = new[] { input[0], input[1], input[2], input[3] };
return BitConverter.ToSingle(fArray, 0);
}
}