The serial port in .Net

A few months ago a project at work required that we interface with a device through a serial connection. Ours is a .Net 4 app written in C#, and Microsoft provides a convenient SerialPort class that looked like it would suit our needs perfectly. Everything seemed fine during development, but as soon as it went to testing we started running in to problems. In our case, the app would intermittently crash when closing the serial port, and it was at that point that we discovered via Google that users have reported having problems using the SerialPort class as far back as .Net 2, when it was introduced.

Part of the problem is that SerialPort is more complex than you might initially expect – certainly more so than reading the documentation might suggest. The other part of the problem is that there are bugs in the implementation of the SerialPort class that have been acknowledged by Microsoft but remain unresolved.

Documentation

In the case of documentation, MSDN is a good place to start, especially to read the user comments that appear at the bottom of the page. Unfortunately, an intricacy of the MSDN site is that documentation is associated with a specific version of the framework, and so are the comments. If you Google for “.Net SerialPort”, the first link will take you to a page that shows the documentation for the most recent version of the framework, but at the time of this writing, it has no comments at all. Change the “Other Versions” drop down list near the top of the page to select .Net Framework 4, though, and you suddenly get a version of the page that shows a number of comments – which provide some incredibly useful (essential, really) bits of information and links.

This link in particular is worth checking out. It starts out as a question regarding the behaviour of the DataReceived event, but before you know it a Microsoft engineer is answering questions and providing a useful glimpse of the underlying behaviour of the class.

Bugs

Shortcomings in the documentation aside, there are a couple of long outstanding issues with the class that can cause problems for the unwary.

The first is the one that we ran in to during testing. The machine that we were testing on at the time had some hardware issues that were later resolved, but before we determined that that was the case, the app would intermittently disintegrate. No exception. Nothing.

A colleague spent a fair amount of time trying to track down the root cause, and ultimately found this post on the Microsoft Connect site. Microsoft acknowledges that it is an issue with the code, but has marked the issue as “won’t fix”. Fortunately the author of the report posted a workaround that we were able to implement and resolve the issue.

The second bug is one that we haven’t yet encountered. It’s described in detail in this blog post. In essence, the SerialPort class fails to initialize a setting of the port that can cause errors that are difficult to diagnose. As luck would have it, the same fellow that tracked down the issue also spent the time to nail down a workaround and posted it here.

The interesting thing about all this, other than the fact that what we thought would be completely straightforward but ended up being a minor adventure, was that in both cases the cause and solution could be determined because reflection enable inspection of the class.  I have to admit as well that it seems a bit weird to me that Microsoft hasn’t delivered an official fix.