?
Solved

Serial Port data corruption

Posted on 2011-02-23
9
Medium Priority
?
731 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
[X]
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
9 Comments
 
LVL 16

Expert Comment

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

Author Comment

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

Accepted Solution

by:
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

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

Open in new window

0
 
LVL 16

Expert Comment

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

Author Comment

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

Author Comment

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

Author Closing Comment

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

Expert Comment

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

764 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