Question about overflow error message

Hi I'm working on the application on the side and I have asked questions about it before but now I have a new question regarding an error message I get when I run the application. What is going on that is causing this problem? Below is a portion of the code that might be able to help out.

OverFlowException

Value was either too large or too small for an unsigned byte. Here is the code that throws the error message.

I started getting this error message when I added the following code:
 comport.ReceivedBytesThreshold = 24;

What this function does is it takes the byte array buffer that stores all the data that was read from the serial port. It subtracts 0x30 ascii from each byte and adds the result.

private void processData(byte[] array)
        {                          
             int[] result = new int[array.Length];
            byte[] processedArray = new byte[array.Length];
            int j = 0;          

            //***this converts the data to integer
            for (int i = 0; i < array.Length; i++)
            {
                //result[i] = Convert.ToInt16((array[i]));
                portBuffer.Add(Convert.ToInt16(array[i]));
            }
           
            //this adds the data to the new array
            for (int i = 0; i < result.Length - 1; i += 2)
            {
                processedArray[j] = Convert.ToByte((portBuffer.IndexOf(i) - 0x30) + portBuffer.IndexOf(i + 1) - 0x30);///// this is where the debugger throws the error message
               // processedArray[j] = Convert.ToByte((result[i] - 0x30) + (result[i + 1] - 0x30));
                j++;

            }

            processEvents(processedArray);
           
        }


Here is the method I have for reading data from the serial port:

private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            // This method will be called when there is data waiting in the port's buffer


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

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

                buffer = new byte[bytes];

                comport.Read(buffer, 0, bytes);                                              
               
                if (buffer.Length == comport.ReceivedBytesThreshold)
                {
                    processData(buffer);
                    MessageBox.Show("test");
                }
             
            }
        }
dave0989Asked:
Who is Participating?
 
JimBrandleyCommented:
I wanted to see the values of the two values when the exception was thrown. One thing that is a bit confusing to me is that your byte array is not declared as unsigned, but the error message is complaining about an unsigned byte.

If you have an unsigned byte that contains 1, and subtract 2 from it, will you get zero or an exception? I guess I have never tried it.

Anyway, before we can eliminate the exception I think we need to know what the data values are at the time the exception is thrown.

Jim
0
 
JimBrandleyCommented:
Which line of code throws the exception?

Jim
0
 
JimBrandleyCommented:
Sorry - I missed the comment the first time. Add this befor the seconf for loop:
int first;
int second;

Then in the loop:
first = portBuffer.IndexOf(i);
second = portBuffer.IndexOf(i + 1);

Then quickwatch these two to see what you have when the exception is thrown.

Jim
0
 
dave0989Author Commented:
Great thanks. I will leave the question open untill tomorrow afternoon when I have a chance to test your reccommendation.

Regards,

Dave
0
 
dave0989Author Commented:
Well I went ahead and tried what you told me to do and I didn't get the error message but the extra thing I added was I commented the following code out.

processedArray[j] = Convert.ToByte((portBuffer.IndexOf(i) - 0x30) + portBuffer.IndexOf(i + 1) - 0x30);///// this is where the debugger throws the error message
               // processedArray[j] = Convert.ToByte((result[i] - 0x30) + (result[i + 1] - 0x30));

I did that because even after I added these changes

int first;
int second;

Then in the loop:
first = portBuffer.IndexOf(i);
second = portBuffer.IndexOf(i + 1);

I still recieved the error message. Then I went ahead and changed all my byte arrays to int arrays and the error message disappeared. Problem is I need to work with byte arrays and be able to subtract 0x30 from each byte and combine two bytes at a time. So there is some issue because byte is an unsigned int. So do you have any further ideas?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.