menu

Back To Home

Saturday, December 21, 2019

C# Write serial data to Arduino uno R3



Arduino program:

int data;
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(13,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
if(Serial.available()){
  data=Serial.read();
  if(data=='A'){
    digitalWrite(13,HIGH);
  }
  else {
    digitalWrite(13,LOW);
  }
}
}

C# program:

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;

namespace C_write_data
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            serialPort1.Open();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            serialPort1.Write("A");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            serialPort1.Write("J");
        }
    }
}


C# GUI:


Video:

No comments:

Post a Comment