| http://www.devx.com | Printed from http://www.devx.com/dotnet/Article/31001/1954 |
|
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.
by
Wei-Meng Lee
|
|||
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
On my computer, the two serial ports are COM28 and COM29. Write down the port names as you'll need these later.
|
| Creating the Chat Application Using Visual Studio 2005, create a new Windows application and save it as C:\SerialCommChat. Populate the default Form1 as shown in Figure 3.
Table 1. Set the properties for the various controls, as shown.
Switch to the Code View for Form1 to start coding the form. First, declare a member variable SerialPort to represent the serial port that you want to work with:
Notice that you need to declare SerialPort with the WithEvents keyword. This is because the SerialPort class has the DataReceived event that is fired when data arrives at the serial port and hence you need to service this event to receive the data. When the form is first loaded, you will retrieve all the available serial port names on your computer using the My.Computer.Ports.SerialPortNames collection and then add these port names to the ComboBox control:
Figure 4 shows what the ComboBox control will look like when the form is first loaded.
In particular, notice that I have explicitly set the various properties of the SerialPort class, such as PortName, BaudRate, Parity, etc. These are the communications parameters you need to set when communicating with serial devices. When communicating with serial devices, check their settings such as baudrate, parity, databits, and stopbits and set them accordingly in your application.
The Disconnect button closes the current opened serial port:
To send data to the recipient through the serial port, use the Write() method of the SerialPort class:
One nice feature of the SerialPort class is that you need not constantly poll for incoming data. Instead, you just need to service the DataReceived event and it will automatically fire when incoming data is detected. However, as this event is running on a separate thread, any attempt to update the main Form directly will result in an error. Hence, you need to use a delegate to update controls on the main thread (Form1):
The delegate and the updateTextBox() subroutine is defined as follows:
|
| Testing the Application You are now ready to test the application. Press F5 in Visual Studio 2005 to debug the application. You need to run another instance of the application in order to test the chat functionality. Hence, go to C:\SerialCommChat\SerialCommChat\bin\Debug and double-click on SerialCommChat.exe. In the first instance of the application, select the port number used by your computer for the serial port connection, which you wrote down at the opening of this article. (My computer used port 28.) Then click Connect. On the other instance, select the other port (in my case, port 29) and click Connect. You can now start chatting (see Figure 5)!
Figure 6 shows sending and receiving Japanese characters.
While I tested the application on my local computer, you can also test the application in the following scenarios:
One interesting use for the chat application is to communicate with serial devices. One good candidate to test on is your Bluetooth-enabled mobile phone (and modems). Most mobile phones support the AT command set, which means that you can programmatically interact with the phone by issuing AT commands. To see how our application communicates with a Bluetooth handset, you first need the following hardware:
Table 2. Some AT commands
Take note that not all phones support the same AT command set. Refer to your handset's manual for the AT commands supported. Two very interesting AT commands are the ATDT and AT*EVA. You can use them to make and receive calls, respectively.
To allow users to control their mobile phones using their computer, I added the controls as shown in Figure 8. The code for the Dial Number and Answer Call buttons are as follows:
Press F5 to test the application. You can now enter a phone number, click the Dial Number button, and your mobile phone will automatically dial the number. When the phone rings, click the Answer Call button to answer the call.
As you can see, the SerialPort class has greatly simplified your life by encapsulating much of the functionality that you need (and which are only accessible through P/Invoke in .NET 1.x). Let me know what types of applications you're able to build using serial communications.
Wei-Meng Lee (Microsoft .NET MVP) is a technologist and founder of Developer Learning Solutions, a technology company specializing in hands-on training on the latest Microsoft technologies. He speaks regularly at international conferences and has authored and co-authored numerous books on .NET, XML, and wireless technologies, including 'Windows XP Unwired' and the '.NET Compact Framework Pocket Guide' (both from O'Reilly Media, Inc). He writes extensively for the O'Reilly Network on topics ranging from .NET to Mac OS X. Wei-Meng is currently the Microsoft Regional Director for Singapore.
|
| DevX is a division of Jupitermedia Corporation © Copyright 2007 Jupitermedia Corporation. All Rights Reserved. Legal Notices |