from saleae import automation
import os
import os.path
from datetime import datetime
import threading

# 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, 4, 5],
        digital_sample_rate=40_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=60)
    )

    # Start a capture - the capture will be automatically closed when leaving the `with` block
    # Note: We are using serial number 'F4241' here, which is the serial number for
    #       the Logic Pro 16 demo device. You can remove the device_id and the first physical
    #       device found will be used, or you can use your device's serial number.
    #       See the "Finding the Serial Number of a Device" section for information on finding your
    #       device's serial number.

    # for i in range(60): 循环采集60次

    threads = []
    for i in range(60):
        print(f'starting capture {i}...')

        capture = manager.start_capture(
            device_id='A3E22C8E845D2C7D',
            device_configuration=device_configuration,
            capture_configuration=capture_configuration)

        # Wait until the capture has finished
        # This will take about 5 seconds because we are using a timed capture mode
        capture.wait()

        def _worker(cap):
            # Store output in a timestamped directory
            output_dir = os.path.join(
                os.getcwd(), f'output-{datetime.now().strftime("%Y-%m-%d_%H-%M-%S")}-{i}')
            os.makedirs(output_dir)

            # Export raw digital data to a CSV file
            cap.export_raw_data_csv(
                directory=output_dir, digital_channels=[0, 1, 2, 3,4,5])

            # Finally, save the capture to a file
            capture_filepath = os.path.join(output_dir, 'example_capture.sal')
            cap.save_capture(filepath=capture_filepath)

            cap.close()
            pass

        thread = threading.Thread(target=_worker, args=(capture, ))
        threads.append(thread)
        thread.start()
    for th in threads:
        th.join()
    input("按回车键退出...")
最后修改:2024 年 04 月 10 日
如果觉得我的文章对你有用,请随意赞赏