9 Replies - 3953 Views - Last Post: 10 November 2010 - 09:33 AM Rate Topic: -----

#1 TelcoJoe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 28-May 10

Index out of range exception

Posted 08 November 2010 - 06:33 PM

I am trying to parse serial data between two using indexof. When I test it using data entered into a text box, all is fine, but when I put the program into use with serial data, I get an index out of range exception.


//RxString is the serial data

int start = RxString.IndexOf('@') + 1; // Data starts with @
int end = s.IndexOf('#', start); // Ends with #
string result = s.Substring(start, end - start);

//The data now looks like 0001J123 and needs to be dissected
// into three different strings using  substring

string A = result.substring (0, 4);
string T = result.substring (4, 1);
string Z = result.substring (5, 3);




The program gets hung up usually on string Z with an Index out of Range Exception. This leads me to believe that the program may not be getting all of the data out of the serial port buffer. Any ideas on this?

This post has been edited by TelcoJoe: 08 November 2010 - 06:42 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Index out of range exception

#2 mavarazo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 182
  • Joined: 25-October 10

Re: Index out of range exception

Posted 08 November 2010 - 11:42 PM

When you debug it, does result contain a string of the size 8?
Was This Post Helpful? 0
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Index out of range exception

Posted 08 November 2010 - 11:57 PM

You are not checking to see if the '#' is found:

...
int end = s.IndexOf('#', start); // Ends with #
string result = s.Substring(start, end - start);
...



end will equal -1 if not found.

If it is then, yes, you have not received/retrieved all the data.

This post has been edited by n8wxs: 08 November 2010 - 11:59 PM

Was This Post Helpful? 0
  • +
  • -

#4 TelcoJoe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 28-May 10

Re: Index out of range exception

Posted 09 November 2010 - 10:40 AM

when I run the program using a textbox as input, it works fine, but the problems start when data is introduced from a serial port.
Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Index out of range exception

Posted 09 November 2010 - 10:53 AM

Conceptually it doesn't matter where the data is from as long as it is in the expected/same format. How is the data being represented from said serial port?
Was This Post Helpful? 0
  • +
  • -

#6 TelcoJoe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 28-May 10

Re: Index out of range exception

Posted 09 November 2010 - 12:55 PM

This is how the serial data is being received:

public void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
RxString = serialPort1.ReadExisting();
this.Invoke(new EventHandler(DisplayText);
}
public void DisplayText(object sender, EventArgs e)
{     
int start = RxString.IndexOf('@') + 1; 
int end = RxString.IndexOf('#', start); 
string result = RxString.Substring(start, end - start);

string A = result.substring (0, 4);
string T = result.substring (4, 1);
string Z = result.substring (5, 3);


This post has been edited by TelcoJoe: 09 November 2010 - 12:57 PM

Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Index out of range exception

Posted 09 November 2010 - 01:14 PM

Have you put some tracer statements in there to see what you're actually getting? There appear to be some gotchas/caveats regarding this function via its MSDN doc page.
Was This Post Helpful? 0
  • +
  • -

#8 TelcoJoe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 28-May 10

Re: Index out of range exception

Posted 09 November 2010 - 01:36 PM

If I remove the substring part of the program and just use:

int start = RxString.IndexOf('@')+1;
int end = RxString.IndexOf('#', start);
if (end == -1) end = RxString.Length;
string result = RxString.Substring(start, end - start); 
            
textBox1.AppendText(result);



Then the decoded stream comes through fine. The problem is when I then apply the substring to try and parse the data further. A "Index and length must refer to a location within the string." exception comes up.


string A = result.substring (0, 4);
string T = result.substring (4, 1);
string Z = result.substring (5, 3);


Was This Post Helpful? 0
  • +
  • -

#9 mavarazo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 182
  • Joined: 25-October 10

Re: Index out of range exception

Posted 09 November 2010 - 11:46 PM

Aight, that's the formal wording for Index out of Bounds :).
Please report an debug scenario with the values you are working with.
Was This Post Helpful? 0
  • +
  • -

#10 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2257
  • View blog
  • Posts: 9,445
  • Joined: 29-May 08

Re: Index out of range exception

Posted 10 November 2010 - 09:33 AM

TelcoJoe my you need to freshen up your Debugging Skills. So you can examine what the actual values are.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1