usbtmc.py
55 lines
| 1.4 KiB
| text/x-python
|
PythonLexer
/ lppinstru / usbtmc.py
|
r1 | #!/usr/bin/env python | ||
#-*- coding: utf-8 -*- | ||||
"""Simple python library to communicate over USB-TMC protocol with linux's | ||||
usbtmc module. | ||||
""" | ||||
import time | ||||
import sys | ||||
import os | ||||
import glob | ||||
import re | ||||
__author__ = "Alexis Jeandet" | ||||
__copyright__ = "Copyright 2015, Laboratory of Plasma Physics" | ||||
__credits__ = [] | ||||
__license__ = "GPLv2" | ||||
__version__ = "1.0.0" | ||||
__maintainer__ = "Alexis Jeandet" | ||||
__email__ = "alexis.jeandet@member.fsf.org" | ||||
__status__ = "Development" | ||||
def find_instrument(ref,serial=""): | ||||
instruments=glob.glob("/dev/usbtmc[0-9]") | ||||
p = re.compile(ref) | ||||
for instrument in instruments: | ||||
dev=UsbTmc(instrument) | ||||
idn=dev.idn().split(",") | ||||
if p.match(idn[1]): | ||||
if serial=="" or serial == idn[2]: | ||||
return instrument | ||||
return "" | ||||
|
r3 | class UsbTmc(object): | ||
|
r1 | def __init__(self,dev): | ||
self.__PATH__=dev | ||||
self.__FILE__ = os.open(dev, os.O_RDWR) | ||||
if self.__FILE__==-1: | ||||
raise UserWarning("can't open "+dev) | ||||
self.Manufacturer="" | ||||
self.Reference="" | ||||
self.Serial="" | ||||
self.Version="" | ||||
def write(self, command): | ||||
os.write(self.__FILE__, command); | ||||
def read(self, length = 4000): | ||||
return os.read(self.__FILE__, length) | ||||
def idn(self): | ||||
self.write("*IDN?") | ||||
return self.read(100) | ||||
def __str__(self): | ||||
return self.idn() +"\n"+ self.__PATH__ | ||||