Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Programming Serial Ports Using Visual Basic 2005 : Page 3

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.


advertisement
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 5. Chatty: The application is running, enabling chat between two COM ports.
 
Figure 6. Turning Japanese: Sending and receiving Japanese characters using the serial port application. .

If you want to converse in other languages (such as Japanese), you need to set the Encoding property of the SerialPort class so that the data can be sent and received correctly:

With serialPort .PortName = cbbCOMPorts.Text .BaudRate = 9600 .Parity = IO.Ports.Parity.None .DataBits = 8 .StopBits = IO.Ports.StopBits.One .Encoding = System.Text.Encoding.Unicode End With

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:

  • Two computers connected by Bluetooth
  • Two computers connected by a serial cable
Connecting to Serial Devices
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:

  • A Bluetooth-enabled handset, such as the Sony Ericsson T68i, or the Motorola E398
  • A Bluetooth adapter for your computer
Before running the application, pair up the computer with the Bluetooth-enabled handset. Your Bluetooth driver (on your computer) will tell you which serial port is being used to connect to the handset. Suppose that COM42 is used to connect to my Sony Ericsson T68i. I will now connect COM42 in my application and then issue the AT command (see Figure 7).

Author's Note: When communicating with external devices, remember to change the encoding from Unicode to the default.


Figure 7. Here I'm using Bluetooth to issue an AT command to my handset.
 
Figure 8. Here I've added some controls to Form1 in order to provide the user with an interface for manipulating the mobile handset from a computer.

You should see an "AT OK" returned by the phone. You can try out the sample AT commands listed in Table 2.

Table 2. Some AT commands

Command Usage Example Response
AT Attention AT OK
AT* List all supported AT commands *EACS*EAID*EALR*EALS*EAM*EAMS*EAPM*EAPNetc
AT+CGMI Request Manufacturer Identification ERICSSON
AT+CGMM Request Model Identification 1130202-BVT68
ATDT +Number Dial a number  
AT*EVA Answer a call  
AT+CBC? Check battery charge +CBC: 0,44 (44 means the battery is 44% charged)
AT+CSQ Signal Quality +CSQ: 14,99 (signal strength is from 0 to 31; 14 is the signal strength)

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.

Author's Note: Not all phones support the above two AT commands. I tested the two commands using the Sony Ericsson T68i.

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:

'------------------------------------------- ' Event handler for the Dial Number button '------------------------------------------- Private Sub btnDialNumber_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnDialNumber.Click serialPort.Write("ATDT " & txtPhoneNumber.Text & vbCrLf) End Sub '------------------------------------------- ' Event handler for the Answer Call button '------------------------------------------- Private Sub btnAnswerCall_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnAnswerCall.Click serialPort.Write("AT*EVA" & vbCrLf) End Sub

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 is a Microsoft .NET MVP and co-founder of Active Developer, a training company specializing in .NET and wireless technologies. He is a frequent speaker and author of numerous books on .NET, XML, and wireless technologies.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap