-1

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);
    }
}
Carlos E. Ferro
  • 930
  • 10
  • 21
Jejh
  • 85
  • 2
  • 12
  • Use an instance of a collection (e.g. List) to store the received data. `List list = new List();` and in RxHandler `list.Add(ToFloat(....) );` – H.G. Sandhagen Jan 10 '17 at 19:37
  • How do I refer to "list" when in RxHandler Error "An object reference is required for the non-static field, method, or property 'Program.list' – Jejh Jan 10 '17 at 19:54
  • You need to impose somekind of protocol (i.e. message frame with integrity check) on this data link. Your current code appears to use nothing for maintaining message framing. E.G. lose a byte, and you start to convert the misaligned data into garbage float values. See http://stackoverflow.com/questions/16177947/identification-of-packets-in-a-byte-stream for one scheme – sawdust Jan 11 '17 at 02:47
  • Sorry, because you use static methods it must be defined static (inside class `Program` in front of `static Main`: static List list = new List(); – H.G. Sandhagen Jan 11 '17 at 05:02
  • If you want to write (human readable) the list to file after reading all values, put `File.WriteAllLines(filename, list.Select(p => p.ToString()));` after `com.Close();` – H.G. Sandhagen Jan 11 '17 at 05:09
  • @H.G. Sandhagen I have created list as you told me. How do I access list elements when I need i.e. use method like ToFloat in my code? – Jejh Jan 11 '17 at 15:52
  • See answer below. I added some code to your example. – H.G. Sandhagen Jan 11 '17 at 17:37

2 Answers2

0

If all you care about is saving your floats' bytes on disk after validating them as such (i.e., as System.Single values) thru your existing ToFloat method, you could use File.WriteAllBytes for instance.

If you'd like to save them as a human-readable representation, I guess you'll want to rely on Single.ToString before using, say, File.WriteAllText.

YSharp
  • 1,066
  • 9
  • 8
  • Well, generally I'm generally planning to put my floats to array, then using them in order to create plot. – Jejh Jan 10 '17 at 19:43
  • @Jejh Ok, maybe I got confused in which problem you're actually trying to solve beyond your question title, "c# saving COMport data to file" then. – YSharp Jan 10 '17 at 19:57
  • For now I want to save my data to file – Jejh Jan 10 '17 at 21:27
0

Here the code parts I added to save the floats into a file:

class Program 
{
    private static List<float> list = new List<float>();
    static void Main() {
        // see your code
        // ......
        com.Close();

        // Write list to file
        System.IO.File.WriteAllLines("data.txt", list.Select(p => p.ToString()));
        // convert list to array
        float[] floatArray = list.ToArray();
    }

    private static void RxHandler(object sender, SerialDataReceivedEventArgs e) {
        // see your code
        // ......
        sp.Read(buffer, 0, bytes);

        // store float in variable
        float f = ToFloat(new byte[] { buffer[0], buffer[1], buffer[2], buffer[3] }));
        Console.WriteLine(f);
        // add float to list
        list.Add(f);
    }
H.G. Sandhagen
  • 772
  • 6
  • 13