Skip to content

Add a per-machine VISA backend and the NI USB driver - #12

Merged
PEEKPerformer merged 15 commits into
stack/09-cifrom
stack/10-visa-backend-and-driver
Nov 16, 2026
Merged

PEEKPerformer merged 15 commits into
stack/09-cifrom
stack/10-visa-backend-and-driver

Conversation

@PEEKPerformer

@PEEKPerformer PEEKPerformer commented Sep 22, 2026 •

Copy link
Copy Markdown
Owner

Summary

Which VISA implementation opens the bus becomes a machine-local setting (visa_library: automatic, vendor VISA, pyvisa-py), because on a Mac an installed NI-VISA has no GPIB driver behind it. Behind pyvisa-py, resistamet_gui/gpib_usb drives NI GPIB-USB adapters from user space over libusb (MIT). --check-visa diagnoses an installed build, and the frozen backend ships pyusb and libusb.

The driver is clean-room: written from docs/design/ni_usb_gpib_protocol.md only, by an implementer with no network and no GPL sources. Reviewers should start from docs/design/ni_usb_gpib_clean_room.md, which records who touched what.

Stack

Part 10 of 28. Targets stack/09-ci; PR 11 targets this branch. Merge bottom-up with Rebase and merge so the commits and their messages survive. The stack is phase0/reviewable-baseline cut into reviewable pieces; the last PR's tree is that branch's tree.

Type of change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing behavior to change)
  • Documentation only
  • CI / build / packaging only

Hardware testing

  • Tested against real hardware
  • Tested against software-only path
  • Not applicable

GPIB-USB-HS + Keithley 2400 on macOS, 2026-09-18: identify, resistance runs, stop, restart, shutdown.

Test suite

  • QT_QPA_PLATFORM=offscreen pytest tests/ -v passes locally — 1137 passed at this PR's head
  • Added new tests for new behavior, or explained why none are needed

How to verify:

QT_QPA_PLATFORM=offscreen pytest tests/ -v
python -m resistamet_gui.api --check-visa

Checklist

  • My changes follow the existing code style
  • I have updated README.md / version history if user-visible — the changelog's Unreleased section covers the whole stack and lands in PR 21
  • I have read the CONTRIBUTING guidelines

get/set_gpib_address become thin wrappers over get/set_machine_local so a
second per-machine key can be added without touching four code paths.
Behaviour unchanged; existing TestMachineLocalGpib covers it.
pyvisa picks the vendor library when one is installed, which is wrong on a
Mac with NI-VISA but no GPIB driver behind it: the instrument is reachable
only through pyvisa-py there. A machine-local visa_library setting ('' =
pyvisa's default, '@ivi', '@py', or a library path) now reaches every
ResourceManager the app opens, through one function in visa_backend.py.

GET /instruments/resources takes an optional visa_library to try a backend
before saving it and reports which implementation answered; POST
/instruments/identify accepts the same. Runs read the value from the
resolved settings like the address.
A select above the address (Automatic / Vendor VISA / pyvisa-py). Scan and
Identify use the selection before it is saved, and the section says which
implementation actually answered. Identify & save stores both machine-local
values.
The specification an MIT implementation of the adapter can be written from
without reading the GPL drivers: devices, attach sequence, bulk framing,
status block, every operation, addressing, timeouts, pitfalls, provenance
with source hashes. Written by an agent that read the sources and reviewed
by a second one for anything that was expression rather than fact; the
record explains the roles.
NI stopped shipping a GPIB driver for macOS (NI-488.2 21.5.1 does not load
on macOS 13+). This drives the GPIB-USB-HS family over libusb instead:
tables and codec for the adapter's USB protocol, a transport over pyusb, a
controller that owns the sequencing rules, device-level operations, a board
registry, and a pyvisa-py session so GPIB0::24::INSTR opens through it when
an NI adapter is present and through whatever pyvisa-py had otherwise.

Written from docs/design/ni_usb_gpib_protocol.md alone, by an agent that
never saw the GPL drivers (docs/design/ni_usb_gpib_clean_room.md). Tested
against scripted and fake transports only: every worked hex example in the
specification byte for byte, attach in order, write/read/command/serial
poll, timeouts and the stop request, malformed-reply resync, handle
disposal. Not yet run against a real adapter.
visa_backend runs its pyvisa-py extensions for '@py' and for the automatic
choice, which pyvisa resolves to pyvisa-py when no vendor library is
installed; the NI GPIB-USB driver registers itself as one. Installing is
idempotent, touches no bus, and is a no-op without pyusb and libusb.
pyusb (BSD) joins requirements and a 'usb' extra. On macOS and Linux the
spec bundles the libusb shared library and its LGPL-2.1 text beside the
executable, where gpib_usb.transport looks first; the desktop workflow
installs libusb on the macOS runner. Windows keeps NI-VISA and bundles
nothing: the adapter belongs to NI's driver there.
…n later

A fault marked the controller for re-attach, but attach() cleared that mark
before doing the work: if the re-attach then failed on the readiness poll or
a bus error, every later operation raised 'adapter is not attached' without
touching USB until the resource was reopened. The mark is now cleared only
by an attach that succeeds, and the stop-and-drain of a fault during the
re-attach is tracked separately so it still runs exactly once.

BoardRegistry.owns() looks at the bus again when it does not recognise a
board, so an adapter plugged in after the first enumeration is reachable
without a scan first; before, the miss fell through to pyvisa-py and
surfaced on a Mac as 'please install linux-gpib'.
enable_simulation patches pyvisa.ResourceManager process-wide, which is right
for a --simulate run and wrong for a test session: the e2e fixtures left the
fake bound for every later test, so anything checking which VISA backend
answered saw the fake instead. disable_simulation puts the original back and
an autouse fixture calls it, so a new fixture cannot forget.
Python 3.9 resolves pyvisa-py 0.7, whose Session API predates what
visa_session.py is written against, and the test module failed to import
there. install() already caught that ImportError and registered nothing;
the tests now assert it, and the session tests skip on such a run rather
than erroring at collection.
'The app sees no instruments' has one cause the app cannot report: a VISA
library that loads but has no driver behind it (NI-VISA without NI-488.2 on
the lab laptop, or a Mac where NI ships no GPIB driver at all). The sidecar
now prints which implementation it resolved, its version, whether the NI USB
driver loaded libusb and which one, and optionally what the bus enumerates,
as one JSON line; exit 1 when nothing opened.
The macOS job asserts the bundled libusb loads from inside the bundle, which
is the only thing that makes the NI GPIB-USB driver usable in a packaged app.
Distribution metadata for pyvisa, pyvisa-py and pyusb ships too, so the
diagnostic reports real versions instead of 'unknown'.
@PEEKPerformer
PEEKPerformer force-pushed the stack/10-visa-backend-and-driver branch from 3676cb4 to efce3d2 Compare November 16, 2026 18:33
@PEEKPerformer
PEEKPerformer merged commit 43a7c7d into stack/09-ci Nov 16, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant