53 lines
1.6 KiB
Python
53 lines
1.6 KiB
Python
#!/usr/bin/env python3
|
|
# connect_dsx2014a_pyvisa.py
|
|
import pyvisa
|
|
import usb.core, usb.util
|
|
|
|
VID, PID = 0x0957, 0x1798 # Keysight DSOX2014A
|
|
SERIAL = "MY59124583" # from your lsusb/PyUSB output
|
|
|
|
def detach_kernel_tmc(vid, pid):
|
|
dev = usb.core.find(idVendor=vid, idProduct=pid)
|
|
if not dev:
|
|
return
|
|
try:
|
|
for cfg in dev:
|
|
for intf in cfg:
|
|
if intf.bInterfaceClass == 0xFE: # USBTMC
|
|
if dev.is_kernel_driver_active(intf.bInterfaceNumber):
|
|
dev.detach_kernel_driver(intf.bInterfaceNumber)
|
|
try:
|
|
dev.set_configuration()
|
|
except Exception:
|
|
pass
|
|
except Exception:
|
|
pass
|
|
|
|
def main():
|
|
# If the kernel usbtmc module is bound, libusb can't talk. Detach it:
|
|
detach_kernel_tmc(VID, PID)
|
|
|
|
rm = pyvisa.ResourceManager("@py") # pyvisa-py (libusb) backend
|
|
candidates = [
|
|
f"USB0::{VID}::{PID}::{SERIAL}::INSTR",
|
|
f"USB::{VID}::{PID}::{SERIAL}::INSTR",
|
|
f"USB0::0x{VID:04x}::0x{PID:04x}::{SERIAL}::INSTR",
|
|
f"USB::0x{VID:04x}::0x{PID:04x}::{SERIAL}::INSTR",
|
|
]
|
|
|
|
for addr in candidates:
|
|
try:
|
|
inst = rm.open_resource(addr)
|
|
inst.timeout = 5000
|
|
inst.read_termination = "\n"
|
|
inst.write_termination = "\n"
|
|
print("Connected:", addr, "->", inst.query("*IDN?").strip())
|
|
inst.close()
|
|
break
|
|
except Exception as e:
|
|
print(addr, "->", e)
|
|
rm.close()
|
|
|
|
if __name__ == "__main__":
|
|
main()
|