Writing a VB Application

The following description show how to write a simple Visual Basic Application to communicate with a device connected to the USB to TTL cable.

Adding the MSComm Component

To communicate with a Com Port the MSComm Component must be first added to your project.  Select Components from the Project Menu.  Scroll down the list of controls until you find "Microsoft Comm Control 6.0" and click on the tick box to select. 


Figure 1: Component Dialog Box showing the MSCom Component

Click OK.  This adds a telephone icon to your component toolbox.

You can add a MSComm Component to your Form just like adding a Button or Text Box.  Each MSComm component can communicate with one port. 

Initialising the Component

The MSComm Component can be set up either using the properties box (at design-time) or in software (at run-time).  We will assume that our USB to TTL Cable is mapped to Com 2 and that our embedded firmware is communicating at 9600baud.  We will set up the MSCom Component in Software (this allows to chose the com port from a menu or settings dialog in the future rather than hardcode this value now).  The initialisation of the Form is a good place to put the code.
 

Private Sub Form_Load()
    MSComm1.CommPort = 2
    MSComm1.Settings = "9600,n,8,1"
    MSComm1.Handshaking = comNone
End Sub

Code 1: Initialising the MSCom Component

Openning the Port

Opening the Port can be done with the following code, the Port is openned with the 'Open Port' Button is pressed.
 

Private Sub OpenPort_Click()
    MSComm1.PortOpen = True
End Sub

Code 2: Opening the Com Port

 

 

 

Send mail to webmaster@avitresearch.co.uk with questions or comments about this web site.
Copyright © 2007 AVIT Research Ltd
Last modified: 10/02/07