Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Question about overflow error message

Posted on 2007-10-02
6
Medium Priority
?
197 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");
                }
             
            }
        }
0
Comment
Question by:dave0989
  • 3
  • 2
5 Comments
 
LVL 22

Expert Comment

by:JimBrandley
ID: 20001780
Which line of code throws the exception?

Jim
0
 
LVL 22

Expert Comment

by:JimBrandley
ID: 20001841
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
 

Author Comment

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

Regards,

Dave
0
 

Author Comment

by:dave0989
ID: 20015143
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
 
LVL 22

Accepted Solution

by:
JimBrandley earned 2000 total points
ID: 20015380
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

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question