Wrapper de teclado para control IR con arduino y event handlers de c#


Código arduino:


#include <IRremote.h>

int RECV_PIN = 11;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
}

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume(); // Receive the next value
  }
}



Código C# (DLL):



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;

namespace IRemote
{
    public class IRemoteRec
    {
        static SerialPort port;
        String str_recibido = "";

        public event EventHandler IRreceived;

        public void Inicializar(String puerto, int baudios, Parity paridad, int databits, StopBits stopbits)
        {
            port = new System.IO.Ports.SerialPort(puerto, baudios, paridad, databits, stopbits);
            port.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(port_DataReceived);
            port.Open();
        }

        void port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            int bytesToRead = port.BytesToRead;
            char[] z = new char[bytesToRead];
            port.Read(z, 0, bytesToRead);

            foreach (char s in z)
            {
                if (s == '\n')
                {
                    // Console.WriteLine(str_recibido);

                    //quito el \r
                    this.IRreceived(str_recibido.Substring(0, str_recibido.Length - 1), new EventArgs());

                    str_recibido = "";
                }
                else
                    str_recibido += s;
            }
        }
    }
}



Código C# (Uso de DLL):



using System;
using System.Windows.Forms;
using System.IO.Ports;
using IRemote;

namespace MKOtimer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            IRemoteRec ir = new IRemoteRec();

            ir.Inicializar("COM3", 9600, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
            ir.IRreceived += new EventHandler(IR_EventoRecibido);
        }

        void IR_EventoRecibido(object sender, EventArgs e)
        {
            //the CarOwner property has been modified

            if (sender.ToString() == "FFA25D")
                Ejecutar();
           

        }

        void Ejecutar()
        {
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.EnableRaisingEvents = false;
            proc.StartInfo.FileName = "calc";
            proc.Start();
        }

    }
}

No hay comentarios:

Publicar un comentario