Home>

Visua Studio 2019 in C # form application
PC and range sensor are connected.
I want to make a program that acquires and displays data once every 1000 milliseconds by Timer.

I tried to make it referring to the following URL, but it did not work.
https://takachan.hatenablog.com/entry/2017/09/09/225342

I have posted the code for the button click part of Form1.cs.

I would like to know if anyone can find a solution.

Error message No
error has occurred. However, the acquired value is not displayed in the text box. Nothing is displayed.
In the state before trying the Timer, the acquired values ​​could be displayed one after another.
Applicable source code
private void Button8_Click (object sender, EventArgs e) // Start button of range sensor
        {
            const int GET_NUM = 1520;
            const int start_step = 0;
            const int end_step = 760;
            // Timer interval (milliseconds)
            var timer = new System.Timers.Timer (1000);
            timer.Elapsed + = (sender2, e2) =>
            {
                try
                {
                    timer.Stop ();// or timer.Enabled = false;

                    // Process to get data from range sensor
                    try // process to get data from range sensor
                    {
                        string port_name;
                        int baudrate;
                        port_name = "COM4";
                        baudrate = 115200;

                        SerialPort urg = new SerialPort (port_name, baudrate);
                        urg.NewLine = "\ n \ n";
                        urg.Open ();
                        urg.Write (SCIP_Writer.SCIP2 ());
                        urg.ReadLine ();// ignore echo back
                        urg.Write (SCIP_Writer.MD (start_step, end_step));
                        urg.ReadLine ();// ignore echo back
                        List<long>distances = new List<long>();
                        long time_stamp = 0;
                        for (int i = 0;i<GET_NUM;++ i)
                        {
                            string receive_data = urg.ReadLine ();
                            if (! SCIP_Reader.MD (receive_data, ref time_stamp, ref distances))
                            {
                                textBox1.AppendText (receive_data);
                                break;
                            }
                            if (distances.Count == 0)
                            {
                                textBox1.AppendText (receive_data);
                                continue;
                            }
                            // show distance data
                            textBox1.AppendText ("distance [100]:" + distances [100] .ToString ());
                        }
                        urg.Write (SCIP_Writer.QT ());// stop measurement mode
                        urg.ReadLine ();// ignore echo back
                        urg.Close ();
                    }
                    catch (Exception ex)
                    {
                        textBox1.AppendText (ex.Message);
                    }
                    finally
                    {
                        textBox1.AppendText ("Press any key.");
                    }
                }
                finally
                {
                    timer.Start ();// or timer.Enabled = true;
                }
            };
            // start timer
            timer.Start ();
            // Stop the timer
            timer.Stop ();
            // release resources
            using (timer) {}

        }