Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

timer event handling in C# scpi equipment communication

Status
Not open for further replies.

yefj

Advanced Member level 5
Advanced Member level 5
Joined
Sep 12, 2019
Messages
1,505
Helped
1
Reputation
2
Reaction score
5
Trophy points
38
Activity points
9,114
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.
why in timer we have to use static methods?
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)
        {

        }
    }
}
 
Last edited:

why in timer we have to use static methods?
You don't. There are many ways to implement a timer. You can e.g. use System.Windows.Forms.Timer inside your form. Or read Environment.TickCount to measure time intervals.
 

Hi,

do you want to run this on a microcontroller? (you posted in the microcontroller section)
Or on a PC under Windows?

Klaus
 

Hello Klauss,you helped me a lot about a year and a half ago with a lot of patience my steps in I2C SPI UART :)
currently my goal is to communicate with my spectrum analyzer via C# visual studio with timer events(related subject)
Is it the wrong forum section?
Where its prefereble to post my question?
Thanks
 

Hello, I am using Visual studio .Net windows forms to program my C# SPCI communication with my device.
I have succsessded in using timer event handling as shown in the results bellow.
Why there is not "public static void main" in this type of programming?
"public static void main" is the basis of every C based program?


1660226856057.png


1660226648719.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");
        }
    }
}
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top