Guest User

VISA.py

a guest
Jan 2nd, 2014
1,297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.67 KB | None | 0 0
  1. #
  2. # Interface for VISA devices through tty
  3. #
  4. #
  5. import os, threading, termios
  6.  
  7. class VISA:
  8.     def __init__(self, tty_name, out_sink, readLen=100):
  9.         self.tty = os.open(tty_name, os.O_RDWR)
  10.         self.addr = None
  11.         self.out = out_sink  # Function that takes a string
  12.         self.read_len = readLen
  13.         self.startThread()
  14.         self.setAddress(0)
  15.  
  16.     def startThread(self): # To run read in parallel with write
  17.         self.thread = threading.Thread(target=self.read)
  18.         self.thread.daemon = True
  19.         self.thread.start()
  20.  
  21.     def read(self):
  22.         while (True):
  23.             self.out( os.read(self.tty, self.read_len) )    
  24.  
  25.     def cmd(self, cmd_str):
  26.         os.write(self.tty, cmd_str + "\n")
  27.     # I tried to fix this with the following call, but it did not change anything
  28.         # termios.tcflush(self.tty, termios.TCIOFLUSH)
  29.  
  30.     def setAddress(self, a):
  31.         if self.addr == a:
  32.             return False
  33.         self.cmd("++addr %s" % a)
  34.         self.addr = a
  35.         return True
  36.  
  37.     def getVersion(self):
  38.         return self.cmd("++ver")
  39.  
  40. ############# You can ignore the next two classes ##########
  41. ####### The part that executes is below them ########
  42.  
  43. class Device:
  44.     def __init__(self, controller, address):
  45.         self.cont = controller
  46.         self.addr = address
  47.  
  48.     def cmd(self, cmd_str):
  49.         self.cont.setAddress(self.addr)
  50.         self.cont.cmd(cmd_str)
  51.  
  52.    
  53.  
  54. class PowerSupply(Device):
  55.     def reset(self):
  56.         self.cmd("RST?")
  57.  
  58. ########### Executing part ###########
  59.  
  60. def printOut(out):
  61.     print(out)
  62.  
  63.  
  64. v = VISA("/dev/ttyUSB0", printOut)
  65. v.cmd("++addr")
  66. v.cmd("++ver")
Add Comment
Please, Sign In to add comment