top of page

探索 Saleae Logic 2 及自動化 API:提升訊號分析的效率和靈活性


《Saleae專精設計簡易、實惠、多種應用的邏輯分析儀(Logic Analyzers);除了設備本身,亦在軟體上提供多種擴充套件搭配應用。邏輯分析儀的基本工作是抓取數位數據,儲存於緩衝區中並顯示在軟體畫面中。此外,還有擷取訊號並解碼的功能;大多數現代數位通訊系統,都是基於特定的協定來傳輸資料的。邏輯分析儀或其相關軟體套件可以支援這些協定,以幫助使用者了解所擷取的資料意義。


Saleae Logic Analyzers
Saleae Logic Analyzers

Saleae 的邏輯分析儀系列,使用自行開發的軟體〈Saleae Logic 2〉,以其強大的功能和直觀的使用介面,支援 Windows、Linux、MacOS、MacOS 下載方式。目前已被全球的 Saleae 使用者廣泛應用在 I2CI3CSPIMDIO、SMBus 等協定分析。現在,Saleae 導入了〈自動化 API〉,讓 Saleae Logic 2 更進一步,提供更多分析的靈活性。


本文將討論 Saleae Logic 2 和其自動化 API,說明它們在提升信號分析效率方面的優勢。



Saleae Logic 2 的主要用途:


  • 數位訊號擷取和分析 Logic 2 軟體搭配 Saleae 分析儀,能夠擷取、顯示多達 16 個通道的數位訊號,支援高達 500 MHz 的取樣速率。應用上,特別適合分析數位通信協議,如 I2C、SPI、UART 與 I3C 等。

  • 多種協定分析 該軟體內建多種協定分析功能,能夠自動解析捕捉到的數據,轉換為工程師方便閱讀的格式;利於校正、驗證儀器的通訊傳輸內容,幫助工程師快速找出問題。

  • 混合訊號分析 除了數位訊號,Saleae Logic 2 還支援類比信號的擷取和分析。這對於分析類比數位轉換器(ADC)、數位類比轉換器(DAC)、以及其他混合式的訊號系統,提供極大的便利。

  • 數據匯出和處理 使用者可以將擷取、或分析後的數據匯出為多種格式,如:CSV、JSON 等;幫助後續數據處理、製程報告等工作。

  • 自動化測試 借助於 Saleae Logic 2 的自動化 API,使用者可以透過現有提供的腳本,實現測試流程的自動化。這對於需要進行大量重複測試的情況,能夠顯著提高效率和一致性。關於這點,我們在文章後續做說明。


Saleae Logic 2 Automation API
Saleae Logic 2 Automation API


什麼是 Saleae Logic 2 的自動化 API?


〈Saleae Logic 2 Automation API〉以下簡稱為自動化 API 工具,使用者可以透編碼,控制 Saleae Logic 2 的各項功能;例如自動執行擷取、分析和數據處理,適合用於頻繁重複的測試工作、長期監控、或大量數據處理工作。


以下為入門說明:



1. 安裝 Python 自動化 API 程式


首先,使用自動化 API,需使用最新版本的 Logic 2 sofiware(2.4.0+)、logic2 - Automation(1.0.0+)Python 以及 Python 3.8、3.9 或 3.10。


安裝參考:


並安裝〈logic 2 - Automation〉套件,語法如以下:

pip install logic2-automation


2. 啟動 Logic 2


在軟體 UI 中啟動自動化介面,從主選單開啟對話框,捲動到底部。

Saleae logic 2 server UI
Saleae Logic 2 Server UI

勾選「Enable Automation Server」,自動化伺服器將開始在 Logic 2 軟體中的預設連接埠 10430 上運作。



3. 使用 Python 自動化 API


當你的 Logic 2 軟體可以在預設連接埠 10430 上運作後,可以用 這份說明上的範例 來測試自動化 API。不需連接裝置,可直接使用以下範例操作:


  • 請先建立一個名為「saleae_example.py」的 Python 文件,並貼上這份範例語法:

from saleae import automation
import os
import os.path
from datetime import datetime
# Connect to the running Logic 2 Application on port `10430`.
# Alternatively you can use automation.Manager.launch() to launch a new Logic 2 process - see
# the API documentation for more details.
# Using the `with` statement will automatically call manager.close() when exiting the scope. If you
# want to use `automation.Manager` outside of a `with` block, you will need to call `manager.close()` manually.
with automation.Manager.connect(port=10430) as manager:
    # Configure the capturing device to record on digital channels 0, 1, 2, and 3,
    # with a sampling rate of 10 MSa/s, and a logic level of 3.3V.
    # The settings chosen here will depend on your device's capabilities and what
    # you can configure in the Logic 2 UI.
    device_configuration = automation.LogicDeviceConfiguration(
        enabled_digital_channels=[0, 1, 2, 3],
        digital_sample_rate=10_000_000,
        digital_threshold_volts=3.3,
    )
    # Record 5 seconds of data before stopping the capture
    capture_configuration = automation.CaptureConfiguration(
        capture_mode=automation.TimedCaptureMode(duration_seconds=5.0)
    )
    # Start a capture - the capture will be automatically closed when leaving the `with` block
    # Note: The serial number 'F4241' is for the Logic Pro 16 demo device.
    #       To use a real device, you can:
    #         1. Omit the `device_id` argument. Logic 2 will choose the first real (non-simulated) device.
    #         2. Use the serial number for your device. See the "Finding the Serial Number
    #            of a Device" section for information on finding your device's serial number.
    with manager.start_capture(
            device_id='F4241',
            device_configuration=device_configuration,
            capture_configuration=capture_configuration) as capture:
        # Wait until the capture has finished
        # This will take about 5 seconds because we are using a timed capture mode
        capture.wait()
        # Add an analyzer to the capture
        # Note: The simulator output is not actual SPI data
        spi_analyzer = capture.add_analyzer('SPI', label=f'Test Analyzer', settings={
            'MISO': 0,
            'Clock': 1,
            'Enable': 2,
            'Bits per Transfer': '8 Bits per Transfer (Standard)'
        })
        # Store output in a timestamped directory
        output_dir = os.path.join(os.getcwd(), f'output-{datetime.now().strftime("%Y-%m-%d_%H-%M-%S")}')
        os.makedirs(output_dir)
        # Export analyzer data to a CSV file
        analyzer_export_filepath = os.path.join(output_dir, 'spi_export.csv')
        capture.export_data_table(
            filepath=analyzer_export_filepath,
            analyzers=[spi_analyzer]
        )
        # Export raw digital data to a CSV file
        capture.export_raw_data_csv(directory=output_dir, digital_channels=[0, 1, 2, 3])
        # Finally, save the capture to a file
        capture_filepath = os.path.join(output_dir, 'example_capture.sal')
        capture.save_capture(filepath=capture_filepath)


4. 尋找設備的序號 ID


若要尋找已連接裝置的序號,請開啟擷取資訊側邊欄,然後按一下右上角的裝置下拉清單:



然後點擊「設備資訊」,確認彈出的資訊欄,其中包含正在使用連接的設備的資訊與序號。建議從此處複製序號,並在需要「device_id」的 Python 腳本中做使用。



※ 更多近一步操作、疑難排解,可至 Using the Python Automation API 章節索引,查看其他步驟和說明。



結語:與使用者一起持續進化的 Saleae


在快速發展的電子工程和嵌入式系統開發領域,效率和精確度是關鍵。Saleae 公司的設計輕巧、高效能、支援多元協定,是世界上最受歡迎的邏輯分析儀之一。全球統計,每月有超過 20,000 名使用者,他們同時也在 Saleae 的官方論壇中交流,不斷挖掘 Saleae 儀器、軟體、語法的發展性。


Logic 2 的自動化 API 功能,是近年最受歡迎的更新功能之一;提供了高精準度的自動化流程,解決手動測試的負擔,讓工程師能夠專注於更具創新性的工作。今天我們簡單介紹這項功能,希望推廣更多人嘗試 Saleae 系列產品,以及幫助正在使用 Saleae 的使用者,更聰明地運用手中的工具。《翔宇科技》代理 Saleae 系列邏輯分析儀,如果您對產品有興趣,歡迎隨時與我們聯繫。




延伸閱讀:





其他參考資源:













190 次查看

Comments


歡迎 訂閱翔宇科技主題式電子報 >,您將可同步掌握最新的產業新訊以及技術文章。
bottom of page