Using an Arduino controller for interrupts

In this article, I will give an example of using an arduino controller to call interrupts in a C # program.



It's worth noting that WindowsForms has a Timer element that turns on and executes code after a certain amount of time.







I decided to implement this using the Arduino UNO microcontroller.



To do this, I programmed the controller so that after a certain period of time it sends data to the serial port, causing the program to interrupt.



I believe that this article will be of interest to those who are engaged in programming microcontrollers, since it provides an example of one of the options for using microcontrollers.



void setup(){
Serial.begin(9600);//  
}
int str=0;
int interval=0;
String text;
void loop()
{


	while(str==0)
	 str=Serial.parseInt();//  
  if (str==1)//    
  {
	 while(interval==0)
	   interval=Serial.parseInt();// 
	 text=String(interval);
	 text+="!";
	 while(str!=2){
		delay(interval);//    
		
		Serial.println(text);//    
		
		 str=Serial.parseInt();//  
	 }
	 interval=0;
  }

}


The controller also accepts commands to start (1) and stop (2) the timer. (Analog of start and stop methods for windowsforms timer).



After receiving the start command, the controller waits for a time interval after which the data will be sent to the serial port.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.IO.Ports;
using System.IO;
using System.Net;
namespace ConsoleApp33
{
    class Program
    {
        private static SerialPort ard = new SerialPort("COM13", 9600);//           .
        private static Thread stop_thread = new Thread(Stop);
        private static bool sost = true;
       [MTAThread]
        static void Main(string[] args)
        {
           
            SerialDataReceivedEventHandler handler = new SerialDataReceivedEventHandler(Serial_interrupt);//    
            ard.DataReceived += handler;
            //  
            int interval = int.Parse(Console.ReadLine());
            //  
            while (ard.IsOpen != true)
                ard.Open();
            // 
            ard.WriteLine("1");
            Thread.Sleep(10);
            ard.WriteLine(interval.ToString());
            Console.WriteLine(ard.ReadLine());
            //   
            stop_thread.IsBackground = true;
         
            stop_thread.Start();
         
            while (sost) Thread.Sleep(1);// 
        }
        public enum Comand
        {
          Start = 1,// 
         Stop = 2   // 
        }
        private static System.DateTime dateTime;
        public static void Serial_interrupt(object sender,SerialDataReceivedEventArgs e)
        {
            //     
            dateTime = DateTime.Now;
            Console.WriteLine(dateTime.ToString());
        }
        public static void Stop()
        {
            if ((int)Console.ReadKey().Key == 27)//   Esc  
            {
                ard.WriteLine(Comand.Stop.ToString());// 
                sost = false;// Main
            }
            Thread.Sleep(10);
        }
      
    }
}


A C # program opens a connection to a port and an event occurs when data arrives



ard.DataReceived += handler;


calling the Serial_interrupt () method, which prints the computer's date and time to the console.



There is also a stop_thread thread that terminates the program and sends a shutdown command to the controller when the Esc key is pressed.



After starting the program will wait for the user to enter the time interval after which the controller will send data, then, before pressing the Esc key, the program will output the date and time to the console after the specified time interval.



Thank you all for your attention.



All Articles