# # Interface for VISA devices through tty # # import os, threading, termios class VISA: def __init__(self, tty_name, out_sink, readLen=100): self.tty = os.open(tty_name, os.O_RDWR) self.addr = None self.out = out_sink # Function that takes a string self.read_len = readLen self.startThread() self.setAddress(0) def startThread(self): # To run read in parallel with write self.thread = threading.Thread(target=self.read) self.thread.daemon = True self.thread.start() def read(self): while (True): self.out( os.read(self.tty, self.read_len) ) def cmd(self, cmd_str): os.write(self.tty, cmd_str + "\n") # I tried to fix this with the following call, but it did not change anything # termios.tcflush(self.tty, termios.TCIOFLUSH) def setAddress(self, a): if self.addr == a: return False self.cmd("++addr %s" % a) self.addr = a return True def getVersion(self): return self.cmd("++ver") ############# You can ignore the next two classes ########## ####### The part that executes is below them ######## class Device: def __init__(self, controller, address): self.cont = controller self.addr = address def cmd(self, cmd_str): self.cont.setAddress(self.addr) self.cont.cmd(cmd_str) class PowerSupply(Device): def reset(self): self.cmd("RST?") ########### Executing part ########### def printOut(out): print(out) v = VISA("/dev/ttyUSB0", printOut) v.cmd("++addr") v.cmd("++ver")