简体   繁体   中英

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. 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.

I am able to parse it into single values, but later I'm not able to access the list using the ctr. I am new to C#, so help is highly appreciated.

Is it because in your DisplayVal method you are setting ctr to 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.

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

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

"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 "," :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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