Click here to Skip to main content
Licence 
First Posted 13 Aug 2008
Views 36,030
Downloads 0
Bookmarked 42 times

SerialPort.NET: Top 10 Reasons Why You Deserve Better

By | 13 Aug 2008 | Article
The .NET Framework 2.0 comes with a SerialPort component that can perform basic communications tasks. There is only one problem: serial communications aren't a high priority for Microsoft, and it shows. CommStudio Express is a free alternative to the built-in .NET SerialPort control.
This article is in the Product Showcase section for our sponsors at The Code Project. These reviews are intended to provide you with information on products and services that we consider useful and of value to developers.

The Microsoft .NET Framework 2.0 comes with a SerialPort component that can perform basic communications tasks. There is only one problem: serial communications aren't a high priority for Microsoft, and it shows. The Microsoft SerialPort control is not only slow and hard to use, it also lacks some of the basic functionality required to communicate with some types of serial devices.

CommStudio Express is a free alternative to the built-in .NET SerialPort control. It's small, it's fast, and it's free. If you want more, it provides an easy upgrade path to CommStudio Professional, featuring an advanced debugger and ZModem file transfers. You can download the free CommStudio Express component from www.CommStudio.com. This article explains why there's a better way to live your life than struggling with the .NET Framework's built-in SerialPort class.

Contents

Reason #10: Performance Matters

It's an understatement to say that the built-in .NET SerialPort and SerialStream classes have a few performance issues. Scott Hanselman, .NET Guru Extraordinaire, notes in his blog that the built-in .NET serial port classes are sometimes four times slower than they should be.

"I noticed the implementation of set_dtrEnable inside of System.IO.Ports.SerialStream was WAY more complicated than it needed to be for my purposes." - Scott Hanselman

Unlike the SerialPort class, CommStudio was designed to be a lean and mean communicating machine. The entire CommStudio assembly is less than 250K of highly optimized managed C# code.

Performance issues affect more than just the speed of your application. They impact the overall system load, power usage, and battery life. In addition, CommStudio's higher efficiency results in superior scaling.

Reason #9: Get Your Support Engineers Out of Hardware Hell

If you're going to handle multiple ports at once, it's a good idea to use intelligent serial port adapters. Every version of CommStudio is extensively tested with intelligent multi-port devices such as Digiboards and Edgeport USB to Serial adapters. CommStudio supports serial connections over Windows Terminal Server and Remote Desktop Connection, right out of the box.

It's likely that your application will be used in a wide and unexpected variety of environments. By choosing a communications component with broad third-party hardware and software support, you will save your organization tons of technical support time.

Reason #8: Multi-threading Can Be Easy

The built-in .NET SerialPort fires its DataAvailable and PinChanged events from a helper thread. As a result, your event code may only directly access thread-safe objects inside your application. If you access Windows Forms controls without proper thread marshaling, you'll get an InvalidOperationException. Too many developers have found out the hard way that improper thread marshalling can be one of the most problematic issues to debug.

CommStudio makes developing event-driven and multi-threaded applications super easy: like several other multi-threaded classes in the .NET Framework it uses a SynchronizeObject property to let you determine which thread your event handlers should be called in. By default, all events will run in your main application thread and you don't have to worry about threading violations or marshalling.

Reason #7: Common UI Elements Instantly Make Your App Look Great

If you have time on your hands you could write the UI code for choosing and configuring serial ports yourself. For low-level settings you could tell you users to go to the Control Panel, and the high-level settings could be handled by dialog boxes you create. Getting everything working just right takes a surprising amount of effort, though: what happens if the user unplugs a USB cable and the hardware changes from underneath you? How do you handle (and test!) third party devices from different vendors?

CommStudio Professional only requires a device-appropriate user interface to let the user choose communications settings.

If SerialConnection1.ShowPropertiesDialog(Me) Then
  ' If user clicked OK, save settings to the registry
  SaveSetting("MyApp", "Communications", "Settings", _
    SerialConnection1.Options.ToString())
End If

Running this code on a system with a regular serial port will show the following dialog:

SerialProperties.png

CommStudio hides all this complexity from you and your users with an elegant and easy-to-use interface that takes less than a few minutes to integrate in your application.

Reason #6: Writing Automated Login Scripts Can Actually Be Fun

The large majority of serial communications applications interact step-by-step with devices or remote systems: the application would repeatedly send some data and then read the results back as a predetermined sequence of characters.

Using the built-in .NET Framework SerialPort control, this ends up being unnecessarily slow and complicated: you have to read the characters one by one and parse them on the fly. Because so many developers are building login-style scripts, CommStudio Professional makes this super easy and efficient.

For example, let's say you have an intelligent weather station with a serial port interface. Typically, you would query for a value, such as WIND and the device would respond with the strength and the direction.

Private Function ReadWindStrength() As Integer
    Dim result As Integer = -1
    Dim found As Boolean = False
    Dim strength As String

    ' Read up to 100 bytes until the device reports "WIND "
    SerialConnection1.ReadUntil(1000, "WIND ", found)
    If found Then
      ' Read value until the end of the line
      strength = SerialConnection1.ReadUntil(20, vbCr, found)
      ' Assign the value as in integer to result
      If found Then Integer.TryParse(strength, result)
    End If
    Return result
  End Function

Reason #5: You Deserve Real Support by Real Comm-geeks

OK, so let's say you decide to stick with what comes in the .NET Framework box and you run into a problem. Are you ready to spend a few hundred dollars just on support or use up one of your precious MSDN support incidents? Even if you do, serial communications is a very tiny detail for Microsoft, and any support engineer who ends up getting your support incident will most likely have to get familiar with this obscure detail in the .NET Framework.

Contrast this with CommStudio... we have been immersed in communications for over ten years, and we have thousands of customers who have built mission critical applications using CommStudio Professional. We want to help you, because that's all we do: help you make better communications apps. In fact, we eat parity bits for breakfast.

Reason #4: Three Words: ZModem, ZModem, ZModem!

ZModem rocks. It's the fastest and most reliable serial file transfer protocol available. CommStudio Professional features the most robust implementation available today, all written in super elegant C# code. CommStudio's ZModem implementation has been tested to interoperate with all popular different implementations of the protocol on Windows, Linux, Unix, and even mainframe computers.

The funny thing about the ZModem protocol is that it is making a very strong comeback in a surprising area: It was originally designed as a serial protocol for modem-based packet switching networks that had low connection quality with very high latency. Wired connections have come a long way since then, but cellular connections face the exact same set of challenges that the ZModem protocol was designed for.

The beauty of ZModem is that it takes advantage of both high-speed and error free-connection, and it can make the best of the lousiest, most error-ridden, slowest wireless connection you can imagine. If the system you're communicating with does not support ZModem, CommStudio can use the XModem and YModem protocols instead.

Reason #3: Wireless/Cellular Modems Make ZModem Hot Again

ZModem was developed many years ago to optimize speed and reliability of file transfers on modem-based packet switching networks. What were the characteristics of these networks? High latency and considerable packet loss. After a few decades, we've come full circle: cellular modem networks have high latency, and can have considerable packet loss when the signal is weak. If you're transferring data wirelessly and you want your data to absolutely, positively, get there, use ZModem.

Reason #2: Exceptional Debugging Tools Preserve Your Sanity

CommStudio Professional includes a life-saving debugger for serial communications. The main problem that faces serial communications developers is that everything happens in real time. You can't exactly step through the code slowly because by the time you're done, the other device will have timed out.

CommStudio solves this problem by giving you very detailed diagnostic information. You can see in real time what's happening, and you can also analyze it afterwards to really find out what's going on. If you have a nasty problem, CommStudio can be a lifesaver and save you hours and even days of development time (not to mention your sanity). Here's a sample from a log:

<call name=&quot;Open&quot; name=&quot;2008-08-11T00:33:35.333582Z&quot;>
  <long id=&quot;inputQueueSize&quot;>16384</long>
  <long id=&quot;outputQueueSize&quot;>16384</long>
</call>
<get name=&quot;IsOpen&quot; name=&quot;2008-08-11T00:33:35.337582Z&quot;>
  <bool id=&quot;Value&quot;>True</text>
</get>
<call name=&quot;Open&quot; name=&quot;2008-08-11T00:33:35.348582Z&quot; />
<event name=&quot;WriteBufferEmpty&quot; name=&quot;2008-08-11T00:33:35.359582Z&quot; />
<call name=&quot;Write&quot; name=&quot;2008-08-11T00:33:35.357582Z&quot;>
  <data id=&quot;buffer&quot;>41545A453156310D</data>
  <int id=&quot;offset&quot;>0</int>
  <int id=&quot;count&quot;>8</int>
  <int id=&quot;returns&quot;>8</int>
</call>
<call name=&quot;Close&quot; name=&quot;2008-08-11T00:33:37.678582Z&quot; />

Because all data is in XML format, it's easy to read and process by your own custom tools. For example, you could have your customers send a log file in case your application doesn't work as expected.

Reason #1: The Solution to All Your .NET SerialPort Problems is Free, as in Beer

DISCLAIMER: We are not suggesting that beer is the solution to your problems. However, as far as serial communications is concerned, CommStudio Express definitely fits the bill. It's free and it works - fast and easy.

If you need more power and features, there is always CommStudio Professional.

Thank you for taking the time to read this article. Regardless of which communications component you end up using, please let me wish you best of luck in all your development endeavors.

References

License

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

About the Author

Mike Sax

Web Developer

United States United States

Member

Mike Sax is president and founder of Sax.net (www.Sax.net), a leading vendor of communications and automation components. Mike is also founder and chairman of the Association for Competitive Technology, a Washington DC-based trade association that is working to preserve the freedom to achieve, compete and innovate (www.ActOnline.org).

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Question9 Bit data PinmemberAORD11:33 26 Mar '12  
GeneralZmodem using CommStudio not working properly - Why? PinmemberKeefJones3:33 27 Apr '11  
QuestionWhere/how do I download the express version? Pinmembersgwealti6:31 19 Jul '09  
Questionis the free version crippled peformance-wise? Pinmemberfriley4:55 14 Nov '08  
Generaldoes not work Pinmembernetiger20022:20 10 Nov '08  
GeneralYou control reset my computer PinmemberMember 19321665:39 5 Nov '08  
AnswerRe: You control reset my computer PinmemberMike Sax5:44 5 Nov '08  
GeneralRe: You control reset my computer Pinmemberflaviosoft7:11 5 Nov '08  
QuestionScript-driven - write method? Pinmemberdatpunk6:56 15 Oct '08  
QuestionHow to set timeout to Write method at serialConnection? PinmemberKoltz2:40 10 Sep '08  
AnswerRe: How to set timeout to Write method at serialConnection? PinmemberKoltz21:10 30 Sep '08  
QuestionDoes this assembly work in Console Application or Windows Service Pinmember3mbassador5:47 9 Sep '08  
Hi,
 
I've tried to use the system.IO.port class to communicate with a Caller ID device but haven't succeeded.
With this assembly and the sample application, I got it to work.
The question now is how do I do this in a Console Application in C# and VSExpress 2008 (if it's possible)?
 

Regards
Osterberg
AnswerRe: Does this assembly work in Console Application or Windows Service PinmemberMike Sax6:19 9 Sep '08  
QuestionRe: Does this assembly work in Console Application or Windows Service Pinmember3mbassador8:48 9 Sep '08  
AnswerRe: Does this assembly work in Console Application or Windows Service PinmemberMike Sax10:03 9 Sep '08  
QuestionDoes this SerialPort.NET support NET 1.1 ? PinmemberKoltz22:39 2 Sep '08  
AnswerRe: Does this SerialPort.NET support NET 1.1 ? PinmemberKoltz22:44 2 Sep '08  
AnswerRe: Does this SerialPort.NET support NET 1.1 ? PinmemberMike Sax6:43 3 Sep '08  
QuestionIs this Spam? PinmemberAppoc61711:08 22 Aug '08  
AnswerRe: Is this Spam? PinmemberMember 28903986:24 28 Aug '08  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120604.1 | Last Updated 13 Aug 2008
Article Copyright 2008 by Mike Sax
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid