8.8 Sample Programs
169
8
Chapter 8 RS-232C/GP-IB Interfaces
Shown below is a sample program which uses VB2005 to enact RS-232C com-
munication, set the measurement conditions, read measurement results and
then save them to file. The sample program will be written in the following man-
ner.
" Creation Procedure(Visual Basic 2005)" (⇒ p.167) description
........................................................................... Write using sample program
Button created to begin measuremen ................................................. Button1
Button created to close application ..................................................... Button2
When the [ Begin Measurement ] is pressed, takes 10 measurements and writes
the measurement values to a [ data.csv ] file.
When the [ Quit ] button is pressed the program closes.
The following program is written entirely in [ Form1 ] code.
Sample Programs(Visual Basic 2005)
Imports System
Imports System.IO
Imports System.IO.Ports
Public Class Form1
‘
Perform process when Button1 is pressed
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim recvstr As String
Dim i As Integer
Try
Button1.Enabled = False '
Disable buttons during communication ...................... (a)
Button2.Enabled = False
Dim sp As New SerialPort("COM1", 9600, Parity.None, 8, StopBits.One) '
Communication port setting .... (b)
sp.NewLine = vbCrLf 'Terminator setting ...........................................................(c)
sp.ReadTimeout = 2000 '
2 second time out .......................................................... (d)
sp.Open() 'Open port
SendSetting(sp) 'BT3562-01 or BT3563-01 settings
FileOpen(1, "data.csv", OpenMode.Output) 'Create text file to be saved .......................................... (e)
For i = 1 To 10
sp.WriteLine("
*FETCH?") 'Begin measurement and read measurement
results command
............................................................ (f)
recvstr = sp.ReadLine() 'Read measurement results
PrintLine(1, recvstr) 'Write to file
Next i
FileClose(1) '
Close file
sp.Close() 'Close port
Button1.Enabled = True
Button2.Enabled = True
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
'
Set measurement conditions
Private Sub SendSetting(ByVal sp As SerialPort)
Try
sp.WriteLine(":TRIG:SOUR IMM") 'Select internal triggering
sp.WriteLine(":INIT:CONT ON") 'Continuous measurement ON
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
‘
Close program when Button2 is pressed
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Dispose()
End Sub
End Class