C# Serial Communication With Arduino

About: Green hand in arduino

Intro: C# Serial Communication With Arduino

If you don't know how to communicate from the Arduino to the PC or communicate from PC to Arduino, this post is perfect for you!!!

This tutorial is also suitable for the communication between arduino and your PC.

These days, I'm learning serial port communication and want to write a simple demo on my LattePanda. After learning for many days, I realized this function. This post is mainly about the serial port communication.

LattePanda is a Win10 single board computer, it also integrated Arduino Compatible Processor, so you can control the physical world easily. If you don't have it, you can connect the Arduino to your PC instead!

Hope it's useful for you.

Step 1: What You Need:




Just a LattePanda.(LattePanda=Arduino Leonardo+Win10 PC).

Step 2: 1.Communicate From the Arduino to the LattePanda CPU


Arduino:Send '1'

Computer: Receive '1'

(You can try to send the data of your sensor or any other parameter if you want.)

I found an easiest way to do this.

C# code:

<p>using System;<br>using System.IO.Ports;
using System.Threading;
namespace ConsoleApp1
    class Program
        static SerialPort _serialPort;
        public static void Main()
            _serialPort = new SerialPort();
            _serialPort.PortName = "COM4";//Set your board COM
            _serialPort.BaudRate = 9600;
            while (true)
                string a = _serialPort.ReadExisting();

Arduino sketch:

<p>void setup() {<br>Serial.begin(9600);
}</p><p>void loop() {

Press Start, and you will see the result like the picture above. We received the data from Arduino successfully.

Step 3: 2. Communicate From the LattePanda CPU to the Arduino


Computer: Send '1' or '0'

Arduino: Receive '1' and turn on the light. Receive '0' and turn off the light.

Just make two button to realize this function. See the picture above.

C# Code:(Winforms)

<p>using System;<br>using System.Windows.Forms;
using System.IO.Ports;
namespace lightcontrol
    public partial class Form1 : Form
        SerialPort port;
        public Form1()
            this.FormClosed += new FormClosedEventHandler(Form1_FormClosed);
            if (port==null)
                port = new SerialPort("COM7", 9600);//Set your board COM
        void Form1_FormClosed(object sender,FormClosedEventArgs e)
            if(port !=null &&port.IsOpen)
        private void button1_Click(object sender, EventArgs e)
        }</p><p>        private void button2_Click(object sender, EventArgs e)
        private void PortWrite(string message)

Arduino sketch:

<p>const int LedPin = 13;  <br>int ledState = 0;  
void setup()  
  pinMode(LedPin, OUTPUT);  
void loop()  
    char receiveVal;     
    if(Serial.available() > 0)  
        receiveVal = Serial.read();  
       if(receiveVal == '1')      
          ledState = 1;     
          ledState = 0;       
    digitalWrite(LedPin, ledState);   

If you click the button 'ON', you will see the light(pin13) turns on.

There's another way to do this, you can see the tutorial here.

Step 4: Summary

These are the simplest way to communicate between arduino and your PC.

Most of the projects need Arduino to send data to computer and use computer to analize data or do other thing. So communicate is necessary and very important.

Hope it's helpful for you. If you have any questions please let me know.



    • Fix It! Contest

      Fix It! Contest
    • Furniture Contest 2018

      Furniture Contest 2018
    • Audio Contest 2018

      Audio Contest 2018