Go Down

Topic: C# serial data communication with arduino (Read 182 times) previous topic - next topic


i am trying to create simple weather win app that reads data from Arduino nano connected to DHT11. I am using (or trying to use to be specific) serial communication for this. I managed to open to and close (connect and disconnect) port connected to an Arduino, but it seems that SerialDataReceivedEventHandler is newer "used". I know it's well known problem, I tried to use many sollutions from internet but none of them worked for me

This is my Arduino code

Code: [Select]

#include "DHT.h"

#define DHTPIN 2   

#define DHTTYPE DHT11   


void setup() {
  Serial.println("DHTxx test!");

void loop() {

  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(t) || isnan(h)) {
    Serial.println("Failed to read from DHT");
  } else {

Here is c# code responsible for reading from serial port

Code: [Select]

    private void SerialDataReceivedEventHandler(object sender, System.IO.Ports.SerialDataReceivedEventArgs  e) // Instrukcje jakie mają się wykonac w momencieodebrania danych z portu szeregowego.

                if (parity == true)
                temperature = serialPort1.ReadLine();
                this.Invoke(new EventHandler(pole_temp_TextChanged));
                parity = !parity; // negacja zmiennej boolean
                    humidity = serialPort1.ReadExisting();
                    this.Invoke(new EventHandler(pole_humm_TextChanged));
                parity = !parity;

Code: [Select]

        private void Form1_Load(object sender, EventArgs e)
            serialPort1.DataReceived += new SerialDataReceivedEventHandler(SerialDataReceivedEventHandler);

because Arduino sends every temperature and humidity read on a new line, I use bold parity to check what data is currently in serial port.
First temperature, second humidity, third temperature etc.

and here i am trying to input temperature and humidity data to textboxe's
Code: [Select]

         private void pole_temp_TextChanged(object sender, EventArgs e)
            pole_temp.Text = temperature + " C";

        private void pole_humm_TextChanged(object sender, EventArgs e)
            pole_humm.Text = humidity + " %";

As i mentioned before, i think that serial communication is not "used'. In arduine IDE serial monitor i get proper readings. Even "C" for temperature and "%" for humidity is not shown in textboxes.
Anyd ideas why it is not working?


Jun 17, 2018, 06:36 pm Last Edit: Jun 17, 2018, 06:41 pm by ieee488

Just view and learn!

If that one is not what you want, there are other tutorials on Youtube.



for a simple terminal emulator in C# have a look at

and C# and Java programs which use an arduino+canbus shield to Monitor Canbus traffic

Go Up