Source code for fluidlab.interfaces.usbtmc_inter

"""Interface with Linux USBTMC (:mod:`fluidlab.interface.usbtmc_inter`)

This interface provides basic interface with USBTMC device. It uses the
linux devices at /dev/usbtmc[0-9], and thus makes use of the usbtmc
linux kernel support.
This is different from other modules, like python-usbtmc or pyvisa-py,
which disable the kernel usbtmc support and address the device directly.

This module is much simpler and merely rely on and ioctl.

Note: on some instruments, the USB port is actually a USB-serial converter.
In that case, use SerialInterface, not LinuxUSBTMCInterface.


.. autoclass:: USBTMCInterface


import os
from pathlib import Path

from fluidlab.interfaces import QueryInterface

[docs]class USBTMCInterface(QueryInterface): def __init__(self, device=0, **kwargs): """ Create a new USBTMCInterface. Input argument ============== device: Path('/dev/usbtmc0'), str or bytes '/dev/usbtmc0' or device number (e.g. 0) """ super().__init__() if isinstance(device, bytes): device = device.decode("ascii") if isinstance(device, Path): device = device.as_posix() if isinstance(device, int): device = f"/dev/usbtmc{device:d}" self.devname = device self.write_termination = b"\n" self.read_termination = b"\n" def __str__(self): return f'USBTMCInterface("{self.devame:}")' def __repr__(self): return str(self) def _open(self): =, os.O_RDWR) def _close(self): os.close( = None self.devname = None def _write(self, message, tracing=False, verbose=False): # On prend a priori des bytes, mais si on nous donne un str # on convertit à condition que ce soit du pur ascii if isinstance(message, str): message = message.encode("ascii") if not message.endswith(self.write_termination): message += self.write_termination os.write(, message) def readline(self, maxbytes=1024): msg =, maxbytes) if msg.endswith(self.read_termination): N = len(msg) - len(self.read_termination) + 1 msg = msg[:N] return msg def _read(self, maxbytes=1024, tracing=False, verbose=False): # Par compatibilité avec les autres interfaces, on renvoie # un str (à condition ascii). # Si c'est un appareil qui renvoie autre chose que de l'ascii # utiliser readline. return self.readline(maxbytes).decode("ascii")