|
通常在需要大量的連續試驗數據采集時,需要使用自動化的可編程萬用表。其中HP/Agilent/Fluke/Keithley等多個廠家的儀表都支持用戶編程,并且支持 IEEE802.2 SCPI(標準儀器編程接口)命令編程。其中可用的接口可能是 GPIB/HPIB, 標準RS-232串口(含TTL規格接口),USB,PXI等,支持在多種開發環境下使用,支持多個儀器類型。這里以 Agilent 34405A 5 1/2 位數字萬用表為例做一介紹。如果將其編程接口形式改編,可以用于如HP34401/34410A等儀表中,實現同樣的目的。
'*****************************************************************************************************************
'
' Agilent 34405A 5 1/2位數字萬用表演示程序 for Excel
'
' 本程序由 Agilent 提供的例程改編。
'
' 應用前提:
' 1. 在系統中已經安裝了 IVISharedComponets 2.3.0 版(根據操作系統的差異,選擇32位或64位均可)。
' 2. 系統中已安裝 driver_ivi_matlab_Agilent_34405_1_1_1_0(34405A的驅動程序,32位或64位,根據系統確定)。
' 3. IOLibSuite_17_1_20011, 基本操作庫。這些資料需要下載的話,需要在 www點agilent點com/find/34405a 頁面下載(驅動),可能需要注冊。
' 4. 驗證系統是否可用的前提是在設備管理器中可以看到 "USB Test and Measument Devices" --> "USB Test and Measurement Device (IVI)"
' 5. 在設備管理器中選中該設備,點擊右鍵,選擇屬性,選擇"詳細信息"-->"設備范例ID",可以得到"USB\VID_0957&PID_0618\TW48070141"的字符,記錄備用。
'
' Excel中應用:
' 1. 在要記錄數據的Excel文檔中,創建新的宏模塊。
' 2. 點擊VB編輯器的"工具"-->"引用", 打開該文檔的工程引用窗口。
' 3. 在可引用的引用列表中,選擇"IVI Agilent34405 1.1 Type Library" 和 "IviDriver 1.0 Type Library",確定后關閉引用對話框。
' 4. 在要做測試的用戶模塊開頭,定義全局的 DMM 引用變量。 如"Dim myDmm as New Agilent34405" 為后續程序使用。
' 5. 使用前面在設備管理其中獲得的設備范例ID,作為要打開的設備名稱,操作設備。
' 6. 設備使用 Initialize 命令打開。使用結束后,使用Close命令關閉。
' 7. 設備Initialize 以后,設備面板上就會出現"Remote"的顯示,表示設備已經處于遙控模式。
' 8. 使用 34405A 面板上的 Local 按鍵可以使設備脫落Remote模式,接收面板控制。
' 9. 這些設備可以直接使用類型庫的內置函數操作,也可以通過 Agilent/HP 的 SCPI 腳本命令操作。兩者的功能相同。
'******************************************************************************************************************
'以下程序示例了設備的打開和具體功能測試。 使用的是 34405A的內置類型庫進行操作。
Sub TestAgilent34405()
'DMM 對象
Dim myDmm As New Agilent34405
Dim resourceDesc As String
Dim initOptions As String
Dim idquery As Boolean
Dim reset As Boolean
'中間過程變量
Dim msgStr As String
Dim errorCode As Long
Dim errorMsg As String
Dim myStr As String
Dim data As Double
'數據保存變量
Dim tstSht As Worksheet
Dim curRow As Integer
On Error GoTo ErrHandler
errorCode = -1
'*****************************************************************************************
'每次運行時,刪除已有的同名的數據表,然后再重新創建一個新的記錄表
'******************************************************************************************
For Each tstSht In ActiveWorkbook.Sheets
If tstSht.Name = "DMM_Agilent_34405A 操作演示" Then
tstSht.Delete
Exit For
End If
Next
Set tstSht = ActiveWorkbook.Sheets.Add
tstSht.Name = "DMM_Agilent_34405A 操作演示"
'*****************************************************************************************
'以下為測試過程的內容
'*****************************************************************************************
tstSht.Cells(1, 1) = "Agilent 34405A 自動操作演示程序 V1.0"
tstSht.Cells(2, 1) = "記錄時間:" & Format(Now(), "YYYY-mm-dd HH:MM:ss")
tstSht.Cells(4, 1) = "1. 設備信息查詢"
'設備初始化
ressourceDesc = "USB0::0x0957::0x0618::TW48070141::0::INSTR"
'ressourceDesc = "USB0::<manufacturer_ID>::<model_code>::<serial_number>::0::INSTR"
'此初始化過程對執行結果進行保存。 注意, Simulate 必須設置為 false, 否則會虛假運行。
initOptions = "QueryInstrStatus=true, Simulate=false, DriverSetup= Model=, Trace=true, TraceName=c:\\temp\\traceOut"
idquery = True '查詢設備ID
reset = True '設備初始化
myDmm.Initialize ressourceDesc, idquery, reset, initOptions '儀器初始化
tstSht.Cells(5, 1) = "設備驅動初始化正常。"
tstSht.Cells(6, 1) = "標識符: "
tstSht.Cells(6, 2) = myDmm.Identity.Identifier
tstSht.Cells(7, 1) = "版本: "
tstSht.Cells(7, 2) = myDmm.Identity.Revision
tstSht.Cells(8, 1) = "制造商: "
tstSht.Cells(8, 2) = myDmm.Identity.Vendor
tstSht.Cells(9, 1) = "設備描述: "
tstSht.Cells(9, 2) = myDmm.Identity.Description
tstSht.Cells(10, 1) = "型號: "
tstSht.Cells(10, 2) = myDmm.Identity.InstrumentModel
tstSht.Cells(11, 1) = "固件版本:"
tstSht.Cells(11, 2) = myDmm.Identity.InstrumentFirmwareRevision
tstSht.Cells(12, 1) = "序列號#: "
tstSht.Cells(12, 2) = myDmm.System.SerialNumber
tstSht.Cells(13, 1) = "模擬支持:"
tstSht.Cells(13, 2) = myDmm.DriverOperation.Simulate
tstSht.Cells(14, 1) = "SCPI 版本:"
tstSht.Cells(14, 2) = myDmm.System.SCPIVersion
tstSht.Cells(15, 1) = "蜂鳴器狀態:"
tstSht.Cells(15, 2) = myDmm.System.BeeperState
tstSht.Cells(16, 1) = "儀器當前狀態:"
tstSht.Cells(16, 2) = myDmm.DriverOperation.QueryInstrumentStatus
'*************************************************************************************************************************************************
'使用 SCPI 命令操作設備:通過 System2.WriteString 和 System2.ReadString 實現 SCPI命令的輸出和結果輸出。
' 具體的 SCPI 操作命令請參考 Agilent 34405A 操作手冊。
'**************************************************************************************************************************************************
tstSht.Cells(17, 1) = "2. SCPI 命令的支持"
tstSht.Cells(18, 1) = "*IDN?"
myDmm.System2.WriteString "*IDN?" '& vbCrLf
'myDmm.System2.WaitForOperationComplete 1000
tstSht.Cells(18, 2) = myDmm.System2.ReadString()
tstSht.Cells(19, 1) = "SYST:ERR?"
myDmm.System2.WriteString "SYSTem:Err?" ' & vbCrLf
'myDmm.System2.WaitForOperationComplete 1000
tstSht.Cells(19, 2) = myDmm.System2.ReadString()
'**************************************************************************************************************
'以下通過內置命令操作設備,將設備的全部功能都循環使用一次。
'**************************************************************************************************************
tstSht.Cells(22, 1) = "3. 常用操作命令演示"
myDmm.System.TimeoutMilliseconds = 15000 '系統響應超時時間定義為 15s。
'DMM 復位
myDmm.Utility.reset
curRow = 22
curRow = curRow + 1
'設置 DMM 為即時觸發模式
myDmm.Trigger.TriggerSource = Agilent34405TriggerSourceEnum.Agilent34405TriggerSourceImmediate
'"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
'---直流電壓測量 ----
'"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
'量程 10V, 快速測量模式(較低分辨率)
myDmm.Voltage.DCVoltage.Configure 10, Agilent34405ResolutionEnum.Agilent34405ResolutionLeast
'Display the Raw DC Volts Measurement
'MsgBox "Raw DC Volts measurement: " & Format(data, "0.000") & " Volts"
tstSht.Cells(curRow, 1) = "3.1 直流電壓測量,10V擋,快速測量,原始數據"
tstSht.Cells(curRow, 2) = "myDmm.Voltage.DCVoltage.Configure 10, Agilent34405ResolutionEnum.Agilent34405ResolutionLeast"
myDmm.System2.WaitForOperationComplete 1000
tstSht.Cells(curRow, 3) = myDmm.Measurement.Read()
tstSht.Cells(curRow, 4) = "V"
curRow = curRow + 1
'Use the measurement as the NULL value
myDmm.Math.NullValue = data
myDmm.Math.Enable = True
tstSht.Cells(curRow, 1) = "3.2 直流電壓測量,10V擋,使用Offset校準"
myDmm.System2.WaitForOperationComplete 1000
tstSht.Cells(curRow, 3) = myDmm.Measurement.Read()
tstSht.Cells(curRow, 4) = "V"
curRow = curRow + 1
'"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
' 交流電壓測量
'"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
'10V AC,低分辨率快速測試
myDmm.Voltage.ACVoltage.Configure 10, Agilent34405ResolutionEnum.Agilent34405ResolutionLeast
tstSht.Cells(curRow, 1) = "3.3 交流電壓測量,10V擋,低分辨率,快速原始數據"
tstSht.Cells(curRow, 2) = "myDmm.Voltage.ACVoltage.Configure 10, Agilent34405ResolutionEnum.Agilent34405ResolutionLeast"
myDmm.System2.WaitForOperationComplete 1000
tstSht.Cells(curRow, 3) = myDmm.Measurement.Read()
tstSht.Cells(curRow, 4) = "V"
curRow = curRow + 1
'Use the measurement as the NULL value
myDmm.Math.NullValue = data
myDmm.Math.Enable = True
tstSht.Cells(curRow, 1) = "3.4 交流電壓測量,10V擋,使用Offset校準"
myDmm.System2.WaitForOperationComplete 1000
tstSht.Cells(curRow, 3) = myDmm.Measurement.Read()
tstSht.Cells(curRow, 4) = "V"
curRow = curRow + 1
'"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
'直流電流測量
'"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
'100mA擋,低分辨率,快速模式
myDmm.Current.DCCurrent.Configure 0.1, Agilent34405ResolutionEnum.Agilent34405ResolutionLeast
tstSht.Cells(curRow, 1) = "3.5 直流電流測量,100mA擋,快速原始數據"
tstSht.Cells(curRow, 2) = "myDmm.Current.DCCurrent.Configure 0.1, Agilent34405ResolutionEnum.Agilent34405ResolutionLeast"
myDmm.System2.WaitForOperationComplete 1000
tstSht.Cells(curRow, 3) = myDmm.Measurement.Read() * 1000
tstSht.Cells(curRow, 4) = "mA"
curRow = curRow + 1
'"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
'交流電量測量
'"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
'100mA擋,低分辨率快速方式
myDmm.Current.ACCurrent.Configure 0.1, Agilent34405ResolutionEnum.Agilent34405ResolutionLeast
tstSht.Cells(curRow, 1) = "3.6 交流電流測量,100mA擋,快速原始數據"
tstSht.Cells(curRow, 2) = "myDmm.Current.ACCurrent.Configure 0.1, Agilent34405ResolutionEnum.Agilent34405ResolutionLeast"
myDmm.System2.WaitForOperationComplete 1000
tstSht.Cells(curRow, 3) = myDmm.Measurement.Read() * 1000
tstSht.Cells(curRow, 4) = "mA"
curRow = curRow + 1
'"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
'頻率測量
'"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
myDmm.Frequency.Configure 100, Agilent34405ResolutionEnum.Agilent34405ResolutionLeast
myDmm.Frequency.VoltageRange = 10
tstSht.Cells(curRow, 1) = "3.7 頻率測量,100Hz,10V 擋,快速原始數據"
tstSht.Cells(curRow, 2) = "myDmm.Frequency.Configure 100, Agilent34405ResolutionEnum.Agilent34405ResolutionLeast" & vbCrLf & "myDmm.Frequency.VoltageRange = 10"
myDmm.System2.WaitForOperationComplete 1000
tstSht.Cells(curRow, 3) = myDmm.Measurement.Read()
tstSht.Cells(curRow, 4) = "Hz"
curRow = curRow + 1
'"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
'電阻測量
'"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
'10k ohm 快速測量
myDmm.Resistance.Configure 10000#, Agilent34405ResolutionEnum.Agilent34405ResolutionLeast
tstSht.Cells(curRow, 1) = "3.8 電阻測量(2線法),10K擋,快速原始數據"
tstSht.Cells(curRow, 2) = "myDmm.Resistance.Configure 10000#, Agilent34405ResolutionEnum.Agilent34405ResolutionLeast"
myDmm.System2.WaitForOperationComplete 1000
tstSht.Cells(curRow, 3) = myDmm.Measurement.Read()
tstSht.Cells(curRow, 4) = "Ohms"
curRow = curRow + 1
'"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
'溫度測量
'"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
'配置為 5k Ohms 量程
myDmm.Temperature.Thermistor.Configure Agilent34405ThermistorTypeEnum.Agilent34405ThermistorType5000, Agilent34405ResolutionEnum.Agilent34405ResolutionBest
tstSht.Cells(curRow, 1) = "3.9 溫度測量,5K Ohm擋,快速原始數據"
tstSht.Cells(curRow, 2) = "myDmm.Temperature.Thermistor.Configure Agilent34405ThermistorTypeEnum.Agilent34405ThermistorType5000," & vbCrLf & " Agilent34405ResolutionEnum.Agilent34405ResolutionBest"
myDmm.System2.WaitForOperationComplete 1000
tstSht.Cells(curRow, 3) = myDmm.Measurement.Read()
tstSht.Cells(curRow, 4) = "degrees C"
curRow = curRow + 2
'********************************************************************************************
'主程序完成,以下是補充部分。
'錯誤檢測.
'********************************************************************************************
tstSht.Cells(curRow, 1) = "4 系統錯誤檢查"
curRow = curRow + 1
errorCode = -1
Do
myDmm.Utility.ErrorQuery errorCode, errorMsg
tstSht.Cells(curRow, 1) = "錯誤編號:" & errorCode
tstSht.Cells(curRow, 2) = errorMsg
curRow = curRow + 1
Loop While errorCode <> 0
'系統恢復并關閉測試程序
myDmm.System2.WriteString "*RST" '系統復位
myDmm.Close
curRow = curRow + 3
tstSht.Cells(curRow, 1) = "測試完成,數據關閉。 時間:"
tstSht.Cells(curRow, 2) = Format(Now(), "YYYY-mm-DD HH:MM:SS")
'***********************************************************************************************
'************************************表格格式化部分略去**********************************
'***********************************************************************************************
tstSht.Application.ActiveWorkbook.Save '保存測量的結果數據文件。
MsgBox "測試完成。!"
Exit Sub
'下面是出錯處理過程。比較簡單,未做詳細處理。
ErrHandler:
MsgBox "測試過程出錯。錯誤信息為:" & Err.Description
End Sub
實際運行的結果如下(數據表格的格式化):

這樣基本上實現了自動控制萬用表進行數據獲取的目的。在需要自動連續讀數的場所,可以大大降低對測試人員的需求。
|
|