使用Python編寫控制NI VirtualBench的自動化腳本

更新 Jul 30, 2023

環境

硬件

  • VirtualBench All-in-One Instrument

驅動程式

  • NI-VirtualBench Application and Driver

程式語言

  • Python

我該如何使用Python編寫控制NI VirtualBench的自動化腳本?

有一個第三方套件程式: pyVirtualBench,提供了對VirtualBench硬體的Python接口。它透過包裝VirtualBench驅動程式的C API來實現。

注意:該套件是第三方軟體,NI (National Instruments)不直接提供技術支援。

請按照以下步驟測試由Python到VirtualBench的連線。
  1. 確保已滿足以下各項要求:
  2. 使用NI MAX或NI VirtualBench應用程式驗證電腦與已啟動的VirtualBench硬體之間的連線。
  3. 使用NI MAX或NI VirtualBench應用程式確認您的VirtualBench名稱: File » About
NI MAX:


NI VirtualBench應用程式:
  1. 依前步驟拿到的實際硬體名稱去調整Python腳本中的第5行PyVirtualBench的引數:
#!/usr/bin/env python
from pyvirtualbench import PyVirtualBench, DmmFunction, PyVirtualBenchException

# Change the next line before executing!
virtualbench = PyVirtualBench("VirtualBenchIK-VB8012")
# Device ID as found in NI VirtualBench Application in File > About
# The device's ID can be changed via the NI VirtualBench Application, File > Preferences  

try:
  dmm = virtualbench.acquire_digital_multimeter()
  dmm.configure_measurement(DmmFunction.DC_VOLTS, True, 10)
  myData = dmm.read()
  print(myData)
  dmm.release()
  
except PyVirtualBenchException as e:
  print("Error/Warning %d occured: %s" %(e.status, e)) 

finally:
  virtualbench.release()
 
  1. 根據作業系統的環境變數路徑和Python環境的設定,您可能需要在程式碼中增加對新package位置的引用。若是這種情況,請在上述腳本中二行插入對sys.path.append的設定,用.py包文件的路徑替換掉placeholder <absolute path to pyvirtualbench location>
#!/usr/bin/env python
sys.path.append(os.path.abspath("<absolute path to pyvirtualbench location>")
from pyvirtualbench import PyVirtualBench, DmmFunction, PyVirtualBenchException
[...]

  1. 執行腳本。腳本將會讓您的電腦連線到VirtualBench硬體,設定數位電表,讀取一個電壓值,然後在螢幕上輸出該值。將探針連線到VirtualBench前面板上的“ HI”和“ LOW”接腳以量測實際值。