Solved

Serial Port data corruption

Posted on 2011-02-23
9
722 Views
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.


0
Comment
Question by:LD147
  • 5
  • 3
9 Comments
 
LVL 16

Expert Comment

by:sjklein42
Comment Utility
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?
0
 
LVL 1

Author Comment

by:LD147
Comment Utility
>>>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?  
0
 
LVL 16

Accepted Solution

by:
sjklein42 earned 500 total points
Comment Utility
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.
0
 
LVL 1

Author Comment

by:LD147
Comment Utility
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))
Loop

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 16

Expert Comment

by:sjklein42
Comment Utility
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?
0
 
LVL 1

Author Comment

by:LD147
Comment Utility
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 :)
0
 
LVL 1

Author Comment

by:LD147
Comment Utility
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!
0
 
LVL 1

Author Closing Comment

by:LD147
Comment Utility
his answer did help but didn't solve the overall issue of the data being corrupted.
0
 
LVL 17

Expert Comment

by:nepaluz
Comment Utility
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.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now