2020年6月19日 星期五

使用 VB 透過 SCPI 控制儀器設備

markdown
因要讀取電表的資料,所以接觸了SCPI這個語言,這次又要讀取示波器的波形資料,所以乾脆整理了一下使用的命令,方便於日後查詢

SCPI (Standard Command for Programmable Instrumentation)可程式化儀器標準語言,是透過ASCII語法來與儀器做溝通

簡單的命令範例如下
MEASure:VOLTage:DC?;:MEASure:CURRent:AC?
當然首先要先安裝電腦端的驅動程式,因為電表是使用Keysight的,所以就上Keysight網站找驅動程式了,下載網址

網站中有三套軟體,IO Libraries Suite是最主要的驅動,會安裝VisaComLib這個Library,主要是透過這個Library跟儀器做溝通的,所以至少要安裝這套軟體

在VB6中,要使用VisaComLib時,需要先引用它,所以要到 [專案] --> [設定引用項目] 中,選取 [VISA-COM 5.12 Type Library] 後才能開始寫應用程式

在VB6中宣告如下

 '--- General define ---
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 'declare to use "sleep"
Dim ioMgr As VisaComLib.ResourceManager
Dim instrument As VisaComLib.FormattedIO488

'--- function define ---
Dim ioMgr As VisaComLib.ResourceManagerDim instrument As VisaComLib.FormattedIO488
Dim sndBuffer As String
Dim rcvBuffer As String
Set ioMgr = New VisaComLib.ResourceManager
Set instrument = New VisaComLib.FormattedIO488
Set instrument.IO = ioMgr.Open("USB0::10893::4865::MY59022278::0::INSTR")
instrument.SetBufferSize IO_IN_AND_OUT_BUFS, 64

*IDN?是所有儀器通用的命令,透過這個指令來查詢儀器的 ID。儀器通常會回傳製造商名稱、儀器型號,以及製造商提供的韌體版本英數字元
instrument.WriteString "*IDN?"
Debug.Print instrument.ReadString
讀取電壓與電流的程式如下
'--------------------------
' Volt
'--------------------------
    status = commSend(instrument, "SENSe:VOLTage:DC:NULL:VALue:AUTO 1")
    status = commSend(instrument, "CONFigure:VOLTage:DC 10,0.001")        ' mV resolution
    status = commSend(instrument, "SAMPle:COUNt 1")
    status = commSend(instrument, "*WAI")
    rcvBuffer = commQuery(instrument, "READ?")
    
'--------------------------
' Current
'--------------------------
    status = commSend(instrument, "SENSe:CURRent:DC:NULL:VALue:AUTO 1")
    status = commSend(instrument, "CONFigure:CURRent:DC 1E-6")        
    status = commSend(instrument, "CURRent:DC:RES 1E-6")                        ' uA resolution
    status = commSend(instrument, "SAMPle:COUNt 1")
    status = commSend(instrument, "*WAI")
    rcvBuffer = commQuery(instrument, "READ?")
電表是使用USB連接,後來要控制R&S示波器,就想改用TCP/IP的方式試試,也是OK的喔,下面列出幾個範例,讀取波形電壓平均值、上升時間及波形的各點電壓資料
'==================================
' R&S RTM3004 波形平均值
'==================================
    instrument.WriteString "TRIGger:A:EDGE:SLOpe NEGative"
    instrument.WriteString "MEASurement1:SOURce CH2"
    instrument.WriteString "MEASurement1:MAIN MEAN"
    instrument.WriteString "MEASurement1:RESult? MEAN"
'==================================
'Measure rise time 
'==================================
    instrument.WriteString "TRIGger:A:EDGE:SLOpe NEGative"
    instrument.WriteString "MEASurement1:SOURce CH2"
    instrument.WriteString "REFLevel:RELative:MODE TEN"		' 10% - 90%
    instrument.WriteString "MEASurement1:MAIN RTIMe"
    instrument.WriteString "MEASurement1:RESult?"  
'==================================
' Wave RAW data
'==================================  
    instrument.WriteString "TRIGger:A:MODE NORMal"           ' Normal trigger
    instrument.WriteString "TRIGger:A:EDGE:SLOpe NEGative"      ' Negative edge
    
    instrument.WriteString "FORM ASC"							  ' output format ASCii
    instrument.WriteString "FORM:BORD LSBF"
    instrument.WriteString "CHAN1:DATA:POIN DEF"
    
    instrument.WriteString "SINGle"                     ' Single trigger
    
    instrument.WriteString "CHANnel1:DATA:HEAD?"
    Debug.Print instrument.ReadString
    
    instrument.WriteString "CHANnel1:DATA?"
    Debug.Print instrument.ReadString
下面是UART、USB與TCP/IP的宣告方式
'=========
 'UART
 '=========
    Set instrument.IO = ioMgr.Open("ASRL6::INSTR")  ' COM6
    
    Dim serInfc As VisaComLib.ISerial
    Set serInfc = instrument.IO
    serInfc.BaudRate = 9600                         ' 9600bps baud rate
    serInfc.FlowControl = ASRL_FLOW_NONE
    serInfc.Timeout = 10000
    instrument.IO.SendEndEnabled = True
    instrument.IO.TerminationCharacter = 10          ' could be 10 (line feed) or 13 (carriage return)
    instrument.IO.TerminationCharacterEnabled = True
    instrument.IO.Timeout = 10000
    
 '=========
 'USB
 '=========
    Set instrument.IO = ioMgr.Open("USB0::0x0AAD::0x01D6::104068::INSTR")
    instrument.SetBufferSize IO_IN_AND_OUT_BUFS, 64
    Set serInfc = instrument.IO
    
 '=========
 'TCP/IP
 '=========
    
    Set instrument.IO = ioMgr.Open("tcpip0::192.168.0.13::instr")        ' ip addr 192.168.0.13
    instrument.SetBufferSize IO_IN_AND_OUT_BUFS, 64
    Set serInfc = instrument.IO
下面是用Arduino與ADS1015做的4通道電表,解析度至小數第二位,使用VBA透過UART讀取電表至Excel表格中
Arduin 用的SCPI Library 有蠻多個 (Vrekrer_scpi_parser) (Open Instrument Control),下面範例是使用OIC的Library完成的



沒有留言:

Free ERP on the Raspberry Pi (odoo)

  #更新系統軟體 sudo apt update && sudo apt upgrade -y #安裝資料庫 sudo apt install postgresql -y #安裝py程序 sudo apt install python3-pip -y #安裝od...