Objective
Run Program in Arduino and receive data in Visual Studio using C# Program.
1. Open Arduino Application and type the following code.
Arduino Code (C++ Code)
int Loop = 1;
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println("Data Loop = " + String(Loop));
Loop++;
delay(500);
}
2. Open Microsoft Visual Studio
Create New Project – Console App (.NET Framework)
Use the code mentioned below:
C# Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
namespace ArduinoDataReceive
{
class Program
{
static void Main(string[] args)
{
SerialPort myport = new SerialPort();
myport.BaudRate = 9600;
myport.PortName = "COM3";
myport.Open();
while (true)
{
string data_rx = myport.ReadLine();
Console.WriteLine(data_rx);
}
}
}
}
3. Execute the C# app and receive the response in the command prompt.
Code
Applications
Using this method, we can easlily build an array of sensors on the Arduino board and integrate them with C# based applications.
In the next experiment we will create an activity in UiPath to receive data from Arduino using the above C# code.
Comments