• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 737
  • Last Modified:

Serial Port data corruption

I have a program which reads data from radios that are at various places in this building,  through the Serial Port in a VB.Net program.  It works fine most of the time, but sometimes I got corrupted lines of data.

Examples of good data:
0000013598, R3, 00111110, 277, 56, 2011-02-23 07:51:45
0000013599, R3, 00111088, 277, 07, 2011-02-23 07:32:25
0000013601, R3, 00111108, 277, 65, 2011-02-23 07:17:03

Examples of bad data:
0000000R1 , R3, 70126698,  00, 20, 2011-02-23 08:01:56
000000R1 0, R1, 49910870, 000, 28, 2011-02-23 08:12:42
000000R1 1, R1, 32021850, 000, 28, 2011-02-23 04:06:25

I have found that if the program runs for not too long (20 mins to 2 hours or so), I have no corruption at all.  If Ii let it run for several hours, I find the corruption creeps in.  Right now I am using this:

charread = SerialPort1.ReadByte
str = str & (Chr(charread))

I then "chop up" the 'str' string and do stuff with it.

I was using ReadChar, but I just changed the code to ReadByte and I am running another test.  I've experimented with different values in the "ReadBufferSize" property, to no avail.

Any ideas?  Thanks.

  • 5
  • 3
1 Solution
Not much to go on, but here are some things to check:

In the Task manager, check the Virtual Memory usage of the process.  Is it creeping up or is it stable?

Anything in the System Event log?

Could any other program be trying to write to the serial port at the same time you're trying to read from it?
LD147Author Commented:
>>>In the Task manager, check the Virtual Memory usage of the process.  Is it creeping up or is it stable?
The memory usage is creeping up, but very gradually, and this is understandable considering the program is storing more and more data in memory as time goes on.

>>>Anything in the System Event log?
Nothing I can see that would be related or give me any kind of problem.

>>>Could any other program be trying to write to the serial port at the same time you're trying to read from it?
Definitely not.

Is ReadByte the best way to read the data?  
A few more thoughts:

Are you doing the ReadByte in a DataReceived event handler, or in-line (synchronously stalling the main application thread)?  If you are using a DataReceived handler, try using ReadExisting to grab all available bytes at once instead of ReadByte.

What happens in your program if ReadByte throws a TimeoutException?  Is it handled or logged?

ReadByte and ReadChar are pretty much the same - just don't mix them on the same port as they use different buffers.
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

LD147Author Commented:
I'm doing the ReadByte on a timer, so every xxx milliseconds (I vary it) it does this (plus some other stuff after the loop).  See code snippet.

As per the code, I check to see if there is data to read before trying to read it.  The routine isn't handled.

I just let a test run using ReadByte for about 2.5 hours, and I got 4 errors:

0006555557, R1, ?5100038, 018, 71, 2011-02-23 11:48:02
0007104555, R1, 555?=440, 019, 82, 2011-02-23 11:42:23
0989131313, R3, 06522<:5, 077, 97, 2011-02-23 12:51:38
6>02700643, R3, 00000002, 354, 89, 2011-02-23 12:15:27

The ?, =, >, <, : characters shouldn't be there (the : is ok in the time).

I'll look at ReadExisting.  Testing this thing is a pain because I have to run each test for a length of time, and there's only so many work hours in a day.....lol.  I'll post my findings using ReadExisting as soon as I can.

Do Until charread = 13 ' repeat until we find a carriage return
                    If SerialPort1.BytesToRead = 0 Then
                        lblStatus.Text = "Status: Listening..."
                        Exit Do
                    End If
                    charread = SerialPort1.ReadByte ' read data from the serial port

                    str = str & (Chr(charread))

Open in new window

I see nothing wrong with your code.

I know how frustrating it must be that it takes to long to reproduce.

Time to think "outside the box".

Maybe there really is noise.  Is PARITY enabled on that port?  Is there a wire you can wiggle?  Are the errors almost always on R1?
LD147Author Commented:
Parity = None.

There's an antenna on the receiver, so I just jiggled it around a little.

The errors are not mostly on R1 (or R3).  It seems like it's random (I know it can't be random but that's how it seems).

I'm going to run a routine that uses ReadExisting, and am going to monitor it the rest of the afternoon, and leave it running overnight.  We'll see how it looks in the morning.  Thanks for your help :)
LD147Author Commented:
I ran it overnight using the ReadExisting method, and out of 660,000 lines of data, I got about 250 errors.  That's better than what I was getting before, so I'll give you some points for recommending the better way to read the data, but the problem of corruption is not going away, I feel.  Some things are just beyond our control.

I can still filter those lines and remove those with special characters.

Thanks again for your help!
LD147Author Commented:
his answer did help but didn't solve the overall issue of the data being corrupted.
out of 660,000 lines of data, I got about 250 errors

I make that an error rate of 0.04% (or 99.96% good data), and by all statistical measures, that is by a margin "perfect" data (and thus errors are negligible).

I am sure if the wind is blowing one direction (or in a solar flare season) you can seek to reduce the error rate, but that would be splitting hairs.

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now