Reading from a serial port in .NET

Read data from a serial port in Visual Basic .NET, C#, and C++ with PortController

Read Data from a Serial Port in .NET

When attempting to read data from a serial port in Windows, it is important to consider your application's thread design and any necessary code marshalling.

As with any program, applications that read from the serial port must strive to provide the best user experience while consuming the least amount of resources possible. In the case of a single threaded application which reads from the serial port, this is a difficult problem because any attempt to read from the serial port can cause the user interface to lock up if there is no incoming data available.

One technique for solving the UI freeze up problem is to continuously poll to see if there is any new incoming data before making the call to read from the serial port. Unfortunately, this technique can tax the CPU and make for some messy code, as your application must be written around perpetually checking the incoming data buffer hundreds of times a second. For simple applications where the incoming data rate is slow and code readability is not a concern, this can be a viable technique. Once the application gets a little more complex or the data starts coming in more quickly, though, look out!

A better method of reading from the serial port is to spin off a worker thread from the main application and have it monitor the serial port for new data. This is most efficiently done by calling the Win32 function WaitCommEvent and then blocking until the a port event occurs using WaitForSingleObject and supplying the event handle from an OVERLAPPED structure. When an event occurs, the worker thread checks to see if the event is a data received event, and if it is, it signals the main thread that data is available. Alternatively, the worker thread can read the data itself, but you must be careful not to let the worker thread call any UI controls since controls must be called from the thread in which they were created!

Sure, this technique works for Visual Basic 6 and Visual C++ applications, but how can we use the above technique when we are writing a .NET application? Indeed, there are no easy ways to read from a serial port within the .NET Framework, so we must use P/Invoke to call WaitCommEvent and WaitForSingleObject as described above. Also, because we receive unmanaged data when we read from the serial port, we must pin the underlying character buffer of the String we are using to receive the data before performing the read. These can be complex tasks.

Confused? Don't worry. Very few developers understand the ins and outs of serial communications -- especially in a .NET environment. Fortunately, PortController.NET can handle all of these complex details for you, making reading from a serial port as easy as calling the Read function. Plus, PortController.NET makes it easy to write to the serial port, set port parameters, control hardware and software handshaking, and more.

Looking for serial port help in a Visual Basic 6 or Visual C++ project? Try PortController ActiveX.

Learn more about PortController.NET
Download PortController.NET Trial

Learn more about PortController ActiveX
Download PortController ActiveX Trial

PortController.NET Screenshots
PortController ActiveX Screenshots
   2008 © Copyright Scientific Component. All rights reserved. Site Map