如何使用 Python 在 VeriStand 中設定 Automatic Frames Processing

更新 Jan 3, 2025

環境

軟體

  • VeriStand

程式語言

  • Python

在 VeriStand XNET 中,支援在Automatic Frames Processing (AFP) 中新增自訂 checksum (校驗) 和 rolling coutner (捲動計數器)。以Python實作的niveristand套件包含與NI VeriStand系統定義檔互動的API(應用程式介面)。本文將介紹如何透過Python腳本在VeriStand XNET中設定AFP。
如果您需要直接使用VeriStand系統定義檔設定AFP,可以參考這篇文章如何在VeriStand XNET中設定AFP功能

要使用 Python 編寫 VeriStand 系統定義腳本,VeriStand 團隊建議使用官方支援的niveristand套件中的功能。

就腳本編寫而言,要遵循的高階操作順序如下:

  1. 實例化一個新的SystemDefinition物件。

  2. 使用系統定義 API 去:

    1. 將新專案(使用者通道、CAN 連線埠等)填入該SystemDefinition 。這是透過將條目新增至組成系統定義的整體樹狀結構(與使用者在系統資源管理器中查看系統定義時可見的相同層次結構)來完成的。

      • 注意:建立的所有系統定義物件都必須新增到此現有結構中,以便它們反映在產生的系統定義中。單獨實例化這些物件是不夠的。
    2. 根據需要更新/設定系統定義中現有專案的屬性。

  3. 呼叫save_system_definition_file將建置的系統定義寫入磁碟上指定路徑的檔案。
    下面是演示設定的範例程式碼:

from niveristand import systemdefinitionapi

# Create the system definition
systemDefinition = systemdefinitionapi.SystemDefinition(
    "Test System Definition",
    "Test Description",
    "Author",
    "1.0.0.0",
    "Controller",
    "Windows",
    "C:\\Users\\Public\\Documents\\Test.nivssdf")
target = systemDefinition.root.get_targets().get_target_list()[0]
xnet = target.get_hardware().get_chassis_list()[0].get_xnet()

# Create a database and add it into the system definition
database = systemdefinitionapi.Database("NIXNET_example")
target.get_xnet_databases().add_database(database)

# Create the CAN port
canPort = systemdefinitionapi.CANPort("TestCANPort", 1, database, "CAN_Cluster", 100000)

# Set the Automatic Frame Processing info for the CAN port
canPort.set_automatic_frame_processing("NI VeriStand 2011_CRC8", "National Instruments\\NI VeriStand 2011_CRC8.enb", [0,0,0,0])

# Add the CAN port into the system definition
xnet.get_can().add_can_port(canPort)

# Add an outgoing frame under the CAN port
signalBasedFrame = systemdefinitionapi.SignalBasedFrame("TestFrame", 123, database, "CAN_Cluster", 64, 0, False, [])
canPort.get_outgoing().get_cyclic().add_signal_based_frame(signalBasedFrame)

# Add the Automatic Frame Processing section to the frame
automaticFrameProcessing = signalBasedFrame.create_automatic_frame_processing()

crc = automaticFrameProcessing.get_crc()
counter = automaticFrameProcessing.get_counter()

# TODO: Perform any other needed configuration
crc.afp_data = [1,1,0,0]
counter.afp_data = [1,1,0,0]
crc.remove_alternate_channel()
counter.remove_alternate_channel()

# Save the system definition
saved, error = systemDefinition.save_system_definition_file()

if saved:
    print("Save success")
else:
    print("Save failure")

成功執行上述程式碼後,您應該在下列路徑建立一個名為 Test.nivssdf 的 VeriStand 系統定義檔:

C:\Users\Public\Documents\