Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Version 1.0a, 29 Jun 13
- """
- import usb.core
- import usb.util
- import sys, array
- class Scope:
- #class variables
- #constructor
- def __init__(self, VID, PID, EP_OUT=0x01, EP_IN=0x86):
- self.__vendorId = VID
- self.__productId = PID
- self.__dev = []
- self.bTag = 0x01
- #static methods
- #instance method
- def findDevice(self):
- # find our device
- # idVendor=0x1ab1,idProduct=0x04b0
- #print ("looking for VID: " + self.__vendorId + \
- #" PID: " + self.__productId)
- self.__dev = usb.core.find(idVendor=self.__vendorId, idProduct=self.__productId)
- # was it found?
- if self.__dev is None:
- return False
- # set the active configuration. With no arguments, the first
- # configuration will be the active one
- self.__dev.set_configuration()
- return True
- def write(self, SCPI_command = ""):
- """
- *************************************************************
- Perform a message write
- *************************************************************
- """
- #message ID 1=DEV_DEP_MSG_OUT, 2=REQUEST_DEV_DEP_MSG_IN
- MsgID = 0x01
- # sequential message identifier
- self.bTag += 1
- if (self.bTag > 255):
- self.bTag = 0
- bTagInverse = (self.bTag ^ 0xFF)
- command = bytearray(SCPI_command, 'latin1')
- txSize_0 = (len(command) & 0xFF)
- txSize_1 = (len(command)>>8 & 0xFF)
- txSize_2 = (len(command)>>16 & 0xFF)
- txSize_3 = (len(command)>>24 & 0xFF)
- EOM = 0x01
- bReserved = 0x00
- frame = [MsgID, self.bTag, bTagInverse, bReserved, txSize_0, txSize_1, \
- txSize_2, txSize_3, EOM, bReserved, bReserved, bReserved]
- # add the command
- frame += command
- # pad the frame to 4 byte (32bit) chunks
- while ((len(frame) % 4) > 0):
- frame += [0x00]
- ret = self.__dev.write(0x02, frame)
- def read(self, no_of_bytes = 0, timeout_ms=100):
- """
- *************************************************************
- Write the read back request giving the device the ability to
- write back to the BULK-IN endpoint
- *************************************************************
- """
- #message ID 1=DEV_DEP_MSG_OUT, 2=REQUEST_DEV_DEP_MSG_IN
- MsgID = 0x02
- # sequential message identifier
- self.bTag += 1
- if (self.bTag > 255):
- self.bTag = 0
- bTagInverse = (self.bTag ^ 0xFF)
- rxSize_0 = (no_of_bytes & 0xFF)
- rxSize_1 = (no_of_bytes>>8 & 0xFF)
- rxSize_2 = (no_of_bytes>>16 & 0xFF)
- rxSize_3 = (no_of_bytes>>24 & 0xFF)
- bReserved = 0x00
- #last two bytes are termination chars not supported by the rigol and must be 0
- frame = [MsgID, self.bTag, bTagInverse, bReserved, rxSize_0, rxSize_1, \
- rxSize_2, rxSize_3, 0x00, 0x00]
- # pad the frame to 4 byte (32bit) chunks
- while ((len(frame) % 4) > 0):
- frame += [0x00]
- ret = self.__dev.write(0x02, frame)
- """
- Try and read the response back from the device
- """
- ret = self.__dev.read(0x86, no_of_bytes + 13, 0, timeout_ms)
- return ret
- def trim_response(self, response):
- _response = response
- data_byte_count = _response[4]+(_response[5]<<8)+\
- (_response[6]<<16)+(_response[7]<<24)
- #remove the header section
- for x in range(0,12):
- del _response[0]
- #remove all padding from the end
- while len(_response) > data_byte_count:
- del _response[-1]
- return _response
- def close(self):
- #reset the usb bus
- self.__dev.reset()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement