BT3564_user_manual_chi_20191017H.pdf - 第163页

8.8 示例程序 157 8 MessageBox.Show(ex.Message, "Error", MessageBoxButtons .OK, MessageBo xIcon.Error) End Try End Sub 'Close program when Button2 is pressed Private Sub Button2_Click(sender As Object , e As Ev…

100%1 / 198
8.8
示例程序
156
如果利用 Visual C#
®
2017 记述与 Visual Basic
®
2017 相同的示例,则如下所示。
示例程序 (Visual C#
®
2017)
(a) 通讯期间,将 Begin Measurement”按钮与 Close”按钮设为无法按下。
(b) 的通讯条件与计算机的使用条件相匹配。
计算机使用的端口编号:1
传输速度:9600 bps
奇偶性:无
数据长度:8
停止位:1
(c) 将表示收发字符串结束段的终止符设为 CR + LF
(d) 将读入操作时间设置为 2 秒。
(e) 打开文件 data.csv。但是如果已有同名文件存在,则删除以前的文件 data.csv,生成文件。
(f) 向本仪器发出 “进行 1 次测量并将其结果返回计算机”的命令。
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
'Begin measurement and read measurement results Command.......................(f)
SerialPort1.WriteLine(":FETCH?")
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
8.8
示例程序
157
8
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
'Close program when Button2 is pressed
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.Dispose()
End Sub
End Class
8.8
示例程序
158