C# System.IndexOutOfRangeException:'索引超出了数组的界限。'C

C# System.IndexOutOfRangeException:'索引超出了数组的界限。'C,c#,.net,arrays,arduino,C#,.net,Arrays,Arduino,我目前正在进行一个项目,我将arduino uno连接到windows窗体,并使用串行通信显示其中的温度。 然而,每次我编译我的应用程序时,我都会遇到这个奇怪的错误 System.IndexOutOfRangeException:'索引超出了数组的边界 我已正确声明了字符串数组。有人能帮我克服这个问题吗 using System; using System.Windows.Forms; using System.Threading.Tasks; namespace WindowsFormsAp

我目前正在进行一个项目,我将arduino uno连接到windows窗体,并使用串行通信显示其中的温度。 然而,每次我编译我的应用程序时,我都会遇到这个奇怪的错误

System.IndexOutOfRangeException:'索引超出了数组的边界

我已正确声明了字符串数组。有人能帮我克服这个问题吗

using System;
using System.Windows.Forms;
using System.Threading.Tasks;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            serialPort1.Open();
        }

        public String[] towTemp = new string[1];
        public Task Tempdisplay()
            {
            //timer1.Start();
            return Task.Factory.StartNew(() =>
            {
                try
                {
                    String tempFromArduino = serialPort1.ReadLine().ToString();
                    towTemp = tempFromArduino.Split(',');
                    CheckForIllegalCrossThreadCalls = false;
                    if (float.TryParse(towTemp[0], out float result1))
                    {
                    result1 = (float)(Math.Round(Convert.ToDecimal(result1), 1));
                    label2.Text = result1.ToString();
                    aGauge1.Value = result1;
                    }
                    else
                    {
                        return;
                    }
                    if (float.TryParse(towTemp[1], out float result2))
                    {
                    result2 = (float)(Math.Round(Convert.ToDecimal(result2), 1));
                    label3.Text = result2.ToString();
                    aGauge2.Value = result2;
                    }
                    else
                    {
                        return;
                    }




                }
                catch (Exception err)
                {
                   MessageBox.Show(err.ToString());
                }
            });
            }

        private async void timer1_Tick(object sender, EventArgs e)
        {

            timer1.Interval = 1000;
           await Tempdisplay();



        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {

            timer1.Stop();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            timer1.Start();
        }
    }
}
用那条线towTemp=tempFromArduino.Split',';您覆盖了数组,这意味着它现在可以具有长度0


在访问它之前检查它的长度。

无论您如何声明它,您都会使用以下赋值覆盖原始数组:towTemp=tempFromArduino.Split',';。如果字符串中没有逗号,您将在towTemp[1]处收到此错误,您可以声明所需的数组,但如果您不填充该数组,或者传入的数据与预期不符,则可以获得超出范围的索引。在访问索引之前,请始终检查索引。我应该如何保存旧数组并尝试从arduino获取值?我已正确声明了字符串数组。你意识到新的字符串[1];创建一个包含1个元素的数组?@gre_gor我甚至创建了一个包含20个元素的数组,但我想我已经纠正了这个问题请重新检查这个问题我添加了一个串行COM的屏幕截图,以及值是如何流到windows应用程序的。也许你经常检查输出,但没有新数据,可以尝试以下方法:字符串tempfromardino=serialPort1.ReadLine.ToString;Console.WriteLinetempFromArduino;如果tempfromardino.Contains,{return;}towTemp=tempfromardino.Split',';我已经尝试过,但知道我遇到了以下错误:System.OverflowException:“算术运算导致溢出。”