Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi guys i try to create Ariplan Attitude . in here i read the serial from gyro but when i wanna read the 4th or 5th element of buffer i cant, i get this error ("Index was outside the bounds of the array in C#")
it seem when i put bytes it cant read 4th and 5th element.
how can i access to this element

the fourth element buffer[4] is read 0 or 1 for positive or negative , the fifth element buffer[5] is show the main number for rotation.

My Question is how can i read the buffer[4] & buffer[5] from serial like this (in Hex): 68 07 00 81 (00) (00) 98 20?

Mycode:
C#
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {

            // Obtain the number of bytes waiting in the port's buffer
            int bytes = serialPort1.BytesToRead;

            // Create a byte array buffer to hold the incoming data

            byte[] buffer = new byte[bytes];
            

            // Read the data from the port and store it in our buffer
           // if (buffer.Length == 7 )
             serialPort1.Read(buffer, 0, bytes); //bytes
           
             
            

            Log(LogMsgType.Incoming, ByteArrayToHexString(buffer));
           
         //   RxString = port.ReadLine();
         //   ArduinoData = RxString.Split(',', '\n', '\r');
          //  if (ArduinoData.Count() == 7) //ensures we have all data, plus line end ("\n" or "\r")
            if (buffer.Count() == bytes)
            {

                if (buffer[4] == 0) // <--- Problem

                    PitchAngle = Convert.ToDouble(buffer[5]);//<--- Problem
                else
                    PitchAngle = -1.0 * Convert.ToDouble(buffer[5]);//<--- Problem
                // RollAngle = -1.0 * Convert.ToDouble(buffer[5]) * Math.PI / 180;//<--- Problem
                // YawAngle = -1.0 * Convert.ToDouble(ArduinoData[6]) * Math.PI / 180;
                // if (YawAngle < -Math.PI) YawAngle = YawAngle + Math.PI;
                Invalidate();
            }
    
        }
Posted
Updated 19-Mar-13 8:26am
v2
Comments
Sergey Alexandrovich Kryukov 19-Mar-13 14:30pm    
Did you use the debugger?
—SA
Jegan Thiyagesan 19-Mar-13 16:16pm    
The line of code "if (buffer.Count() == bytes)" is redundant because you just created the buffer with size of bytes, it will always be true. What you should be checking is
if (bytes > 5)
So that if the bytes read is less than 5 you will not try to access that index.

Second, the DataReceived event sent out when there is some data in the serial buffer, this doesn't mean that the data is full packet i.e. in your case the full 8 bytes of data. So you have to keep the received data separate until you got all the 8 bytes, then process them.
A-DeveloperZ 19-Mar-13 16:53pm    
Thanks alot Jegan Thiyagesan its so nice answer 95% its work now i get better result

but how can i put bytes and read the fifth element ??

it seem its not exist ?? when i call buffer[5] ??

becuse when i put byets i get correct serial but i cant read buffer[5]

when i put 8 i have some wrong serial !!!

Thanks

If you want to access the fifth item of an array (namely buffer[4]) then you have first to make sure that such an item exists, that is array length is at least 5.
Since you don't know in advance how many bytes you are going to get from the serial port you need buffering, i.e. accumulate data collected in multiple event handler call into a buffer and process such buffer only after the accumulated data has the minimum required size.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 19-Mar-13 16:29pm    
Right, a 5.
—SA
A-DeveloperZ 19-Mar-13 16:39pm    
Thanks for u r nice answer, actually when i wanna call buffer[4] its not exist also buffer[5] how u got my array lenght is 5 ?

when i call buffer[0] or buffer[1] is fine its exist but buffer[4] &[5] is not create when i call so what should i do?

also when i make array 8 or 7 is fine but i get wrong Serial i mean i delete bytes and put 8 exept it but my serial become like that :

68 07 00 81 00 00 00 00 98 20 00 00 00 00 00 00

i dont know this code come from where

the correct one must come like that
68 07 00 81 00 00 97 1F
The code is so bad that I advise to throw it out. Here is the problem: You always compare count with '==' operator. This way, it's easy to make a bug, just because, say, comparison if (buffer.Count() == bytes) would let you execute the statement with buffer[4], if, by some reason, both buffer.Count() == 4 and bytes == 4. You don't need to ask questions, it would be enough to run it under the debugger.

But this style of coding cannot do anything good. You should never hard-code all your 4, 5, 7, -1.0 and so on. Just don't. You need to get rid of your spaghetti code.

—SA
 
Share this answer
 
Comments
A-DeveloperZ 19-Mar-13 15:57pm    
thanks for your Solution. but actually i didnt get my answer.

the problem is not from there.

actully i cant access to any element of buffer.

if u delete that part is fine the code is run but my question is i cant access to buffer[5] or buffer[4]. how can i access to it.

u know the better way to i write this part for buffer.

if u have a better idea can u please write.

Thanks.
Sergey Alexandrovich Kryukov 19-Mar-13 16:02pm    
The problem looks trivial, just write it from the very beginning, but accurately... there is no any technical "secrets" or techniques you need to get help with.
—SA
A-DeveloperZ 19-Mar-13 16:04pm    
Thanks.
Sergey Alexandrovich Kryukov 19-Mar-13 16:28pm    
Sure.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900