Click here to Skip to main content
15,897,704 members
Articles / Operating Systems / Windows
Article

Having Problems Getting a .NET 2.0 SerialPort Instance to Raise its DataReceived Event?

Rate me:
Please Sign up or sign in to vote.
3.35/5 (11 votes)
23 Feb 2007CPOL3 min read 73.6K   20   14
When dealing with fast influxes of large amounts of data on instance of the SerialPort class in .NET 2.0 the dataReceived event does not fire. If you have this problem perhaps this could save you some cash on excedrine.

Introduction

This is an article about the System.IO.Ports.SerialPort bug and a work around for it.

Problem

It seems there is a flaw in the design within instances of the SerialPort class in the .NET 2.0 Framework. When too much information comes in on the serialport instance at a rather quick rate, the dataReceived event does not fire. For example, when using a GPS unit that adheres to the NMEA standard, information is continuously pumped to the serial port at a rather high rate. For some reason, this prevents the DataReceived event from firing. Also, when attempting to check the size of the buffer using the BytesToRead property on the instance there will always be a 0, so you cannot really properly size an array to its extents for a good read loop.

My colleague and I ran into this problem on a project, and as he scoured forum threads, FAQ's, and MSDN with no luck, I sat at my desk hacking at the problem. I played with various methods within the SerialPort object, and found a little trick that may help those forum threads that never got resolved.

Solution

Create a timed event perhaps using a forms.timer or a timer class (if you are not doing this from within a Windows Form). On its tick event, call your serialports' readbyte method, and append the byte to a module level variable that can be concatenated with the rest of the buffer. When this is called, the serialport instance actually gets a grasp on what is going on for a brief moment and the DataReceived event is actually fired! YAY, now you can get your hands on the number of the bytes that are available, as well as read what is inside the buffer! But don't celebrate just yet. The SerialPort is not fixed, and the event will only fire each time you call the readbyte method (which is why I decided to use a timer).

This is by no means a perfect solution. We have benchmarked the results and realized that the information is not fragmented or out of order for our purpose. The DataReceived event does not fire until after the readbyte method is called so information does not actually show up in the wrong place. I have not attempted to do anything between the concatenation. As soon as I called the readbyte I concatenated. It may be possible that if you perform other statements after the readbyte and before its concatenation that the delegate running the DataReceived event handler will concatenate before the readbyte since it is running on a separate thread, so keep this in mind. This may seem like a rather gimmicked work around but it works. The only way to make it perfect is for someone at Microsoft who has a little influence to catch wind of this, and make a change.

I am not sure if this is isolated to the .NET Framework, however. We were using a serialport OCX in a .NET 1.1 application we have, and in 2.0 which had the same problem, we didn't bother to experiment with it in the 2.0 Framework and since we were porting the 1.1 application to the 2.0 Framework, there was no need to keep the OCX around. As such, it could be a flaw in the CreateFile API (if my suspicions are correct in that the SerialPort class may be a wrapper for the API call), or a flaw somewhere else in the code. If this is the case, they may have fixed it in the Vista recode, but something tells me that they didn't do a complete recode… Why fix what isn't broken (that we all know about anyway?)

History

  • 23rd February, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralUSB -> Serial Driver [modified] Pin
JPP-11111-Dec-09 15:58
JPP-11111-Dec-09 15:58 
GeneralThread fix Pin
pulley6918-Sep-08 5:56
pulley6918-Sep-08 5:56 
GeneralSimilar Problem Pin
Doug Richards14-May-08 13:35
Doug Richards14-May-08 13:35 
GeneralSame Probleme here Pin
andreas648311-Sep-07 20:24
andreas648311-Sep-07 20:24 
GeneralSolve problem. Pin
paolobia29-Mar-07 4:08
paolobia29-Mar-07 4:08 
GeneralRe: Solve problem. Pin
iceteatwo11-Sep-09 2:55
iceteatwo11-Sep-09 2:55 
GeneralRe: Solve problem. Pin
gimic12-Sep-09 8:32
gimic12-Sep-09 8:32 
GeneralRe: Solve problem. Pin
iceteatwo14-Sep-09 21:17
iceteatwo14-Sep-09 21:17 
GeneralMore information please Pin
arcticbrew23-Feb-07 14:57
arcticbrew23-Feb-07 14:57 
GeneralRe: More information please Pin
gimic27-Feb-07 9:21
gimic27-Feb-07 9:21 
GeneralRe: More information please Pin
arcticbrew27-Feb-07 17:57
arcticbrew27-Feb-07 17:57 
GeneralRe: More information please Pin
gimic1-Mar-07 12:01
gimic1-Mar-07 12:01 
GeneralMore than a simple get data effort Pin
arcticbrew1-Mar-07 13:30
arcticbrew1-Mar-07 13:30 
GeneralRe: More information please Pin
KnutEdelbert5-Mar-07 2:08
KnutEdelbert5-Mar-07 2:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.