We help IT Professionals succeed at work.

Question about overflow error message

dave0989
dave0989 asked
on
210 Views
Last Modified: 2013-12-17
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");
                }
             
            }
        }
Comment
Watch Question

Top Expert 2007

Commented:
Which line of code throws the exception?

Jim
Top Expert 2007

Commented:
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

Author

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

Regards,

Dave

Author

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?
Top Expert 2007
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.