""" ------------------------------------------------------------------------------------------- (C)2008 Nuria Pujol Vilanova FILE NAME: OOS4000.py DESCRIPTION: USB comunication module for USB4000 Device (Ocean Optics) in Python. - OpenDevice - ResetDevice - SentIntTime - GetSample - MaxSample - GetReducedSample OS4000.py file improvement according new scenario (S4000AppPc.py) GPL License ------------------------------------------------------------------------------------------- """ import sys, os,usb from struct import pack,unpack from numpy import* from time import time from pylab import plot,show def OpenDevice(id=0x2457): #id=Hex identificator """ ----------------------------------------------------------------------------------------- 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: 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,buf_tx): """ ----------------------------------------------------------------------------------------- SetIntTime(handle,buf_tx) DESCRIPTION: Write new integration time value on the device. Non output. ARGUMENTS: handle -- HandleDavice Object related to device to set new integration time. Object returned by OpenDevice buf_tx -- New integration time in vendor descrived format. *buf_tx= chr(0x02) + struct.pack('i', IntegrationTimeValue) buf_tx obtained from S4000AppPc.py execution ----------------------------------------------------------------------------------------- """ o=handle.bulkWrite(0x01,buf_tx) def GetSample(buf_tx,itime,id=0x2457): """ ----------------------------------------------------------------------------------------- GetSample(buf_tx,itime,id=0x257) DESCRIPTION: Return a single adquisition in a vector (3840 values) ARGUMENTS: buf_tx -- New integration time in vendor descrived format. itime -- Integration time in INT format id -- Hexadecimal idVendor identifier (default =0x2457) *buf_tx= chr(0x02) + struct.pack('i', IntegrationTimeValue) buf_tx obtained from S4000AppPc.py execution ------------------------------------------------------------------------------------------ """ #---------------------------------------------------------------------------------------- #-- 'itime' is only used to calculate time that USB buffer have #-- to wait. #-- Provably, only 'buf_tx' will be enough, buf_tx can be easully #-- translated to itime (to improve)* #---------------------------------------------------------------------------------------- handle=OpenDevice(id) #ResetDevice(handle) SetIntTime(handle,buf_tx) buffer=chr(0x09) o=handle.bulkWrite(0x01,buffer) SampleTime=int(time()) #----------------------------------------------------------------------------------------- #-- 'sampletime' is when sample is 'taken' in seconds (UTC format) #-- more or less to be stored in netCDFFile if is necessary #----------------------------------------------------------------------------------------- b0=handle.bulkRead(0x86,2048,itime/7) b4=handle.bulkRead(0x82,5632,itime/11) sincro=handle.bulkRead(0x82,1) #'sincro' not util data but has to be readed, if not it remains in buffer data=b0+b4 #'data' is util data readed from USB4000 #---------------------------------------------------------------------------------------- #-- Every spectrometric data (for every photodiode) has 2 bytes and #-- become from USB4000 in this format: #-- #-- [LSB photodiode1,MSB phot1,LSB phot2,MSB phot2,...] #-- #-- 'SpecSignature' contain LSB and MSB combined getting values #-- from '0' (ideally, always there is some noise) to 32000 that can be #-- represented graphically #---------------------------------------------------------------------------------------- data=[x for x in data] dataLSB=[((256+data[2*i])&0xFF) for i in range(len(data)/2)] dataMSB=[((256+data[2*i+1])&0xFF)<<8 for i in range(len(data)/2)] SpecSignature=[dataLSB[i]^dataMSB[i] for i in range(len(dataLSB))] return SpecSignature,SampleTime #---------------------------------------------------------------------------------------- #--'SpecSignature can be represented on Pc screen but not in PDA #-- (there are more points (values) than screen pixels. #-- For this reasons we have to decrease the numer of SpecSignature #-- values (we loose resolution) but we can still recognize signal #-- obteined. #-- Entire SpecSignature will be saved on a database in netCDF format #-- (if this option is selected on PDA) and this reduced sample sent #-- to PDA to be represented #---------------------------------------------------------------------------------------- def MaxSample(SpecSignature): """ ----------------------------------------------------------------------------------------- MaxSample(SpecSignature) DESCRIPTION: Return maximum value of a sample ARGUMENTS: SpecSignature -- One single sample values vector ---- """ length =len(SpecSignature) maximum=max(SpecSignature[2:length]) return maximum def GetReducedSample(SpecSignature,screenV=120,screenH=240): """ ----------------------------------------------------------------------------------------- GetReducedSample(SpecSignature,screenV=100,screenH=240) DESCRIPTION: From entire sample obteined from USB4000 (SpecSignature) obtain a 'reduced' sample able to be 'drawn' on PDA screen size ARGUMENTS: SpecSignature -- One single sample values vector screenV -- number of able PDA screen pixels on vertical screenH -- number of able PDA screen pixels on horizontal ---- """ resolution=int(len(SpecSignature)/screenH) short_signature=[] maximum=MaxSample(SpecSignature) for x in range(screenH): short_signature.append(screenV-((SpecSignature[x*resolution]*screenV)/maximum)) #---------------------------------------------------------------------------------------- #-- Samples values ajustements according to screen size #-- (screenH x screenV in pixels) #---------------------------------------------------------------------------------------- return short_signature def main(): itime=300000 buf_tx=chr(0x02)+pack('i',itime) [lectura,time]=GetSample(buf_tx,itime) plot(lectura) show() if __name__ == '__main__': main()