Imports System
Imports System.IO
Imports System.IO.Ports
Public Class Form1
'Perform process when Button1 is pressed
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim recvstr As String
Dim i As Integer
Try
Button1.Enabled = False 'Disable buttons during communication......(a)
Button2.Enabled = False
'Communication port setting.......................(b)
SerialPort1.PortName = "COM1"
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.Parity = Parity.None
SerialPort1.StopBits = StopBits.One
SerialPort1.NewLine = vbCrLf 'Terminator setting......................(c)
SerialPort1.ReadTimeout = 2000 '2 seconds time out.....................(d)
SerialPort1.Open() 'Open a port
SendSetting(SerialPort1) 'Instrument settings
FileOpen(1, "data.csv", OpenMode.Output) 'Create text file to be saved.........(e)
For i = 1 To 10
SerialPort1.WriteLine(":FETCH?") 'Begin measurement and read measurement results Command....(f)
recvstr = SerialPort1.ReadLine() 'Read measurement results
WriteLine(1, recvstr) 'Write to file
Next
FileClose(1) 'Close file
SerialPort1.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