A first chance exception of type ‘System.IndexOutOfRangeException’ occurred in mscorlib.dll
I can avoid this exception by sending data at a slower rate. In my case, I am sending a string that is about 50 characters maximum (30 to 50). I doubt that it makes any difference but for information I am using XBee’s. If I send the strings at a rate faster than approximately 300 millisecond intervals I receive the exception. Above 300 milliseconds seems to be OK.
Now for my question…
I am using the serial port LineReceived event and I assume that the line received buffer is being overrun with data. (Just my assumption)
Is there a way I can catch this exception?
I’ve used try/catch blocks all over my code and none ‘trap’ this error. The exception does not seem to bother operation but I would like to trap the error and send a message back to the message source and YELL …“SLOW DOWN!”
void XBeeSerial_LineReceived(GT.Interfaces.Serial sender, string lineoftext)
{
try
{
XBeeSerial.DiscardInBuffer();
int count = 0;
foreach (char c in lineoftext)
{
count++;
}
if (count == 0 || count > 32) //Should never be larger than 32
{
//String received is empty or has bad format
return;
}
int len = 0;
string[] ssplit = lineoftext.Split(delimiter);
len = Int32.Parse(ssplit[0]); //Get the string character count
if (ssplit.Length > 1)
{
//With my DX5e controller a 0 is always received and never changes
//A 0 should be after the string length number
if (ssplit[1] != "0" && count != len)
{
//We received a bad string
return;
}
}
//Process data string received
DoDX5e(ssplit, lineoftext);
}
catch
{
string SPerror = "XBeeSerial_LineReceived error";
datastring[0].Text = SPerror; //string as received at serial port
//Update the text
window.Invalidate();
}
}
//
private void DoDX5e(string[] ssplit, string s)
{
try
{
/*
//Sent from PC as:
//I do not receive data from the Rate Hi/Lo switch or the Trim slide switches
_sarg = String.Empty;
_sarg = args.data[0].ToString(); //always 0
_sarg += "," + args.data[1].ToString(); //Throttle
_sarg += "," + args.data[2].ToString(); //always 0
_sarg += "," + args.data[3].ToString(); //Aileron
_sarg += "," + args.data[4].ToString(); //Elevator
_sarg += "," + args.data[5].ToString(); //Rudder
_sarg += "," + args.data[6].ToString(); //CH5switch
_sarg += "," + args.data[7].ToString(); //Trainerswitch
_sarg += "," + args.data[8].ToString(); //always 0
*/
//data array[].Text is Glide text boxes in T35 LCD display
//The first item in the string sent from the PC is the string length
//split[0] is the string length, split[1] (args.data[0]) is always 0
//args.data[0], args.data[3], args.data[9] are always 0
//First row of text boxes (5)
data1[0].Text = ssplit[2]; //Throttle
data1[1].Text = ssplit[6]; //Rudder
data1[2].Text = ssplit[5]; //Elevator
data1[3].Text = ssplit[4]; //Aileron
//Second row of text boxes (2)
data2[0].Text = ssplit[7]; //CH5 Switch
data2[1].Text = ssplit[8]; //Trainer Switch
//Forth row text box (1)
datastring[0].Text = s; //string as received at serial port
//Update the text
window.Invalidate();
}
catch
{
string SPerror = "DoDX5e method error";
datastring[0].Text = SPerror; //string as received at serial port
//Update the text
window.Invalidate();
}
}
@ willgeorge - Are you trying to read the channels of the DX5 receiver hooked up to the pc and send it to the FEZ? If so why not hook the receiver straight up to the FEZ?
Why are you discarding the input buffer? You might be losing data.
[quote]int count = 0;
foreach (char c in lineoftext)
{
count++;
}[/quote]
How about using:
Change your try/catch loop to:
[quote]try
{
…
}
catch(Exception ex)
{
…
}[/quote]
This will allow you to catch the exception.
I think you might have a problem where you processing time is too
long, and message are backing up in the event queue. When you handle
an event, you are clearing the input buffer, which might contain data,
which results in an unexpected message format. The Int32.Parse may be
trying to parse a non-numeric value.
I was thinking ‘Outside the box’ when I should have been thinking ‘inside’ the box (my code!) Geee…
Thinking about my question and thinking it was the LineReceived event was WAY OFF base.
In my way of thinking, all the LineReceive method cares about is when it receives the LineReceivedEventDelimiter. In my case it is the character ‘~’. It does not care about the string length (In my case is is somewhat variable). If the string is shorter than I want/need it does not care. It just sends me what it received.
Checking further I have found out that the exception ( ‘System.IndexOutOfRangeException’ ) is related to my string[] ssplit = lineoftext.Split(delimiter) statement.
Normally my ssplit array should be ssplit { string [10] } and sometimes (when sending strings too fast) the split array is { string [9] } or { string [8] } . I use the array to send text to different Glide text blocks on the T35. If I get the exception, I will not ‘see’ any problem in the T35 display. The exception happens and no new text is sent to that text block. I still see the old text.
Old code (partial)
int count = 0;
foreach (char c in lineoftext)
{
count++;
}
// My error here. I check for 0 and for a string longer than 32 characters
// I should be checking for the length of the string (not for 0)
// However, I do not know the length it should be yet.
// I only know this AFTER doing the string split.
//BUT string split is wrong if strings arrive too fast.
if (count == 0 || count > 32) //Should never be larger than 32
{
//String received is empty or has bad format
return;
}
int len = 0;
string[] ssplit = lineoftext.Split(delimiter);
// Only now should I be able to read the string length in ssplit[0] but if the array is wrong what do I do now?
Revised:
// I am now using
int len = 0;
string[] ssplit = lineoftext.Split(delimiter);
if (ssplit.Length != 10) // The array should always be ssplit { string [10] }
{
// Do something about the error.
string x = "error";
}
len = Int32.Parse(ssplit[0]); //Get the string character count
@ willgeorge - Are you trying to read the channels of the DX5 receiver hooked up to the pc and send it to the FEZ? If so why not hook the receiver straight up to the FEZ?
Dang… I’d have to think of that first!
Actually, that was really my next step. But at the moment, I am not sure how to use USB devices connected directly to the Spider. I wanted to be sure I could send data from my DX5e and my InterLink. These are for flight simulators. the InterLink is not a real transmitter the DX5e is. If I got those working I was going for my real goal which is a Fataba 6EX. I needed to know what data was available from the controllers and if I could use it in some way…
I am not using the receivers at the moment. Only the transmitter over USB.
I travled the world repairing all kinds of printing related machines. Everything from printing presses to large format film camers and all electronics from computers up to 440 volt motors. Anything above 440 volt I refused to work around.
Traveled everywhere but only a few times in eastern europe. I got so sick of planes, trains and hotels.
These days I refuse to travel anywhere unless it is an emergency.