### Install tm_devices Dependencies (Console) Source: https://github.com/tektronix/tm_devices/blob/main/CONTRIBUTING.md Installs the project dependencies and sets up pre-commit hooks for local development. This script automates the virtual environment setup and package installation. ```console python scripts/contributor_setup.py ``` -------------------------------- ### Add Devices with Environment Variables Source: https://github.com/tektronix/tm_devices/blob/main/docs/basic_usage.md Illustrates configuring device connections using environment variables. This is a common practice for automating device setups outside of Python scripts. ```python # fmt: off --8<-- "examples/miscellaneous/adding_devices_with_env_var.py" ``` -------------------------------- ### Install Project Dependencies (Python/Poetry) Source: https://github.com/tektronix/tm_devices/blob/main/CONTRIBUTING.md Installs and configures project dependencies using pip and poetry, and sets up pre-commit hooks. This ensures the development environment is correctly configured. ```python python -m pip install -U pip poetry poetry install pre-commit install ``` -------------------------------- ### Setting up Virtual Environment and Installing Dependencies for PyInstaller Source: https://github.com/tektronix/tm_devices/blob/main/docs/troubleshooting/usage.md Commands to create and activate a Python virtual environment, and install `tm_devices`, `pyinstaller`, and a specific version of `tomli` to resolve packaging issues. ```bash python -m venv .venv .venv\Scripts\activate # Windows # or source .venv/bin/activate # Linux/Mac pip install tm_devices pyinstaller tomli==2.0.1 ``` -------------------------------- ### Use Device Aliases Source: https://github.com/tektronix/tm_devices/blob/main/docs/basic_usage.md Demonstrates how to assign custom alias names to devices and reference them by these aliases. This simplifies device management in code. ```python # fmt: off --8<-- "examples/miscellaneous/alias_usage.py" ``` -------------------------------- ### Example `main.py` for `tm_devices` PyInstaller Test Source: https://github.com/tektronix/tm_devices/blob/main/docs/troubleshooting/usage.md A simple Python script demonstrating the usage of `DeviceManager` from the `tm_devices` library, suitable for testing PyInstaller packaging. ```python from tm_devices import DeviceManager with DeviceManager() as device_manager: scope = device_manager.add_scope("192.168.0.1") print(scope) ``` -------------------------------- ### Install tm_devices Package Source: https://github.com/tektronix/tm_devices/blob/main/README.md This command installs the tm_devices Python package using pip. Ensure you have pip installed and accessible in your environment. ```shell pip install tm_devices ``` -------------------------------- ### Select VISA Backend Source: https://github.com/tektronix/tm_devices/blob/main/docs/basic_usage.md Configures the DeviceManager to use specific VISA backends from different implementations. This allows flexibility in choosing the underlying VISA library. ```python # fmt: off --8<-- "examples/miscellaneous/visa_connection_selectivity.py" ``` -------------------------------- ### Save Screenshot to Local Machine Source: https://github.com/tektronix/tm_devices/blob/main/docs/basic_usage.md Demonstrates how to capture a screenshot from a device and save it to the local machine. This requires the device driver to inherit from `ScreenCaptureMixin`. ```python # fmt: off --8<-- "examples/scopes/tekscope/save_screenshot.py" ``` -------------------------------- ### Add Devices via Python Code Source: https://github.com/tektronix/tm_devices/blob/main/docs/basic_usage.md Demonstrates how to configure device connections programmatically within Python code. This method is useful for dynamic configuration within scripts. It requires the 'tm_devices' library. ```python # fmt: off --8<-- "examples/miscellaneous/adding_devices.py" ``` -------------------------------- ### List Available VISA Devices Source: https://github.com/tektronix/tm_devices/blob/main/docs/basic_usage.md Prints available VISA devices to the console. This is a common utility for identifying connected instruments. ```console $ list-visa-resources [ "TCPIP0::192.168.0.1::inst0::INSTR", "ASRL4::INSTR" ] ``` -------------------------------- ### Running PyInstaller with `tm_devices` Metadata Source: https://github.com/tektronix/tm_devices/blob/main/docs/troubleshooting/usage.md Example command for running PyInstaller to create a single-file executable, ensuring `tm_devices` package metadata is included using the `--copy-metadata` flag. ```bash pyinstaller --onefile main.py --copy-metadata=tm_devices ``` -------------------------------- ### Get Device by Alias in Python Source: https://github.com/tektronix/tm_devices/blob/main/docs/configuration.md This Python code shows how to retrieve a pre-configured device from the Device Manager using its alias. This method is recommended to avoid relying on device declaration order when working with multiple devices of the same type. ```python from tm_devices import DeviceManager with DeviceManager() as device_manager: my_device = device_manager.get_device(alias="custom_alias") ``` -------------------------------- ### Add Custom Device Support with Python Source: https://github.com/tektronix/tm_devices/blob/main/docs/basic_usage.md Explains how to add support for devices not natively handled by `tm_devices`. This involves creating a custom device class and providing a mapping for the `DeviceManager` to recognize and instantiate it. ```python # fmt: off --8<-- "examples/miscellaneous/custom_device_driver_support.py" ``` -------------------------------- ### Access PyVISA Resource Directly with Python Source: https://github.com/tektronix/tm_devices/blob/main/docs/basic_usage.md Demonstrates how to directly access the PyVISA resource object when specific actions are not yet available through tm_devices drivers. This is useful for advanced control or when working with features not abstracted by the package. ```python # fmt: off --8<-- "examples/miscellaneous/pyvisa_resource_access.py" ``` -------------------------------- ### Save and Recall Waveform and Session Source: https://github.com/tektronix/tm_devices/blob/main/docs/basic_usage.md Shows how to save a waveform or the entire scope session to an external file for later recall. This is useful for preserving instrument states and waveforms. ```python # fmt: off --8<-- "examples/scopes/tekscope/basic_save_recall.py" ``` -------------------------------- ### YAML Configuration for TM Devices Options Source: https://github.com/tektronix/tm_devices/blob/main/docs/configuration.md This snippet shows the YAML syntax for configuring various runtime options for TM Devices. It includes settings for verbosity, connection handling, timeouts, update checks, and logging. ```yaml options: verbose_mode: false verbose_visa: false standalone: false setup_cleanup: false teardown_cleanup: false disable_command_verification: false retry_visa_connection: false default_visa_timeout: 5000 check_for_updates: false log_console_level: INFO log_file_level: DEBUG log_file_directory: ./logs log_file_name: tm_devices_.log log_colored_output: false log_pyvisa_messages: false log_uncaught_exceptions: true ``` -------------------------------- ### Sample tm_devices YAML Configuration Source: https://github.com/tektronix/tm_devices/blob/main/docs/configuration.md This snippet demonstrates a sample YAML configuration file for the tm_devices project. It shows how to define various device connections, including TCPIP by hostname and IP, HiSLIP, SOCKET connections with specified ports, and USB connections. This configuration is used to set up and manage connections to test and measurement instruments. ```yaml #########################[tm_devices.yaml]######################### devices: # TCPIP connection to SCOPE using hostname - address: MSO54-123456 alias: my_scope_by_hostname device_type: SCOPE # TCPIP connection to SMU using IP - address: 123.45.67.255 alias: my_smu_by_ip connection_type: TCPIP device_type: SMU # TCPIP connection to SCOPE using the High Speed LAN Instrument Protocol (HiSLIP) - address: MSO54-123456 alias: my_scope_on_hislip0 device_type: SCOPE lan_device_name: hislip0 # SOCKET connection to SCOPE types needs lan_port number - address: 123.45.67.255 alias: my_scope_port4000 connection_type: SOCKET device_type: SCOPE lan_port: 4000 # USB connection to SCOPE using hostname - address: MSO54-123456 alias: my_scope connection_type: USB device_type: SCOPE ``` -------------------------------- ### Add DPOJET Measurements and Plots Source: https://github.com/tektronix/tm_devices/blob/main/docs/basic_usage.md Demonstrates how to add DPOJET measurements and plots to compatible Tektronix oscilloscopes (e.g., DPO70KSX/C/7KC/DPO5KB). Measurement reports can be saved as PDFs. ```python # fmt: off --8<-- "examples/scopes/tekscope_70k/dpojet/adding_dpojet_measurements.py" ``` -------------------------------- ### Configure Measurement on Single Sequence Source: https://github.com/tektronix/tm_devices/blob/main/docs/basic_usage.md Configures a scope for a measurement on a single acquisition by setting acquisition parameters and adding the desired measurement on a selected channel. ```python # fmt: off --8<-- "examples/scopes/tekscope/get_acquisition_data.py" ``` -------------------------------- ### Access Mainframe and PSU Module Source: https://github.com/tektronix/tm_devices/blob/main/docs/basic_usage.md Shows how to connect to a mainframe and access its modules, specifically a Power Supply Unit (PSU) module, using the DeviceManager. This is useful for controlling modular instrument systems. ```python # fmt: off --8<-- "examples/mainframes/psu_modules/access_mainframe_and_psu_module.py" ``` -------------------------------- ### Read Dynamic Buffers from SMUs with Python Source: https://github.com/tektronix/tm_devices/blob/main/docs/basic_usage.md Shows how to create and read from a dynamic buffer, specifically for Source Measure Units (SMUs). This is essential for applications requiring real-time data acquisition or complex measurement sequences. ```python # fmt: off --8<-- "examples/source_measure_units/2600/reading_dynamic_buffers.py" ``` -------------------------------- ### Disable Command Verification Source: https://github.com/tektronix/tm_devices/blob/main/docs/basic_usage.md Shows how to disable the extra query that verifies property settings. This can be done at the device or device manager level to potentially speed up operations. ```python # fmt: off --8<-- "examples/miscellaneous/disable_command_verification.py" ``` -------------------------------- ### Add Devices to Device Manager in Python Source: https://github.com/tektronix/tm_devices/blob/main/docs/configuration.md This Python code snippet demonstrates how to instantiate the Device Manager and add devices directly within the Python script. It allows for runtime configuration changes and is useful for dynamic device management. ```python # fmt: off --8<-- "examples/miscellaneous/adding_devices.py" ``` -------------------------------- ### Customize Logging and Console Output Source: https://github.com/tektronix/tm_devices/blob/main/docs/basic_usage.md Explains how to customize logging levels and console output verbosity. This can be configured in Python code, a config file, or via environment variables. Note that Python code configuration takes precedence. ```python # fmt: off --8<-- "examples/miscellaneous/customize_logging.py" ``` -------------------------------- ### Perform Curve Query and Save to CSV Source: https://github.com/tektronix/tm_devices/blob/main/docs/basic_usage.md Performs a curve query on a scope and saves the results to a CSV file. This requires an Arbitrary Function Generator (AFG) connected to channel 1 of the scope. ```python # fmt: off --8<-- "examples/scopes/tekscope/basic_curve_query.py" ``` -------------------------------- ### Generate Internal AFG Signal Source: https://github.com/tektronix/tm_devices/blob/main/docs/basic_usage.md Generates a signal using the oscilloscope's Internal Arbitrary Function Generator (AFG). Requires a specific scope model and license, and the AFG output connected to CH1. ```python # fmt: off --8<-- "examples/scopes/tekscope/generate_internal_afg_signal.py" ``` -------------------------------- ### YAML Device Configuration Syntax Source: https://github.com/tektronix/tm_devices/blob/main/docs/configuration.md This YAML structure defines how to configure devices for the Device Manager. It includes parameters for address, alias, connection type, device type, and specific settings for TCPIP, USB, SOCKET, SERIAL, GPIB, and REST_API connections. ```yaml devices: - address: alias: connection_type: device_type: # TCPIP connection - address: alias: connection_type: TCPIP device_type: lan_device_name: # USB connection - address: - alias: connection_type: USB device_type: # SOCKET connection - address: alias: connection_type: SOCKET device_type: lan_port: # SERIAL connection - address: alias: connection_type: SERIAL device_type: serial_config: baud_rate: 9600 data_bits: 8 flow_control: xon_xoff parity: none end_input: none # GPIB connection - address: gpib_board_number: alias: connection_type: GPIB device_type: # REST API connection - address: alias: connection_type: REST_API device_type: lan_port: device_driver: ``` -------------------------------- ### Register Device Manager for Program Termination with Python Source: https://github.com/tektronix/tm_devices/blob/main/docs/basic_usage.md Provides a method to ensure the `DeviceManager` is closed upon program termination, particularly when it cannot be used as a context manager. This prevents resource leaks by guaranteeing cleanup. ```python # fmt: off --8<-- "examples/miscellaneous/register_dm_atexit.py" ``` -------------------------------- ### Load Devices from YAML Configuration File Source: https://context7.com/tektronix/tm_devices/llms.txt Loads device connections and configurations from a YAML file using the DeviceManager. Once loaded, devices can be accessed by their alias, simplifying the process of managing multiple instruments in a test setup. Dependencies include the 'tm_devices' library and a valid YAML configuration file. ```python from tm_devices import DeviceManager # Load devices from config file (./tm_devices.yaml) with DeviceManager() as dm: # Devices automatically connected from config scope = dm.get_device(alias="main_scope") awg = dm.get_device(alias="signal_gen") psu = dm.get_device(alias="power_supply") dmm = dm.get_device(alias="gpib_dmm") # Use devices print(scope.query("*IDN?")) print(awg.query("*IDN?")) ``` -------------------------------- ### Serve Documentation Locally (MkDocs) Source: https://github.com/tektronix/tm_devices/blob/main/CONTRIBUTING.md Serves the built documentation locally using MkDocs, enabling previewing of the documentation changes. The --clean and --no-livereload flags ensure a fresh build and prevent automatic reloading. ```console mkdocs serve --clean --no-livereload ``` -------------------------------- ### Build Documentation (Tox) Source: https://github.com/tektronix/tm_devices/blob/main/CONTRIBUTING.md Builds the project's documentation using Tox. This command generates the documentation files, which can then be served locally. ```console tox -e docs ``` -------------------------------- ### Serve Documentation Locally (Python HTTP Server) Source: https://github.com/tektronix/tm_devices/blob/main/CONTRIBUTING.md Serves the built documentation from the '.results_docs' directory using Python's built-in HTTP server. This is an alternative method to serve documentation locally after building it with Tox. ```python python -m http.server -d .results_docs ``` -------------------------------- ### Run Full Project Validation (Tox) Source: https://github.com/tektronix/tm_devices/blob/main/CONTRIBUTING.md Executes comprehensive static code analysis, tests, and documentation validation using Tox. This is a slow but thorough check recommended before submitting changes. ```console tox -p ``` -------------------------------- ### Run Pre-commit Checks (Console) Source: https://github.com/tektronix/tm_devices/blob/main/CONTRIBUTING.md Executes only the pre-commit hooks to verify code formatting and style compliance. This is a fast check for adherence to project standards. ```console pre-commit run --all ``` -------------------------------- ### Run Basic Project Validation (Tox) Source: https://github.com/tektronix/tm_devices/blob/main/CONTRIBUTING.md Runs basic static code analysis and tests using Tox. This is a much quicker way to check code quality and test coverage. ```console tox -p -m basic ``` -------------------------------- ### PyVISA-sim Resource Definition for `tm_devices` Source: https://github.com/tektronix/tm_devices/blob/main/docs/troubleshooting/usage.md Defines the correct YAML format for PyVISA-sim resource definitions to ensure `tm_devices` can connect to simulated devices. Resource names must be in all caps. ```yaml spec: '1.1' resources: TCPIP::DEVICE-HOSTNAME::INSTR: device: device 1 ``` -------------------------------- ### Control Command Verification in DeviceManager (Python) Source: https://context7.com/tektronix/tm_devices/llms.txt This example shows how to manage command verification settings within the DeviceManager. It demonstrates disabling verification globally, enabling it for a specific device, and then disabling it again for performance. Command verification ensures commands are executed correctly by sending an additional query. ```python from tm_devices import DeviceManager with DeviceManager() as dm: # Disable verification for all devices dm.command_verification_enabled = False scope = dm.add_scope("192.168.0.1") # Enable verification for specific device scope.command_verification_enabled = True # This command will be verified (extra query sent) scope.set_and_check(":HORIZONTAL:SCALE", 100e-9) # Disable verification for faster execution scope.command_verification_enabled = False scope.write(":HORIZONTAL:SCALE 50e-9") # No verification query ``` -------------------------------- ### Create Virtual Environment (Python) Source: https://github.com/tektronix/tm_devices/blob/main/CONTRIBUTING.md Manually creates a Python virtual environment named '.env' for project isolation. This is an alternative to the helper script for setting up the development environment. ```python python -m venv .env ``` -------------------------------- ### Debugging YAML for Test Device Configuration Source: https://github.com/tektronix/tm_devices/blob/main/docs/troubleshooting/contributions.md This snippet illustrates a common misconfiguration in `tests/sim_devices/devices.yaml` where the 'device' key does not match an entry in the specified device YAML file. This can lead to VISA connection errors and test failures. Ensure the 'device' value correctly references a device defined in the 'devices' list within the file specified by the 'filename' key. ```yaml # tests/sim_devices/devices.yaml resources: TCPIP::PSU2281-HOSTNAME::INSTR: device: psu2282 filename: psu/psu2281.yaml ``` ```yaml # tests/sim_devices/psu/psu2281.yaml spec: '1.1' devices: psu2281: dialogues: - q: '*IDN?' r: KEITHLEY INSTRUMENTS INC.,MODEL 2281S-20-6,01234567,01.00 ``` -------------------------------- ### List Available VISA Resources via CLI and Programmatically Source: https://context7.com/tektronix/tm_devices/llms.txt Discover connected VISA devices using the `list-visa-resources` command-line tool or the `print_available_visa_devices` Python function. This helps in identifying instrument connection strings for use with the DeviceManager. Supports various connection types like TCPIP, ASRL, and USB. ```bash # Command line tool $ list-visa-resources [ "TCPIP0::192.168.0.1::inst0::INSTR", "TCPIP0::192.168.0.2::inst0::INSTR", "ASRL4::INSTR", "USB0::0x0699::0x1234::C012345::INSTR" ] ``` ```python from tm_devices import print_available_visa_devices # Programmatic access to VISA resources print_available_visa_devices() ``` -------------------------------- ### Define Base Class for New Device Series Source: https://github.com/tektronix/tm_devices/blob/main/docs/contributing/add_new_driver.md This Python code defines the base class for a new device series (e.g., Fancy PSU). It inherits from an abstracted device type and uses decorators to mark it as a family base class. This sets up the foundation for specific drivers within this series, requiring them to implement defined abstract functions. ```python ### drivers/power_supplies/new_series_psu/fancy_power_supply.py """Fancy PSU Base device driver for the new series/family of fancy power supplies.""" from abc import ABC from tm_devices.drivers.power_supplies.power_supply import PowerSupplyUnit from tm_devices.drivers.device import family_base_class @family_base_class # Mark the base class for the new family of devices class BaseFancyPSU(PowerSupplyUnit, ABC): """Base Fancy PSU device driver.""" # Abstracted functions for specific driver implementation goes here. ``` -------------------------------- ### Run Tests Only (Tox) Source: https://github.com/tektronix/tm_devices/blob/main/CONTRIBUTING.md Executes only the test suite using Tox. This is useful for quickly verifying the correctness of code changes without running broader analysis. ```console tox -e tests ``` -------------------------------- ### Run pytest with absolute HTML path (Windows) Source: https://github.com/tektronix/tm_devices/blob/main/docs/troubleshooting/contributions.md This command activates the virtual environment and runs pytest, specifying an absolute path for the HTML report using the --html flag and the current directory (`%CD%`). This resolves FileNotFoundError issues caused by relative paths in certain pytest-html versions. ```bat .venv\Scripts\activate.bat pytest -k "test_docs" --self-contained-html --html=%CD%\.results_doctests\results.html ``` -------------------------------- ### Activate Virtual Environment (Shell) Source: https://github.com/tektronix/tm_devices/blob/main/CONTRIBUTING.md Activates the Python virtual environment. This command differs based on the operating system (Linux/macOS vs. Windows). Once activated, all subsequent Python commands will use the environment's interpreter and packages. ```shell # Linux source .venv/bin/activate ``` ```shell # Windows .venv\Scripts\activate.bat ``` -------------------------------- ### Implement Specific Driver Inheriting from Base Series Class Source: https://github.com/tektronix/tm_devices/blob/main/docs/contributing/add_new_driver.md This Python code demonstrates creating a specific device driver class (e.g., FancyPSU123) that inherits from a previously defined base class for a device series (BaseFancyPSU). It resides in a dedicated subpackage for the series and allows for specific driver implementations. ```python ### drivers/ower_supplies/new_series_psu/fancy_psu_123.py """BaseFancyPSU device driver.""" from tm_devices.drivers.power_supplies.new_series_psu.fancy_power_supply import ( BaseFancyPSU, ) class FancyPSU123(BaseFancyPSU): """Fancy PSU 123 device driver.""" # Specific driver implementation goes here. ``` -------------------------------- ### Run pytest with absolute HTML path (Linux) Source: https://github.com/tektronix/tm_devices/blob/main/docs/troubleshooting/contributions.md This command activates the virtual environment and runs pytest, specifying an absolute path for the HTML report using the --html flag and the current working directory (`pwd`). This prevents FileNotFoundError caused by relative path issues in some pytest-html versions. ```bash source .venv/bin/activate pytest -k "test_docs" --self-contained-html --html=$(pwd)/.results_doctests/results.html ``` -------------------------------- ### Create Development Branch (Git) Source: https://github.com/tektronix/tm_devices/blob/main/CONTRIBUTING.md Creates a new Git branch for local development, allowing changes to be made in isolation from the main codebase. This is a standard practice for feature development or bug fixes. ```git git checkout -b name-of-your-bugfix-or-feature ``` -------------------------------- ### Configure DeviceManager with DMConfigOptions and VISA Backends Source: https://context7.com/tektronix/tm_devices/llms.txt Configure DeviceManager behavior using DMConfigOptions for setup/teardown cleanup, verbosity, and specifying VISA backends. This allows fine-grained control over instrument reset and error checking. Supports programmatic configuration before instantiation. ```python from tm_devices import DeviceManager from tm_devices.helpers import DMConfigOptions, PYVISA_PY_BACKEND # Configure options before instantiation config = DMConfigOptions( setup_cleanup=True, teardown_cleanup=True, verbose_mode=True, verbose_visa=False ) with DeviceManager(config_options=config) as dm: # Select VISA backend dm.visa_library = PYVISA_PY_BACKEND # Enable/disable cleanup operations dm.setup_cleanup_enabled = True dm.teardown_cleanup_enabled = True # Add scope with automatic reset on connection and close scope = dm.add_scope("192.168.0.1") scope.write("*RST") scope.expect_esr(0) # Verify no errors after reset ```