31
Chapter 7 Remote Program Example Using Visual Basic 6
Acquires ten measurement values and displays the average value.
Remote Program Example Using
Visual Basic 6 Chapter 7
Private Sub Command1_Click()
' Create a form containing the following three objects:
' 1.TextBox - Text1
' 2.CommandButton - Command1
' 3. Microsoft Comm Control 6.0- MSComm1 added from Menu Bar / Project / Components
' Add from the menu bar [Project] - [Component].
MSComm1.CommPort = 1 ' set COM1 port
MSComm1.PortOpen = True ' open COM
MSComm1.Settings = "9600,n,8,1" ' communications settings
MSComm1.Output = "LLO" & vbCrLf ' send Local lock command
MSComm1.Output = "CONF:VOLT:DC 5" & vbCrLf ' set DCV 5V function
For i = 1 To 50000 ' wait for function ready
DoEvents
Next i
buffer = MSComm1.Input ' clear COM input buffer
Text1.Text = "" ' clear display
reading = 0 ' clear average value
For i = 1 To 10
MSComm1.Output = "FETC?" & vbCrLf ' send FETCH? command
buffer = ""
Do
buffer = buffer & MSComm1.Input
Loop While InStr(buffer, vbCrLf) = 0 ' receive reading
reading = reading + Val(buffer) ' sum reading
Text1.Text = Text1.Text & "Reading" & i & " = " & buffer & vbCrLf
' show reading
Next i
Text1.Text = Text1.Text & vbCrLf & "Average = " & (reading / 10) & vbCrLf
' show average
MSComm1.Output = "GTL" & vbCrLf ' send Goto Local command
MSComm1.Port Open = False ' close COM
End Sub