Question serial Buffer overflow

softhard

Active member
Joined
Sep 29, 2012
Messages
35
Programming Experience
Beginner
Hello,
I have serial communication going between my device and PC. I receive greater amount data sometimes which taking more processing time in my code and meanwhile my buffer is overflowed and resulted as an error. How can i accomplish this much data receiving at a time and should not result the overflow? I have attached the corresponding image where "tdata" variable is declared as string and acting as my buffer.

How could i make my process fast?

Please help me.
 

Attachments

  • namnlös.JPG
    namnlös.JPG
    110.9 KB · Views: 30
The overflow exception is caused by attempting to assign a value greater than 255 or less than 0 to a variable declared as type Byte. String.IndexOf returns a value of type Integer. I recommend you turn on Option Strict.
I also recommend you limit the scope of that variable, you don't need that index outside that loop so declare it there when you need it. You also don't need to perform that IndexOf operation twice:
Dim index As Integer = tdata.IndexOf("~"c)
If index <> -1 Then '... use index

Your profile states .Net 2.0 which means Visual Basic 2005, in later versions type inference is introduced, which means you can declare a variable without specifying the type and compiler will interpret the type of assigned value, like this:
Dim index = tdata.IndexOf("~"c)

Inferred type here the index variable will be handled as type Integer, because that is what IndexOf returns. If possible upgrade to a newer Visual Basic version.
 
Back
Top