0

I've been trying to get a live updating chart to work with WPF using livecharts, my goal is to have a chart update as it reads a serial input from an Arduino that just gives some numbers.

Using this example: https://lvcharts.net/App/examples/v1/wpf/Constant%20Changes

Although the example includes a built in randomizer of numbers, I want to switch that out for the Arduino serial input.

I read the serial input like this:

private void Button_Serial_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(SerialThread);

            thread.Start();
        }

        static SerialPort _serialPort;

        private void SerialThread() //serial thread starts here
        {
            _serialPort = new SerialPort();
            _serialPort.PortName = "COM3";//Set your board COM
            _serialPort.BaudRate = 9600;


            try
            {
                _serialPort.Open();
            }
            catch (Exception e)
            {
                Console.WriteLine("could not connect to serial");
            }

            while (true)
            {
                string serialMSG = _serialPort.ReadExisting();
                Console.WriteLine(serialMSG);
                Thread.Sleep(200);
            }
        }

My problem is that I don't know what code to switch out for it to read the serial instead of the built in randomizer the example uses. The example has no usable comments or explanation of how it works and my inexperience with coding makes me unable to understand it fully.

I've looked at similar issues, but most just say to read through livechart examples. Well I did, but I do not understand it enough still. Any assistance is appreciated.

AEDNordic
  • 11
  • 1
  • Does this answer your question? [What is the correct way to read a serial port using .NET framework?](https://stackoverflow.com/questions/13754694/what-is-the-correct-way-to-read-a-serial-port-using-net-framework) – styx Mar 04 '20 at 09:19
  • I don't think my problem is reading the port, I can do that. I can see the numbers coming in on the console, I just need those numbers on the chart instead of the current numbers the example uses. Maybe I misunderstood what you linked to explains but it doesn't seem to help me. – AEDNordic Mar 04 '20 at 09:32
  • so what **is** the problem? you can just replace the random number with the number you get from serial port – styx Mar 04 '20 at 09:36
  • That is exactly what I want to do, but I don't know how. I don't quite understand the code or how how the number is generated and used so I don't know what to replace. – AEDNordic Mar 04 '20 at 09:39

1 Answers1

0

Instead of while(true), you should let c# you decided when data

    class Program
    {
        private static SerialPort port = new SerialPort("COM3",
                     9600, Parity.None, 8, StopBits.One);

        private static ChartValues<MeasureModel> _chartValues;

        static void Main(string[] args)
        {

            SerialPortWorker();       

            Console.Read();
        }

        private static void SerialPortWorker()
        {
            port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived); //called when the data waiting in the buffer

            port.Open(); //// Begin communications 

            Console.ReadLine(); // keep console thread alive
        }

        private static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if(port.BytesToWrite > 0)
            {
                var data = port.ReadExisting(); //read incoming data

                _chartValues.Add(new MeasureModel
                {
                    DateTime = DateTime.Now,
                    Value = data
                });

                SetAxisLimits(now);

                //lets only use the last 150 values
                if (ChartValues.Count > 150) ChartValues.RemoveAt(0);
            }
        }


    }




  class MeasureModel
    {
        public DateTime DateTime { get; set; }
        public String Value { get; set; }
    }
styx
  • 1,852
  • 1
  • 11
  • 22
  • I'm not exactly sure where to put the code you gave me, would this go in the mainwindow.xaml.cs (code behind) or a new class? – AEDNordic Mar 04 '20 at 11:17
  • @AEDNordic that's depends on your design but should be in a new class – styx Mar 04 '20 at 11:40
  • I'm trying my best using the code you gave me, and creating a new class, but I just can't seem to get it to work. it complains about missing stuff etc. I just don't get it. – AEDNordic Mar 04 '20 at 12:48