SHARE
TWEET

rigol.py

a guest Jul 2nd, 2013 580 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. """
  2. Version 1.0a, 29 Jun 13
  3. """
  4.  
  5. import usb.core
  6. import usb.util
  7. import sys, array
  8.  
  9. class Scope:
  10.     #class variables
  11.  
  12.  
  13.     #constructor
  14.     def __init__(self, VID, PID, EP_OUT=0x01, EP_IN=0x86):
  15.         self.__vendorId = VID
  16.         self.__productId = PID
  17.         self.__dev = []
  18.         self.bTag = 0x01
  19.  
  20.     #static methods
  21.  
  22.     #instance method
  23.     def findDevice(self):
  24.         # find our device
  25.         # idVendor=0x1ab1,idProduct=0x04b0
  26.         #print ("looking for VID: " + self.__vendorId + \
  27.         #" PID: " + self.__productId)
  28.         self.__dev = usb.core.find(idVendor=self.__vendorId, idProduct=self.__productId)
  29.         # was it found?
  30.         if self.__dev is None:
  31.             return False
  32.  
  33.  
  34.         # set the active configuration. With no arguments, the first
  35.         # configuration will be the active one
  36.         self.__dev.set_configuration()
  37.         return True
  38.  
  39.     def write(self, SCPI_command = ""):
  40.         """
  41.         *************************************************************
  42.         Perform a message write
  43.         *************************************************************
  44.         """
  45.         #message ID 1=DEV_DEP_MSG_OUT, 2=REQUEST_DEV_DEP_MSG_IN
  46.         MsgID = 0x01
  47.         # sequential message identifier
  48.         self.bTag += 1
  49.         if (self.bTag > 255):
  50.             self.bTag = 0
  51.         bTagInverse = (self.bTag ^ 0xFF)
  52.         command = bytearray(SCPI_command, 'latin1')
  53.         txSize_0 = (len(command) & 0xFF)
  54.         txSize_1 = (len(command)>>8 & 0xFF)
  55.         txSize_2 = (len(command)>>16 & 0xFF)
  56.         txSize_3 = (len(command)>>24 & 0xFF)
  57.         EOM = 0x01
  58.         bReserved = 0x00
  59.  
  60.         frame = [MsgID, self.bTag, bTagInverse, bReserved, txSize_0, txSize_1, \
  61.          txSize_2, txSize_3, EOM, bReserved, bReserved, bReserved]
  62.  
  63.         # add the command
  64.         frame += command
  65.  
  66.         # pad the frame to 4 byte (32bit) chunks
  67.         while ((len(frame) % 4) > 0):
  68.                 frame += [0x00]
  69.  
  70.         ret = self.__dev.write(0x02, frame)
  71.  
  72.     def read(self, no_of_bytes = 0, timeout_ms=100):
  73.         """
  74.         *************************************************************
  75.         Write the read back request giving the device the ability to
  76.         write back to the BULK-IN endpoint
  77.         *************************************************************
  78.         """
  79.         #message ID 1=DEV_DEP_MSG_OUT, 2=REQUEST_DEV_DEP_MSG_IN
  80.         MsgID = 0x02
  81.         # sequential message identifier
  82.         self.bTag += 1
  83.         if (self.bTag > 255):
  84.             self.bTag = 0
  85.         bTagInverse = (self.bTag ^ 0xFF)
  86.         rxSize_0 = (no_of_bytes & 0xFF)
  87.         rxSize_1 = (no_of_bytes>>8 & 0xFF)
  88.         rxSize_2 = (no_of_bytes>>16 & 0xFF)
  89.         rxSize_3 = (no_of_bytes>>24 & 0xFF)
  90.         bReserved = 0x00
  91.  
  92.         #last two bytes are termination chars not supported by the rigol and must be 0
  93.         frame = [MsgID, self.bTag, bTagInverse, bReserved, rxSize_0, rxSize_1, \
  94.          rxSize_2, rxSize_3, 0x00, 0x00]
  95.  
  96.         # pad the frame to 4 byte (32bit) chunks
  97.         while ((len(frame) % 4) > 0):
  98.                 frame += [0x00]
  99.  
  100.         ret = self.__dev.write(0x02, frame)
  101.  
  102.         """
  103.         Try and read the response back from the device
  104.         """
  105.         ret = self.__dev.read(0x86, no_of_bytes + 13, 0, timeout_ms)
  106.  
  107.         return ret
  108.  
  109.     def trim_response(self, response):
  110.         _response = response
  111.         data_byte_count = _response[4]+(_response[5]<<8)+\
  112.         (_response[6]<<16)+(_response[7]<<24)
  113.  
  114.         #remove the header section
  115.         for x in range(0,12):
  116.             del _response[0]
  117.  
  118.         #remove all padding from the end
  119.         while len(_response) > data_byte_count:
  120.             del _response[-1]
  121.  
  122.         return _response
  123.  
  124.     def close(self):
  125.         #reset the usb bus
  126.         self.__dev.reset()
RAW Paste Data
Top