""" ------------------------------------------------------------------------------------------- (C)2008 Nuria Pujol Vilanova USB comunication module for USB4000 Device (Ocean Optics) in Python. OpenDevice ResetDevice SentIntTime GetSample GPL License ------------------------------------------------------------------------------------------- """ import usb from pylab import* def OpenDevice(id=0x2457): #id= hexadecimal identifier """ --------------------------------------------------------------------------- OpenDevice(id=0x2457) DESCRIPTION: Return a HandleDevice varible related with a USB device that has the same 'id' identifier. (HandleDevice Object is the only able to write and read in USB comunication) ARGUMENTS: id -- hexadecimal idVendor identifier (default =0x2457) --------------------------------------------------------------------------- """ buses = usb.busses() for bus in buses : for device in bus.devices : if device.idVendor == id: print "USB4000 Device Found" dev=device elif device.idVendor == 0: #USB port without any device pass else: print "Other type of Device Found" handle= dev.open() return handle def ResetDevice(handle):#handle device """ --------------------------------------------------------------------------- ResetDevice(handle) DESCRIPTION: Reset the comunication. Non output. ARGUMENTS: handle - HandleDavice Object related to comunication to reset. Object returned by OpenDevice --------------------------------------------------------------------------- """ handle.reset() def SetIntTime(handle,itime): """ --------------------------------------------------------------------------- SetIntTime(handle,itime) DESCRIPTION: Write new integration time value on the device. Non output. (this function should be improved) ARGUMENTS: handle - HandleDavice Object related to device to set new integration time. Object returned by OpenDevice itime - New integration time in INT format. --------------------------------------------------------------------------- """ vector=[] while itime != 0: number=itime-((itime/2)*2) vector.append(number) itime = itime/2 vector = vector + ([0]*(32-len(vector))) vector.reverse() def elevat(x):return 2**x pot1=map(elevat,range(0,8)*4) pot1.reverse() buf=[vector[i]*pot1[i] for i in range (len(vector))] def sum(seq): def add(x,y):return x+y return reduce(add,seq,0) MSW_MSB=sum(buf[0:8])#Byte 4 MSB_LSB=sum(buf[8:16])#Byte 3 LSW_MSB=sum(buf[16:24])#Byte 2 LSW_LSB=sum(buf[24:32])#Byte 1 buf_tx=chr(0x02)+chr(LSW_LSB)+chr(LSW_MSB)+chr(MSB_LSB)+chr(MSW_MSB) o=handle.bulkWrite(0x01,buf_tx) def GetSample(handle,itime): """ --------------------------------------------------------------------------- GetSample(handle,itime) DESCRIPTION: Return a single adquisition in a vector (3840 values) ARGUMENTS: handle - HandleDavice Object related with the device to get samples from. itime - Integration time to use in sample adquisition --------------------------------------------------------------------------- """ command =chr(0x09) o=handle.bulkWrite(0x01,command) b0=handle.bulkRead(0x86,2048,itime) b4=handle.bulkRead(0x82,5632,itime) sincro=handle.bulkRead(0x82,1)#Non usefull data but has to be read data=b0+b4 data_LSB=[((256+data[2*i])&0xFF) for i in range(len(data)/2)] data_MSB=[((256+data[2*i+1])&0xFF)<<8 for i in range(len(data)/2)] sample=[data_LSB[i]^data_MSB[i] for i in range(len(data_LSB))] return sample def main(): """ --------------------------------------------------------------------------- DESCRIPTION Plot sample data obteined from USB4000 DEFAULT ARGUMENTS VALUES: handle - HandleDavice Object with idVendor 0x2456 itime - 4000us --------------------------------------------------------------------------- """ itime=400000 handle=OpenDevice() ResetDevice(handle) SetIntTime(handle,itime) lectura=GetSample(handle,itime) plot(lectura) show() if __name__ == '__main__': main()