How to use C# to read sensor data from Arduino or ESPx via serial connection

When you have an old Windows machine, you can convert it into an IOT gateway.

So how can we read sensor data with our windows machine? Since our windows machine probably do not have GPIO pins, we can read sensor data via an intermediary device.

For example, we can first attach an Arduino Uno or ESP32 development board as the intermediary device to our windows machine via USB. Given that connection, the intermediary device will read the sensor data and send those values via serial. On our Windows machine, we can then run a program to read those values from serial.

So how can we read sensor data from Arduino or ESPx via serial connection on our Windows machine? Since C# is a programming language for creating windows application, we can use C# to read those data from serial.

Defining the sample scenario

Since I had talked about how to use a ESP32 development board to read temperature and humidity from a DHT22 sensor, let's include that in our current scenario.

After we had uploaded the Arduino sketch to our ESP32 development board, let's connect it to our windows machine via USB.

When we do so, our ESP32 development board will periodically get the temperature and humidity values and write the values to the serial port.

Identifying the COM port to your Arduino or ESPx device on windows

Before writing any C# code, we need to identify the COM port to interact with our ESP32 device.

In order to do so, first search for device manager:

Finding device manager with windows search

When your device manager loads up, find an entry labelled as Silicon Labs CP210x USB to UART bridge under Ports (COM & LPT):

Device manager showing Silicon Labs CP210x USB to UART bridge as one of the COM ports

As shown above, my windows machine had allocated COM5 to the ESP32 development board connected to it.

If you are using Arduino Uno, you should find an entry that looks like Ardunio Uno (COMX).

Creating the C# code to read the sensor data from Arduino or ESPx via serial connection

At this point in time, we are ready to create the C# code to read the sensor data from Arduino or ESPx via serial connection.

Given that, let's create a simple console application that read from the serial connection every half a second:

using System;
using System.IO.Ports;

namespace Read_Sensor_Data_From_Arduino_or_ESPx_via_serial_port
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            try
            {
                SerialPort port = new SerialPort();
                port.BaudRate = 9600;
                port.PortName = "COM5";
                port.Open();

                try
                {
                    while (true)
                    {
                        string oneLine = port.ReadLine();
                        Console.WriteLine(oneLine);
                        System.Threading.Thread.Sleep(500);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Encountered error while reading serial port");
                    Console.WriteLine(ex.ToString());
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine("Encountered error while opening serial port");
                Console.WriteLine(ex.ToString());
            }

        }
    }
}

Understanding the C# code for reading sensor data from Arduino or ESPx via serial connection

First of all, we indicate that we wish to use some classes contained in the System and System.IO.Ports namespaces:

using System;
using System.IO.Ports;

Once we had done so, we define our own namespace and a class with a Main method.

Inside the Main method, we try to create a SerialPort instance to COM5 with a baud rate of 9600. When we had done so, we attempt to open the connection to our serial port:

SerialPort port = new SerialPort();
port.BaudRate = 9600;
port.PortName = "COM5";
port.Open();

After that, we attempt to read from the serial port periodically, one line at a time and output the readings to console:

while (true)
{
    string oneLine = port.ReadLine();
    Console.WriteLine(oneLine);
    System.Threading.Thread.Sleep(500);
}

Running the C# program to read sensor data from Arduino or ESPx via serial connection

Once you had built your C# program, you can then run the executable with command prompt. When you run your application, you should see output similar to the following:

Print screen of C# program trying to read sensor data from Arduino or ESPx via serial port

How to use C# to read sensor data from Arduino or ESPx via serial connection

About Clivant

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns techcoil.com and hopes that whatever he had written and built so far had benefited people. All views expressed belongs to him and are not representative of the company that he works/worked for.