Reading two sensors in c # from arduino

0

I am sending data from arduino to Visual Studio using C #, I am sending the data from a proximity sensor, and now I also have to send the humidity and temperature sensor data, then how to differentiate the data corresponding to said sensor.

using System; using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.IO.Ports; 
using datacenter.Properties; 
using System.Threading;

namespace datacenter { 

    public partial class datacenter : Form { 

        string dato; 

        public datacenter() {

            InitializeComponent();
            serialPort1.PortName = "COM5";
            serialPort1.BaudRate = 9600;
            serialPort1.Open();
            alarmaof.Visible = true;
            serialPort1.DataReceived += serialrecive; 

        }

        private void Form1_Load(object sender, EventArgs e)
        {

            circularProgressBar2.Value = 0;
            circularProgressBar2.Minimum = 0;
            circularProgressBar2.Maximum = 10000; 
        }
        private void serialrecive(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            string line = serialPort1.ReadLine();
            this.BeginInvoke(new LineReceivedEvent(lineRecived), line);
        }

        private delegate void LineReceivedEvent(string line);
        public void lineRecived(string line)
        {
       //procedimiento para mostrar los datos 
            circularProgressBar2.Text = line;

      //Procedimiento para calcular la distancia
            if(Convert.ToInt32(line) <1000)
            {
                alarmaof.Visible = false;
            }
            else
            {
                alarmaof.Visible = true;
            }
            // procedimiento para la barra circular 
            Thread.Sleep(5);
            circularProgressBar2.Value = Convert.ToInt32(line);
            circularProgressBar2.Update(); 
        }

    }
}
    
asked by Fernando Orozco 24.10.2018 в 00:29
source

2 answers

0
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using datacenter.Properties;
using System.Threading;

namespace datacenter
{
    public partial class datacenter : Form
    {
        string dato;
        public datacenter()
        {

            InitializeComponent();
            serialPort1.PortName = "COM5";
            serialPort1.BaudRate = 9600;
            serialPort1.Open();
            alarmaof.Visible = true;
            serialPort1.DataReceived += serialrecive; 

        }

        private void Form1_Load(object sender, EventArgs e)
        {

            circularProgressBar2.Value = 0;
            circularProgressBar2.Minimum = 0;
            circularProgressBar2.Maximum = 10000; 
        }
        private void serialrecive(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            string line = serialPort1.ReadLine();
            this.BeginInvoke(new LineReceivedEvent(lineRecived), line);
        }

        private delegate void LineReceivedEvent(string line);
        public void lineRecived(string line)
        {
       //procedimiento para mostrar los datos 
            circularProgressBar2.Text = line;

      //Procedimiento para calcular la distancia
            if(Convert.ToInt32(line) <1000)
            {
                alarmaof.Visible = false;
            }
            else
            {
                alarmaof.Visible = true;
            }
            // procedimiento para la barra circular 
            Thread.Sleep(5);
            circularProgressBar2.Value = Convert.ToInt32(line);
            circularProgressBar2.Update(); 
        }

    }
    }
    
answered by 24.10.2018 в 00:37
0

From your arduino code. When you read a data from a sensor, you know since pin is reading it, so that your C # code knows what sensor the message is, just prefixes a character before the value. ejm: H, 64 (humidity sensor)

Then in C # you separate using a split

I hope you serve

    
answered by 24.10.2018 в 16:10