recieveing and splitting serial data from Arduino in C#
-
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