No more messing with serial ports

One of the things I'm very excited about in the 2.0 release of the .NET Framework is the new System.IO.Ports.SerialPort class. I have to deal with serial communications a lot and right now I'm using my own implementation, which is about 600 lines of c# (350 lines of actual code and 250 lines of P/Invoke declarations).

What surprised me the most when testing the Whidbey SerialPort is that it appears to work well with virtual COM-ports. I had a lot of problems opening these ports exposed by Bluetooth and IrDA serial drivers: for instance, 3Com's bluetooth USB device comes with a bunch of software, including a virtual serial port application. The software creates a COM10 and COM11 for this purpose (on my machine at least, numbers will probably vary), but whenever I attempted to call CreateFile on one of these names I'd receive a file not found error. Some other software I tried that should normally work with serial ports didn't either, except for one application that somehow managed to work with every type of virtual or real serial port I threw at it: hyperterminal.

I ended up using API Monitor to find out that instead of opening "COM10", hyperterminal actually uses the string "\\.\COM10" when opening the port. By that time I had tried just about everything, so I decided to prefix all devicename strings used in CreateFile calls with "\\.\" and voila, works like a charm! Later on I found the entry describing this in the Platform SDK documentation ofcourse, but by that time I had already spent way too much time figuring it out. The main reason I didn't expect the issue to be related to the devicename string itself was because none of the sample code using the serial port I was using as references used this notation. Goes to show that you shouldn't depend on examples as documentation.

So besides being happy that I can shortly use an implementation created by people that are closer to the inner workings of it all, I'm also happy with the design: exposing the serial port as a stream will make it very easy to integrate with existing code!

1 Comment

Comments have been disabled for this content.