A lot of people are coming to this site searching for .NET Serial communications on Google. And while Paul Wilson made it known that System.IO.Ports.SerialPort will arrive in .NET Framework 2.0, that fact is not really useful to you if your project is due next week.
Jonathan Travers from Berkshire, England, is an experienced engineer, who just recently started playing with .NET and, according to his site, is impressed with Linux OS. He publishes and sells the RS232.dll .NET library for serial port communication (via C# or any other .NET-supported language), and the current price is 10 British pounds, which is pretty much a giveaway. My employer got a copy of the DLL, and so far it’s been probably the easiest tool for RS232 communication.
Download Jonathan’s help file, which he distributes for free. The instructions include adding the RS232Comms component to the Toolbox in Visual Studio, but apparently the example works only under Visual Studio .NET 2003 and higher as I have been unable to make an addition to my toolbox in the original Visual Studio .NET. Nevertheless, adding a DLL to the application is easy, and in older version of VS you just need to create a reference.
The objects reside in RS232 namespace, and you can specify the COM port number, baudrate, parity, stopbit, data bits, flow control settings and timeout value for the port. After initializing the port and setting all the parameters for the object, calling OpenComms() opens the serial port. WriteChar, WriteByte, WriteBlock(byte) and WriteBlock(string) account for majority of the data you’d want to send to the COM port, while everything that’s received can be handled in public event DataReceivedHandler DataRxEvent. The receiving part is completely event-driven, so no need to set up the timers and implement polling for the RS232 data, which can sometimes result in buffering errors and subsequent data loss.
public event CTS_EventHandler CTS_Event and public event CTS_EventHandler DSR_Event are there as well, but for my project I did not need them. Logging is enabled, so after discovering some errors that need debugging you can pull the data out of the ArrayList (which is really nothing more but a bunch of log strings):
ArrayList opencom = com1.GetPortOpenLog();