Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. recieveing and splitting serial data from Arduino in C#

recieveing and splitting serial data from Arduino in C#

Scheduled Pinned Locked Moved C#
csharpcssdatabaselinqgraphics
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    auting82
    wrote on last edited by
    #1

    Hi, I am fairly new to C# coding so be gentle :laugh: I am trying to receive measurement data from a two sensors DHT11 and soimoisture sensor connected to my Arduino. At this stage I just want to receive the raw measurement data and represent them in separate text boxes. I have almost managed it but I am getting some errors. More specifically I get this error: System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index' Here is my code:

    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;

    namespace PlantMonitoringApp
    {
    public partial class Form1 : Form
    {

        private SerialPort myport;
        private DateTime datetime;
        private string in\_data;
    
        public Form1()
        {
            InitializeComponent();
        }
    
        private void start\_btn\_Click(object sender, EventArgs e)
        {
            myport = new SerialPort();
            myport.BaudRate = 9600;
            myport.PortName = port\_name\_tb.Text;
            myport.Parity = Parity.None;
            myport.DataBits = 8;
            myport.StopBits = StopBits.One;
            myport.DataReceived += Myport\_DataReceived1;
            try
            {
                myport.Open();
                time\_text\_box.Text = "";
    
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
    
            // timer1.Start();
    
    
        }
        void Myport\_DataReceived1(object sender, SerialDataReceivedEventArgs e)
        {
    
            in\_data = myport.ReadLine();
    
            this.Invoke(new EventHandler(displaydata\_event));
            /\*String dataFromArduino = myport.ReadLine();
            String\[\] dataTempHumidMoisture = dataFromArduino.Split();
            int Temperature = (int)(Math.Round(Convert.ToDecimal(dataTempHumidMoisture\[0\]), 0));
            //int Humidity = (int)(Math.Round(Convert.ToDecimal(dataTempHumidMoisture\[1\]), 0));
           // int SoilMoisture= (int)(Math.Round(Convert.ToDecimal(dataTempHumidMoisture\[2\]), 0));
            txtTemperature.Text = Temperature.ToString() + "C";
           // txtHumidity.Text = Humidity.ToString() + "%";
            //txtHumidity.Text = SoilMoistu
    
    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups