Repository navigation
Add a per-machine VISA backend and the NI USB driver - #12
Merged
Merged
Conversation
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
force-pushed
the
stack/10-visa-backend-and-driver
branch
from
November 16, 2026 18:33
3676cb4 to
efce3d2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_usbdrives NI GPIB-USB adapters from user space over libusb (MIT).--check-visadiagnoses an installed build, and the frozen backend ships pyusb and libusb.The driver is clean-room: written from
docs/design/ni_usb_gpib_protocol.mdonly, by an implementer with no network and no GPL sources. Reviewers should start fromdocs/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 isphase0/reviewable-baselinecut into reviewable pieces; the last PR's tree is that branch's tree.Type of change
Hardware testing
GPIB-USB-HS + Keithley 2400 on macOS, 2026-09-18: identify, resistance runs, stop, restart, shutdown.
Test suite
QT_QPA_PLATFORM=offscreen pytest tests/ -vpasses locally — 1137 passed at this PR's headHow to verify:
Checklist
README.md/ version history if user-visible — the changelog's Unreleased section covers the whole stack and lands in PR 21