timer event handling in C# scpi equipment communication

Thread Starter

yef smith

Joined Aug 2, 2020
717
Hello, i have sucsseeded building in visual studio .NET framework a code that sends commands when i press a button as shown bellow.
I want to modify my code so it will send this command every second and to stop sending the command when i press another button.
Google says i should use timer(and event handler), but timer needs static method.
i didn't use "public static void main()" in my code so far and it worked fine.
how to use a timer for sending scpi command every second?


Thanks.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Timers;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        bool meter_state = false;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            serialPort1.Open();
            serialPort1.Write("meas:freq:st 500 mhz\n");
            serialPort1.Write("*idn?\n");
            string data = serialPort1.ReadTo("\n");
            textBox1.AppendText(data);
            serialPort1.Close();

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
 

ApacheKid

Joined Jan 12, 2015
1,533
This is very easy to do, C# is a Rolls Royce language these days (but sadly not easily usable on MCUs).

Anyway, try something like this:

Code:
namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        bool meter_state = false;
        Timer timer1;
        public Form1()
        {
            InitializeComponent();
            timer1 = new Timer();
            timer1.Tick += ReadPort;
            timer1.Interval = 1000;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Stop();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void ReadPort(object sender, EventArgs e)
        {
            serialPort1.Open();
            serialPort1.Write("meas:freq:st 500 mhz\n");
            serialPort1.Write("*idn?\n");
            string data = serialPort1.ReadTo("\n");
            textBox1.AppendText(data);
            serialPort1.Close();

        }
    }
}
 

Thread Starter

yef smith

Joined Aug 2, 2020
717
Hello ApacheKid ,i have successed using time element as shown bellow.
Why in winforms C# visual studio there is no public static void main?
its a basic this i was taught every program needs to have.
Why in c# Forms they do use that?
Thanks.

1660499471724.png
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Timers;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        bool meter_state = false;
        double num2;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //serialPort1.Write("meas:freq:st 500 mhz\n");
            //serialPort1.Write("*idn?\n");
            //string data = serialPort1.ReadTo("\n");
          
            //serialPort1.Write("meas:ocbw on\n");
            //serialPort1.Write("meas:ocbw:chpw?\n");
            //string data = serialPort1.ReadTo("\n");
            //if ((data != null) && (data[0] !='<'))
            //{
            //    num2 = Convert.ToDouble(data);
            //    textBox1.AppendText(Convert.ToString(num2));
                
            //}

            //serialPort1.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {

            //serialPort1.Open();
        }

        private void button3_Click(object sender, EventArgs e)
        {

            serialPort1.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            serialPort1.Open();
            serialPort1.Write("meas:ocbw on\n");
            serialPort1.Write("meas:ocbw:chpw?\n");
            string data = serialPort1.ReadTo("\n");
            if ((data != null) && (data[0] !='<'))
            {
                num2 = Convert.ToDouble(data);
                textBox1.AppendText(Convert.ToString(num2));

            }
            serialPort1.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            textBox2.Clear();
            textBox2.AppendText("clock dissabled");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            textBox2.Clear();
            textBox2.AppendText("clock enabled");
        }
    }
}
 

ApacheKid

Joined Jan 12, 2015
1,533
Hello ApacheKid ,i have successed using time element as shown bellow.
Why in winforms C# visual studio there is no public static void main?
its a basic this i was taught every program needs to have.
Why in c# Forms they do use that?
Thanks.

View attachment 273831
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Timers;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        bool meter_state = false;
        double num2;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //serialPort1.Write("meas:freq:st 500 mhz\n");
            //serialPort1.Write("*idn?\n");
            //string data = serialPort1.ReadTo("\n");
         
            //serialPort1.Write("meas:ocbw on\n");
            //serialPort1.Write("meas:ocbw:chpw?\n");
            //string data = serialPort1.ReadTo("\n");
            //if ((data != null) && (data[0] !='<'))
            //{
            //    num2 = Convert.ToDouble(data);
            //    textBox1.AppendText(Convert.ToString(num2));
               
            //}

            //serialPort1.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {

            //serialPort1.Open();
        }

        private void button3_Click(object sender, EventArgs e)
        {

            serialPort1.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            serialPort1.Open();
            serialPort1.Write("meas:ocbw on\n");
            serialPort1.Write("meas:ocbw:chpw?\n");
            string data = serialPort1.ReadTo("\n");
            if ((data != null) && (data[0] !='<'))
            {
                num2 = Convert.ToDouble(data);
                textBox1.AppendText(Convert.ToString(num2));

            }
            serialPort1.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            textBox2.Clear();
            textBox2.AppendText("clock dissabled");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            textBox2.Clear();
            textBox2.AppendText("clock enabled");
        }
    }
}
A Forms app does have a static main method, look at my simple project:

1660500180044.png

Open up Program.cs

1660500222700.png

See...
 

ApacheKid

Joined Jan 12, 2015
1,533
You can now (as of the most recent VS 2022 update - 17.3.0) begin to use .Net MAUI. This is a new GUI model based on a complete rewrite of Xamarin Forms/WPF but can run on Windows, Mac, Andriod and iOS.

This might be overkill but also might be a good opportunity to try it out with a simple app like yours, the app you'd write would run on all four platforms and that has to be a win.
 

Thread Starter

yef smith

Joined Aug 2, 2020
717
Hello ApacheKid,I didnt use the program.cs in my project only form2.cs
the code bellow is my Program.cs ,There is no mentioning of form2 in the code.
In what cases i would need to play with program.cs?
Thank.

1660508374372.png

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    internal static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
 

ApacheKid

Joined Jan 12, 2015
1,533
One can add new "Form" classes at will, you may have added a form and named it Form2, the project template for a basic forms app creates a single empty form named Form1. You can rename that in the IDE if you wanted to.

A Form app is a Console app that leverages the Application class. That class contains a great deal of plumbing code like the code that manipulates the app's GUI thread or deals with visual styling or message filtering and so on.

Its not often that you'd need to alter Program.cs but its exposed just in case you wanted to.

A Forms app is very different to a WPF app, and these kinds of apps can only run on Windows that's why you must use .Net Framework for these.

If you were to use MAUI instead then you can abandon the old .Net Framework and deal with the new and better and faster .Net Core with all the advantages that provides.

WPF blows Windows Forms out of the water, its pretty old now but was a huge advance in GUI software design. MAUI is very very similar to WPF but is a rewrite really and runs on multiple platforms - the same binary will run on multiple platforms.

WPF/MAUI are resolution independent unlike Forms, a MAUI Page that takes - say - a quarter of the screen space will always take a quarter of the screen space even when you change the resolution up/down. With Forms things are sized in pixels so if you run an app on a higher res monitor it gets relatively smaller, not so WPF/MAUI.

Anyway, I digress, for simple utilities and test programs and the like Forms is absolutely fine, many developers create basic Forms apps for such stuff all the time.
 
Last edited:
Top