Hello everyone:

I wanted to share the code i wrote to communicate with a tektronix
scope MSO2024. I used [0] and [1] as reference. I'm using PyUSB,
libusb 1.0 and Fedora 16

I wrote to main functions. One for opening the scope:

def tekopen(idV=0x0699,idP=0x03A4):
  dev=usbc.find(idVendor=idV,idProduct=idP)
  if dev is None:
    raise ValueError('No se encontro el osciloscopio')
  if dev.is_kernel_driver_active(0):
    dev.detach_kernel_driver(0)
  dev.set_configuration()
  usbt.claim_interface(dev,0)
  return dev

idV and idP can be obtained connecting the scope and then using "lsusb"

The second function is for sending commands or queries to the scope:

def tekcq(dev,comd,oendp=0x01,iendp=0x82):
  n=1
  ncomp=n^0xFF
  mlen=len(comd)
  fiveb=np.divide(mlen,256)
  fourb=np.mod(mlen,256)
  abytes=''
  if np.mod(mlen,4)!=0:
    nabytes=4-np.mod(mlen+12,4)
    for x in range(0,nabytes):
      abytes+=struct.pack('1B',0)
  header=struct.pack('12B',1,n,ncomp,0,fourb,fiveb,0,0,1,0,0,0)
  msg=header+comd+abytes
  if comd[-1]!='?':
    dev.write(oendp,msg,0,100)
  else:
    n=2
    ncomp=n^0xFF
    inr=struct.pack('12B',2,n,ncomp,0,0,1,0,0,0,0,0,0)
    dev.write(oendp,msg,0,100)
    dev.write(oendp,inr,0,100)
    if comd!=':curv?':
      res=dev.read(iendp,256,0,1000)[12:]
      res=res.tostring()
      return res
    else:
      res=dev.read(iendp,512,0,1000)
      fend=res[8]
      res=res[12:]
      data=np.array(res,dtype=np.uint8)
      while fend!=1:
        n+=1
        ncomp=n^0xFF
        inr=struct.pack('12B',2,n,ncomp,0,0,1,0,0,0,0,0,0)
        dev.write(oendp,inr,0,100)
        res=dev.read(iendp,512,0,1000)
        fend=res[8]
        res=res[12:]
        data=np.hstack((data,res))
      return data

If we want to send a command to scope we use:

tekcq(tek,':head off')

If we want to send a query:

a=tekcq(tek,':hor:sca?')

One special case is when we want to query waveform data from the
scope, in that case we use:

data=tekcq(tek,':curv?')

Hope is helpful for someone.

[0] http://comments.gmane.org/gmane.comp.python.pyusb.user/477

[1] http://www1.tek.com/forum/viewtopic.php?f=5&t=4389


-- 
Marcos Anzorena

------------------------------------------------------------------------------
Get 100% visibility into Java/.NET code with AppDynamics Lite!
It's a free troubleshooting tool designed for production.
Get down to code-level detail for bottlenecks, with <2% overhead. 
Download for free and get started troubleshooting in minutes. 
http://pubads.g.doubleclick.net/gampad/clk?id=48897031&iu=/4140/ostg.clktrk
_______________________________________________
pyusb-users mailing list
pyusb-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/pyusb-users

Reply via email to