Skip to main content

A minimal, readable USBTMC implementation for learning and simple use cases

Project description

usbtmc-lite

Minimal USBTMC (USB Test and Measurement Class) implementation for Python.

Communicate with USBTMC-compliant instruments via USB. Lightweight design with IEEE 488.2 block data transfer support.

Features

  • Simple API for instrument communication (write/read/query)
  • IEEE 488.2 block data transfer
  • Status byte register access
  • Keysight firmware mode unlock utility

Installation

pip install usbtmc-lite

Windows

Requires libusb-1.0.dll. Download from libusb releases.

Linux

sudo apt install libusb-1.0-0

You may need to configure udev rules:

# /etc/udev/rules.d/99-usbtmc.rules
SUBSYSTEM=="usb", ATTR{idVendor}=="0957", MODE="0666"

Quick Start

from usbtmc_lite import USBTMC
from usbtmc_lite.utils import find_usbtmc_devices, get_device, get_backend

backend = get_backend("./libusb-1.0.dll")  # Linux/macOS: get_backend()
devices = find_usbtmc_devices(backend=backend)
device = get_device(devices, vendor_id=0x0957, product_id=0x0618)

with USBTMC() as usbtmc:
    usbtmc.open(device)
    print(usbtmc.query("*IDN?"))

Examples

Basic Communication

from usbtmc_lite import USBTMC
from usbtmc_lite.utils import find_usbtmc_devices, get_device, get_backend

backend = get_backend("./libusb-1.0.dll")
devices = find_usbtmc_devices(backend=backend)
device = get_device(devices, vendor_id=0x0957, product_id=0x0618)

with USBTMC() as usbtmc:
    usbtmc.open(device)
    usbtmc.write("*RST")
    print(usbtmc.query("*IDN?"))

Multiple Identical Devices

from usbtmc_lite import USBTMC
from usbtmc_lite.utils import find_usbtmc_devices, get_device, get_backend

backend = get_backend()
devices = find_usbtmc_devices(backend=backend)

# Use serial number to distinguish
device = get_device(devices, 0x0957, 0x0618, serial_number="MY12345678")

with USBTMC() as usbtmc:
    usbtmc.open(device)
    print(usbtmc.query("*IDN?"))

Block Data Transfer

from usbtmc_lite import USBTMC
from usbtmc_lite.utils import find_usbtmc_devices, get_device, get_backend

backend = get_backend()
devices = find_usbtmc_devices(backend=backend)
device = get_device(devices, 0x0957, 0x0618)

with USBTMC() as usbtmc:
    usbtmc.open(device)
    
    # Upload waveform data
    usbtmc.write_block_data(":DATA:UPLOAD", waveform_bytes)
    
    # Download waveform data
    usbtmc.write(":DATA:DOWNLOAD?")
    data = usbtmc.read_block_data(timeout=10000)

Unlock Keysight Firmware Mode

Some Keysight USB modular instruments power on in firmware update mode:

from usbtmc_lite.utils import (
    get_backend,
    find_firmware_mode_devices,
    unlock_keysight_device,
    find_usbtmc_devices,
)
import time

backend = get_backend("./libusb-1.0.dll")

fw_devices = find_firmware_mode_devices(backend=backend)
if fw_devices:
    print(f"Found {len(fw_devices)} device(s) in firmware mode")
    unlock_keysight_device(fw_devices[0])
    time.sleep(3)  # Wait for USB re-enumeration

devices = find_usbtmc_devices(backend=backend)

API Reference

USBTMC

Method Description
open(device) Open connection to USB device
close() Close connection
write(text) Send text command
write_bytes(data) Send raw bytes
read(length, timeout) Read text response
read_bytes(length, timeout) Read raw bytes
query(text, timeout) Write and read
write_block_data(command, data) Write IEEE 488.2 block data
read_block_data(timeout) Read IEEE 488.2 block data
clear() Clear device buffers
read_status_byte_register() Read IEEE 488.2 status byte
Property Description
opened True if device is connected
device Underlying PyUSB device object
terminator_write Write terminator (default: \n)
terminator_read Read terminator (default: \n)

Utilities

Function Description
get_backend(dll_path=None) Get libusb backend
find_usbtmc_devices(backend, vendor_id, product_id) Find USBTMC devices
get_device(devices, vendor_id, product_id, serial_number) Get specific device
find_firmware_mode_devices(backend, vendor_id) Find devices in firmware update mode
unlock_keysight_device(device) Switch Keysight device to USBTMC mode

License

Apache License 2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

usbtmc_lite-0.1.2.tar.gz (20.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

usbtmc_lite-0.1.2-py3-none-any.whl (18.5 kB view details)

Uploaded Python 3

File details

Details for the file usbtmc_lite-0.1.2.tar.gz.

File metadata

  • Download URL: usbtmc_lite-0.1.2.tar.gz
  • Upload date:
  • Size: 20.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for usbtmc_lite-0.1.2.tar.gz
Algorithm Hash digest
SHA256 689ed3a8e4e8dcdaa2edc3f720c55ea6192ee7aeb70c180f6f9c7640dd57d39d
MD5 50e7e5a7cceae48a77ebb0655c4b7989
BLAKE2b-256 ed3ef44c4129404e1e37a1f2a14b06a39ceab1c3c9c4cc0326daae5d3c324850

See more details on using hashes here.

Provenance

The following attestation bundles were made for usbtmc_lite-0.1.2.tar.gz:

Publisher: python-publish.yml on NaoNaoMe/usbtmc-lite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file usbtmc_lite-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: usbtmc_lite-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 18.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for usbtmc_lite-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 340c857ef4699fa27df94bf28c43fd0557f2d0a5aa1b309136b0fea41fb6658a
MD5 5fadf6172573f82cb492586b7b8aed86
BLAKE2b-256 50971cda7dc77e4dbc8c82b1509da4a0570f5dc3c3b375bf7dde3d59c5bbbe0d

See more details on using hashes here.

Provenance

The following attestation bundles were made for usbtmc_lite-0.1.2-py3-none-any.whl:

Publisher: python-publish.yml on NaoNaoMe/usbtmc-lite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page