Navigate Up
Sign In
 
 

Visual Basic Serial Interface Program

The serial interface program works with Visual Basic 6.0 (VB6) on an IBM PC (or compatible) with a Pentium-class processor. A Pentium 90 or higher is recommended, running Windows 95 or better, with a serial interface. It uses the COM1 communications port at 9600 Baud. Use the following procedure to develop the Serial Interface Program in Visual Basic.

 

1.  Start VB6.

2.  Choose Standard EXE and select Open.

3.  Resize form window to desired size.

4.  On the Project Menu, click Components to bring up a list of additional controls available in VB6.

5.  Scroll through the controls and select Microsoft Comm Control 6.0. Select OK. In the toolbar at the left of the screen, the Comm Control will have appeared as a telephone icon.

6.  Select the Comm control and add it to the form.

7.  Add controls to form:

a.  Add three Label controls to the form.

b.  Add two TextBox controls to the form.

c.  Add one CommandButton control to the form.

d.  Add one Timer control to the form.

8.  On the View Menu, select Properties Window.

9.  In the Properties window, use the dropdown list to select between the different controls of the current project.

 

Properties Window Layout 

 

10.  Set the properties of the controls as defined in Table 1.

11.  Save the program.

 

 

Table 1.  Serial Interface Program Control Properties

Current Name

Property

New Value

Label1

Name

Caption

lblExitProgram

Type "exit" to end program.

Label2

Name

Caption

lblCommand

Command

Label3

Name

Caption

lblResponse

Response

Text1

Name

Text

txtCommand

<blank>

Text2

Name

Text

txtResponse

<blank>

Command1

Name

Caption

Default

cmdSend

Send

True

Form1

Name

Caption

frmSerial

Serial Interface Program

Timer1

Enabled

Interval

False

10

 

12.  Add code (provided in Table 2).

a.  In the Code Editor window, under the Object dropdown list, select (General). Add the statement: Public gSend as Boolean

b.  Double Click on cmdSend. Add code segment under Private Sub cmdSend_Click( ) as shown in Table 2.

c.  In the Code Editor window, under the Object dropdown list, select Form. Make sure the Procedure dropdown list is set at Load. The Code window should have written the segment of code: Private Sub Form_Load( ). Add the code to this subroutine as shown in Table 2.

d.  Double Click on the Timer control. Add code segment under Private Sub Timer1_Timer() as shown in Table 2.

e.  Make adjustments to code if different Com port settings are being used.

13.  Save the program.

14.  Run the program. The program should resemble the following.

 

 

 

15.  Type a command query as described in your instrument User's Manual, in the chapter called Remote Interface.

16.  Press Enter or select the Send button with the mouse to send command.

17.  Type Exit and press Enter to quit.

 

 

Table 2.  Visual Basic Serial Interface Program (See SERIAL_VB.TXT)

Public gSend As Boolean                                     'Global used for Send button state

Private Sub cmdSend_Click()                               'Routine to handle Send button press

    gSend = True                                          'Set Flag to True

End Sub

Private Sub Form_Load()                                   'Main code section

    Dim strReturn As String                               'Used to return response

    Dim strHold As String                                 'Temporary character space

    Dim Term As String                                    'Terminators

    Dim ZeroCount As Integer                              'Counter used for Timing out

    Dim strCommand As String                              'Data string sent to instrument

 

    frmSerial.Show                                        'Show main window

    Term = Chr(13) & Chr(10)                              'Terminators are <CR><LF>

    ZeroCount = 0                                         'Initialize counter

    strReturn = ""                                        'Clear return string

    strHold = ""                                          'Clear holding string

    If frmSerial.MSComm1.PortOpen = True Then             'Close serial port to change settings

        frmSerial.MSComm1.PortOpen = False

    End If

    frmSerial.MSComm1.CommPort = 1                        'Example of Comm 1

    frmSerial.MSComm1.Settings = "9600,o,7,1"             'Example of 9600 Baud,Parity,Data,Stop

    frmSerial.MSComm1.InputLen = 1                        'Read one character at a time

    frmSerial.MSComm1.PortOpen = True                     'Open port

 

  Do

    Do                                                    'Wait loop

    DoEvents                                              'Give up processor to other events

    Loop Until gSend = True                               'Loop until Send button pressed

    gSend = False                                         'Set Flag as false

   

    strCommand = frmSerial.txtCommand.Text                'Get Command

    strReturn = ""                                        'Clear response display

 

    strCommand = UCase(strCommand)                        'Set all characters to upper case

    If strCommand = "EXIT" Then                           'Get out on EXIT

        End

    End If

 

    frmSerial.MSComm1.Output = strCommand & Term          'Send command to instrument

    If InStr(strCommand, "?") <> 0 Then                   'Check to see if query

        While (ZeroCount < 20) And (strHold <> Chr$(10))  'Wait for response

            If frmSerial.MSComm1.InBufferCount = 0 Then   'Add 1 to timeout if no character

                frmSerial.Timer1.Enabled = True

                Do

                DoEvents                                  'Wait for 10 millisecond timer

                Loop Until frmSerial.Timer1.Enabled = False

                ZeroCount = ZeroCount + 1                 'Timeout at 2 seconds

            Else

                ZeroCount = 0                             'Reset timeout for each character

                strHold = frmSerial.MSComm1.Input         'Read in one character

                strReturn = strReturn + strHold           'Add next character to string

            End If

        Wend                                              'Get characters until terminators

 

        If strReturn <> "" Then                           'Check if string empty

            strReturn = Mid(strReturn, 1, InStr(strReturn, Term) - 1)  'Strip terminators

        Else

            strReturn = "No Response"                     'Send No Response

        End If

        frmSerial.txtResponse.Text = strReturn            'Put response in textbox on main form

        strHold = ""                                      'Reset holding string

        ZeroCount = 0                                     'Reset timeout counter

    End If

  Loop

End Sub

Private Sub Timer1_Timer()                                'Routine to handle Timer interrupt

    frmSerial.Timer1.Enabled = False                      'Turn off timer

End Sub