简体   繁体   English

C#中动态列表中的范围外异常

[英]Out of range exception in a dynamic list in C#

I am getting an ArgumentOutofRangeException when I try to access my list where I feed in real time data using a counter variable. 当我尝试访问我使用计数器变量实时提供数据的列表时,出现ArgumentOutofRangeException异常。 Here is my partial code 这是我的部分代码

private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
    RxString = serialPort1.ReadExisting();
    RxString = RxString.Replace("$", "");
    this.Invoke(new EventHandler(DisplayText));
}


private void DisplayText(object sender, EventArgs e)
{
    richTextBox1.AppendText(RxString);
    parsed(ref ctr);
    richTextBox2.Text = String.Join(Environment.NewLine, stringList);
}

public void parsed(ref int ctr)
{
    string line;
    line = richTextBox1.Text;

    stringList= new List<String(line.Split(','));
    displayval(ref int ctr);
}

public void displayval(ref int ctr)
{

   line = RxString;

   stringList= new List<String>(line.Split(','));
   richTextBox3.AppendText("\n Pressure:" + stringList[ctr]);
   ctr++;
   richTextBox3.AppendText("\n Accelerometer:" + stringList[ctr]);
   ctr++;
   richTextBox3.AppendText("\n Temperature:" + stringList[ctr]);
   ctr++;
   richTextBox3.AppendText("\n Height:" + stringList[ctr]);
   ctr++;
}

I am getting a real time serial input from an Arduino, and I want to parse the CSV value which is coming like $1032,432,541,145. 我正在从Arduino获取实时串行输入,我想解析CSV值,例如$ 1032,432,541,145。

I am able to parse it into single values, but later I'm not able to access the list using the ctr. 我可以将其解析为单个值,但是后来我无法使用ctr访问列表。 I am new to C#, so help is highly appreciated. 我是C#的新手,因此非常感谢您的帮助。

Is it because in your DisplayVal method you are setting ctr to ctr + 3? 是否因为在DisplayVal方法中将ctr设置为ctr + 3?

If so, then it will be out of range when you try to access it since it's trying to access the list starting from the 3rd value next time. 如果是这样,那么当您尝试访问它时,它将超出范围,因为它下次试图从第三个值开始访问列表。

It is possible that you read an incomplete message here: 您可能在此处阅读了不完整的消息:

RxString = serialPort1.ReadExisting();

So instead of "$1032,432,541,145" it may be "$1032,432,5" and your exception will occur. 因此,它可能不是“ $ 1032,432,541,145”,而是“ $ 1032,432,5”,并且您的异常会发生。

You should add some message termination symbol to your protocol. 您应该在协议中添加一些消息终止符号。 So that you can see that you received a complete message. 这样您就可以看到收到完整的消息。

You are appending lines separated by Environment.NewLine 您要追加由Environment.NewLine分隔的行

When you parse, you split on comma ','. 解析时,您使用逗号“,”分割。 Given the following 2 lines : 给出以下两行:

"value1;value2;value3;value4"
"value5;value6;value7;value8"

Your parsed stringList will have values 您解析的stringList将具有值

"value1"
"value2"
"value3"
"value4\nvalue5" << here, last value of 1st line followed by NewLine 
                    followed by first value of second line
"value6"
"value7"
"value8"

I recommend you parse each line separately. 我建议您分别解析每一行。

For example, if you know that your values will not contain the Environment.NewLine string, you can split first by NewLine and then each by "," : 例如,如果您知道您的值将不包含Environment.NewLine字符串,则可以先按NewLine分割,然后再按“,”分割:

var lines = richTextBox.Text.Split(Environment.NewLine)
stringList = new List<string>();
foreach (var l in lines)
    stringList.AddRange(l.Split(","));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM