Go Down

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

Eredin

Hello,
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   

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  Serial.println("DHTxx test!");
  dht.begin();
}

void loop() {

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

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


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
                }
                else
                {
                    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?

ieee488

#1
Jun 17, 2018, 06:36 pm Last Edit: Jun 17, 2018, 06:41 pm by ieee488
https://www.youtube.com/watch?reload=9&v=vHeG3Gt6STE


Just view and learn!


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




.

horace

for a simple terminal emulator in C# have a look at
https://github.com/horace3/VB_terminal_emulator

and C# and Java programs which use an arduino+canbus shield to Monitor Canbus traffic
https://github.com/horace3/Arduino_Canbus_Monitor

Go Up