RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Programming Serial Ports Using Visual Basic 2005

While serial port programming was absent in .NET version 1.1, Visual Basic developers who grew accustomed to the MSCOMM control in VB6 will be glad to know that this functionality is supported again in .NET 2.0. Learn to use the SerialPort class to make two computers talk to one another or even to manipulate a mobile device from your computer using Bluetooth.

isual Basic programmers who do networking programming will no doubt be familiar with the MSCOMM control in VB6. For those of us who followed VB's progression to VB.NET, it was a big surprise to find that control missing in .NET 1.x. What we had instead was Platform Invoke (P/Invoke), which was the only way to access the unmanaged Win32 APIs from your managed application. Fortunately, the situation has been rectified: In .NET 2.0, the functionality of the MSCOMM control is restored in the form of the SerialPort control (located under the Components tab in the Toolbox).

In this article, I will show you how to use the SerialPort control in .NET 2.0 for your serial communication needs. In particular, I will build a chat application that allows two computers (connected using either a serial cable or a Bluetooth connection) to communicate. One interesting use of this chat application is in communicating with external serial devices. You will learn how to use the AT commands to programmatically control your mobile phones through a serial Bluetooth connection.

Hardware Needed
Unless you have two computers, you won't be able to test serial communications. However, you can use a null modem cable to connect two serial ports on the same computer to simulate two computers communicating over serial ports. But most computers today come with at most one serial port (and some notebooks do not even have one). One good solution is to use a USB-to-serial port adapter to convert an USB connection into a serial port. Hence, if you computer does not have any serial ports, you would need a pair of USB-to-serial port adapters, and a null modem cable (see Figure 1). Then, connect each USB-to-serial port adapters to a USB connection.

Figure 1. Accordingly: A USB-to-serial port adapter and a null modem cable will be needed unless you have two serial ports.
Figure 2. Portly: Locate the newly created serial ports and make note of their names; you'll need them later.

The USB-to-serial port adapter comes with its own drivers. After installing the drivers, right-click on My Computer on the Desktop and select Properties. In the System Properties window, click on the Hardware tab and click the Device Manager button. Expand the Ports (COM & LPT) item and locate the two newly added COM ports (see Figure 2).

On my computer, the two serial ports are COM28 and COM29. Write down the port names as you'll need these later.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date