pylibiio

0.25 · active · verified Thu Apr 16

pylibiio is the Python binding for the libiio library, which provides a robust interface for interacting with Linux Industrial I/O (IIO) devices. These devices include a wide range of ADCs, DACs, accelerometers, gyros, and various sensors. The library facilitates both local communication on embedded Linux targets and remote communication over USB, Ethernet, or Serial, and is actively maintained by Analog Devices, Inc. The current stable version available on PyPI is 0.25, with frequent updates to the underlying C library.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to establish a connection to an IIO context (local or remote), scan for available devices and their channels, and perform a basic buffered read operation. It includes error handling for common connection issues.

import iio
import os

# Try to connect to a local or remote IIO context
# Use 'local' for devices on the same machine, or 'ip:address' for remote
# For testing without hardware, you can often use 'dummy'
# The URI can also be 'usb', 'serial', or an IP address (e.g., 'ip:192.168.1.100')
# For robust examples, use an environment variable or default to local
uri = os.environ.get('IIO_URI', 'local') 

try:
    ctx = iio.Context(uri)
    print(f"Connected to IIO context: {ctx.description}")

    # Scan for available devices
    print("\nAvailable IIO devices:")
    for dev in ctx.devices:
        print(f"  - Device: {dev.name} (ID: {dev.id})")
        for channel in dev.channels:
            print(f"    - Channel: {channel.name} (ID: {channel.id}, Output: {channel.output})")
            # Example: Try to read an attribute if it exists
            try:
                sample_rate = channel.attrs['sampling_frequency'].value
                print(f"      Sampling Frequency: {sample_rate}")
            except KeyError:
                pass
            except Exception as e:
                print(f"      Error reading attribute: {e}")

    # Example: Find a specific device (replace with a known device on your system)
    # For a dummy device, you might use 'iio:device0'
    target_device_name = os.environ.get('IIO_TARGET_DEVICE', 'iio:device0') 
    device = ctx.find_device(target_device_name)
    if device:
        print(f"\nFound target device: {device.name}")
        # Example: Set a device attribute (e.g., gain, if supported)
        # try:
        #     if 'gain_control' in device.attrs:
        #         device.attrs['gain_control'].value = 'manual'
        #         print(f"Set gain_control on {device.name}")
        # except KeyError:
        #     pass
        # except Exception as e:
        #     print(f"Error setting device attribute: {e}")

        # Example: Enable a channel and create a buffer to read data
        input_channel = device.find_channel('voltage0', is_output=False)
        if input_channel:
            input_channel.enabled = True
            print(f"Enabled channel: {input_channel.name}")
            buffer = iio.Buffer(device, 4096) # 4096 samples
            print(f"Created buffer for {device.name}")
            # Read data (replace with actual processing)
            data = buffer.read()
            print(f"Read {len(data)} bytes from buffer.")
            buffer.close()
        else:
            print(f"Channel 'voltage0' not found on {device.name}.")
    else:
        print(f"Target device '{target_device_name}' not found.")

    ctx.close()
    print("\nDisconnected from IIO context.")

except Exception as e:
    print(f"Failed to connect to IIO context or interact with devices: {e}")
    print("Ensure the libiio C library is installed and IIO devices are available/accessible.")

view raw JSON →