nidaqmx Python API

repository·master·Indexed 20 days ago

https://github.com/ni/nidaqmx-python

The official National Instruments Python API for controlling NI data acquisition (DAQ) devices. It provides an object-oriented wrapper around the NI-DAQmx C API for instrumentation, acquisition, and control tasks. The library includes modules for managing tasks, physical and virtual channels, system hardware discovery, and gRPC session options for remote device server interaction.

Tokens
6.1K
Snippets
19
Records
38
Agent score
70%

What's inside nidaqmx

  1. Explore the nidaqmx.system module

    master

    The nidaqmx.system module provides a high-level interface for inspecting and interacting with the NI-DAQmx system configuration. It allows developers to programmatically discover hardware resources, such as devices and physical channels, and manage system-level entities like storage and watchdogs.

    Key sub-modules include:

    • collections: For managing groups of system objects.
    • device: For interacting with specific NI-DAQmx devices.
    • physical_channel: For accessing and configuring individual physical channels.
    • storage: For managing system storage resources.
    • watchdog: For interacting with system watchdog timers.
  2. What is a Task in NI-DAQmx?

    master

    A Task is a collection of one or more virtual channels with timing, triggering, and other properties. It serves as the primary container for managing acquisition or generation operations.

    import nidaqmx
    with nidaqmx.Task() as task:
        pass
  3. Use nidaqmx.task for DAQmx operations

    master

    The nidaqmx.task module is the primary interface for creating and managing NI-DAQmx tasks. A Task represents a collection of DAQmx resources (such as analog inputs, digital outputs, or counters) configured to perform a specific operation like acquisition or generation.

    Key areas of functionality within this module include:

    • Task Channels: Configuring the specific hardware channels (e.g., AI0, AO1) that participate in the task.
    • Task Collections: Managing groups of channels or resources.
    • Task Triggering: Setting up hardware or software triggers to control the timing of task execution.
  4. What are Virtual Channels?

    master

    Virtual channels (or simply channels) are software entities that encapsulate a physical channel along with specific configuration like range, terminal configuration, and custom scaling.

    A physical channel is a terminal or pin used to measure or generate signals. Every physical channel has a unique name following the NI-DAQmx naming convention (e.g., cDAQ1Mod4/ai0, Dev2/ao5, or Dev6/ctr3).

    import nidaqmx
    
    # Adding a single analog input voltage channel with a specific range
    with nidaqmx.Task() as task:
        task.ai_channels.add_ai_voltage_chan("Dev1/ai0", min_val=-10.0, max_val=10.0)
        data = task.read()
    
    # Adding multiple analog input voltage channels
    with nidaqmx.Task() as task:
        task.ai_channels.add_ai_voltage_chan("Dev1/ai0", min_val=-5.0, max_val=5.0)
        task.ai_channels.add_ai_voltage_chan("Dev1/ai1", min_val=-10.0, max_val=10.0)
        data = task.read()
  5. Hardware vs Software Timing

    master

    Timing controls when a signal is acquired or generated.

    • Hardware Timing: Uses a digital signal (like a device clock) to control the rate. It is more accurate and can run much faster than software loops.
    • Software Timing: The rate is determined by the software and operating system. It is generally less accurate than hardware timing.
    import nidaqmx
    from nidaqmx.constants import AcquisitionType, READ_ALL_AVAILABLE
    
    # Example of configuring hardware timing for a finite acquisition
    with nidaqmx.Task() as task:
        task.ai_channels.add_ai_voltage_chan("Dev1/ai0")
        task.timing.cfg_samp_clk_timing(1000.0, sample_mode=AcquisitionType.FINITE, samps_per_chan=10)
        data = task.read(READ_ALL_AVAILABLE)
  6. Access constants in nidaqmx.constants

    master
    The nidaqmx.constants module provides access to the various enumeration types and constant values required to configure and control NI-DAQmx tasks, channels, and hardware. These constants are used as arguments in API calls to specify modes, timing settings, acquisition types, and more. Instead of using raw integers, you should use these named constants to ensure code readability and correctness according to the NI-DAQmx driver specifications.
  7. Control session lifecycle with SessionInitializationBehavior

    master

    The SessionInitializationBehavior enum determines how the NI gRPC Device Server handles the driver session when a client connects. This behavior significantly impacts whether a session is closed when a nidaqmx.task.Task context manager exits.

    BehaviorDescriptionContext Manager Exit Behavior
    AUTOAttaches to an existing session if it exists; otherwise, initializes a new one.If a new session was created, it is closed. If it attached to an existing session, it detaches and leaves the session open.
    INITIALIZE_SERVER_SESSIONRequires the server to initialize a brand new session with the specified name.Automatically closes the server session.
    ATTACH_TO_SERVER_SESSIONRequires the server to attach to an existing session with the specified name.Detaches from the server session and leaves it open.
    from nidaqmx.grpc_session_options import SessionInitializationBehavior
    
    # Force a new session that will be closed on exit
    behavior = SessionInitializationBehavior.INITIALIZE_SERVER_SESSION
    
    # Force attachment to an existing session that stays open on exit
    behavior = SessionInitializationBehavior.ATTACH_TO_SERVER_SESSION
    
    # Default behavior
    behavior = SessionInitializationBehavior.AUTO
  8. Log data to a TDMS file

    master

    Technical Data Management Streaming (TDMS) is a binary format for high-speed data logging. You can configure a task to stream data directly from the device buffer to the hard disk using configure_logging.

    import nidaqmx
    from nidaqmx.constants import AcquisitionType, LoggingMode, LoggingOperation, READ_ALL_AVAILABLE
    
    with nidaqmx.Task() as task:
        task.ai_channels.add_ai_voltage_chan("Dev1/ai0")
        task.timing.cfg_samp_clk_timing(1000.0, sample_mode=AcquisitionType.FINITE, samps_per_chan=10)
        # Configure logging to a file
        task.in_stream.configure_logging(
            "TestData.tdms", 
            LoggingMode.LOG_AND_READ, 
            operation=LoggingOperation.CREATE_OR_REPLACE
        )
        data = task.read(READ_ALL_AVAILABLE)
  9. Manually install the NI-DAQmx driver

    master
    Visit ni.com/downloads to download the latest version of NI-DAQmx. While most 'Additional items' are optional, it is recommended to install the NI Certificates package to allow your OS to trust NI built binaries.
  10. Automatically install the NI-DAQmx driver

    master

    The nidaqmx module includes a CLI command to automate the installation of the NI-DAQmx driver.

    • Windows: Downloads and launches an online streaming installer from ni.com.
    • Linux: Downloads the repository registration package for your distribution and installs it via your package manager.
    python -m nidaqmx installdriver