Serial Port data corruption

Posted on 2011-02-23
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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.
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.


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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
No Data for DropDown List 2 32
VB.NET 2008 - SQL Timeout 9 51
"Emulate" TAB key when press Enter Key 3 74
"lblTime is not declared" 3 35
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question ( here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

726 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