Error: Index was outside the bounds of the array
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace FlightSim
{
public partial class Form1 : Form
{
SerialPort sp = new SerialPort();
String[] parts = new string[4]; //4 parts bec 4 adc value
string stx = "*0000"; //initialise stx string
public Form1()
{
InitializeComponent();
}
private void startcommButton_Click(object sender, EventArgs e)
{
if (sp.IsOpen)
{
sp.Close();
}
try
{
sp.PortName = "COM24";
sp.BaudRate = 115200;
sp.Parity = System.IO.Ports.Parity.None;
sp.DataBits = 8;
sp.StopBits = System.IO.Ports.StopBits.One;
sp.Open();
sp.WriteLine("\r");
label4.Text = "comm started";
timer1.Enabled = true;
}
catch (System.Exception)
{
//Creates and returns a string representation of the current exception.
MessageBox.Show("comm error");
}
}
private void quitButton_Click(object sender, EventArgs e)
{
sp.Close();
this.Close();
}
private void timer1_Tick(object sender, EventArgs e)
{
//There are different methods to capture the serial data. This is one method
stx = sp.ReadExisting(); //limit to 1 sec interval read by timer1
parts = stx.Split('*'); //chopped it from char *
label1.Text = parts[1]; // parts[0] is not valid, 1,2 and 3 OK
}
private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
//label1.Text = hScrollBar1.Value.ToString();
//axAirGauge1.NeedleValue = hScrollBar1.Value;
}
private void hScrollBar2_Scroll(object sender, ScrollEventArgs e)
{
}
private void hScrollBar3_Scroll(object sender, ScrollEventArgs e)
{
}
}
}
The error "Index was outside the bounds of the array" appeared at the line : label1.Text = parts[1];
Someone please tell me what happen..