Learn how to a build a cloud-first strategyRegister Now


Serial Port data corruption

Posted on 2011-02-23
Medium Priority
Last Modified: 2012-05-11
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.

Question by:LD147
  • 5
  • 3
LVL 16

Expert Comment

ID: 34962870
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?

Author Comment

ID: 34963256
>>>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?  
LVL 16

Accepted Solution

sjklein42 earned 1500 total points
ID: 34963737
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.
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Author Comment

ID: 34963834
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

LVL 16

Expert Comment

ID: 34963986
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?

Author Comment

ID: 34964068
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 :)

Author Comment

ID: 34973152
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!

Author Closing Comment

ID: 34973169
his answer did help but didn't solve the overall issue of the data being corrupted.
LVL 17

Expert Comment

ID: 34996767
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

810 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