Forum :
XScopes PC Interface
Topic :
control the scope with python, here are some code examples
The pyusb module allows to control xpotolab with python.
I tried with a xprotolab plain, a computer on Ubuntu and a Raspberry pi
I tried the scope mode only.
First, one need to install pyusb
https://github.com/walac/pyusb
https://github.com/walac/pyusb/blob/master/docs/tutorial.rst
libusb must also be installed, but, if Gabriel's program is running, it’s already done
One must be sure the scope is triggered. If it is not , a time out error will occur while trying to transfer the frame data
On Ubuntu or Raspberry, open python in super user mode.
it’s ready, here are the code examples
#first, import the modules
import sys
import usb.core
import usb.util
#find the scope
xscope = usb.core.find(idVendor=5840, idProduct=1785)
if xscope is None:
print'Xscope is not connected'
#find the number of configurations
print 'nomber of configurations : ',xscope.bNumConfigurations
cfg=xscope[0]
#find the number of interfaces and alternate settings
print "nomber of interfaces of config 0 : ",cfg.bNumInterfaces
intf=cfg[0,0]
print "nomber of alternate settings : ",intf.bAlternateSetting
#find the end points
print "nomber of end point : ",intf.bNumEndpoints
ep0=intf[0]
ep1=intf[1]
print "ep0 adress: ",ep0.bEndpointAddress
print "ep1 :adress ",ep1.bEndpointAddress
print "packet max of ep0 : ",ep0.wMaxPacketSize
print "packet max of ep1 : ",ep1.wMaxPacketSize
#set configuration before beeing able to communicate with the scope
xscope.set_configuration()
#for each frame, 12 paquets of 64 bytes and a paquet of 2 bytes (frame and index)
#get paquets using bulk transfer untill the frame and index are send
u=100
while u>2:
data = xscope.read(ep0.bEndpointAddress,ep0.wMaxPacketSize)
u = len(data)
#paquets will now correspond to a new frame
#get ch1 data
ch1=[]
for u in range (4):
#bulk transfer of 64 bytes
data = xscope.read(ep0.bEndpointAddress,ep0.wMaxPacketSize)
#add data to ch1
ch1.extend(data.tolist())
print 'ch1 = ', ch1
#get ch2 data
ch2=[]
for u in range (4):
data = xscope.read(ep0.bEndpointAddress,ep0.wMaxPacketSize)
ch2.extend(data.tolist())
print 'ch2 = ',ch2
#get chD data
chD=[]
for u in range (4):
data = xscope.read(ep0.bEndpointAddress,ep0.wMaxPacketSize)
chD.extend(data.tolist())
print 'chD = ',chD
#get frame, index data
data = xscope.read(ep0.bEndpointAddress,ep0.wMaxPacketSize)
frameindex=data.tolist()
print 'frame and index =', frameindex
#Now, one want to read and change the scope settings.
#
#commands are sent as CONTROL READ requests.
#commands are letters which can be found in the manual
#There are only two details one has to be carefull :
#first, do not use the command letters as string but use the ASCII value of the command letter.
#For example , to ask the firmware version, do not write ‘a’ but write ord(‘a’) or 0x61.
#Also, ask the exact number of bytes the scope is ready to send, no more, no less.
#To ask for firmware version :
data = xscope.ctrl_transfer(0xC0,0x61,0,0,4)
print data
v=data.tolist()
version = chr(v[0])+chr(v[1])+chr(v[2])+chr(v[3])
print version
#my version is 2,38
#to read registers : command is 'u', 44 bytes are send to computer
data = xscope.ctrl_transfer(0xC0,ord('u'),0,0,44)
print data
#mind, in older devices, there may be only 43 bytes
#to restore defaults , command is ‘k’
xscope.ctrl_transfer(0xC0,ord('k'),0,0,0)
#to write the settings, command is 'b' and 0 byte is send to computer,
#for example
#to set channel A gain (index 12 ) to level 3 :
xscope.ctrl_transfer(0xC0,ord('b'),3,12,0)
#to set channel B gain (index 13 ) to level 5 :
xscope.ctrl_transfer(0xC0,ord('b'),5,13,0)
#to set timescale (index 0 ) to level 5 :
xscope.ctrl_transfer(0xC0,ord('b'),5,0,0)
p { margin-bottom: 0.25cm; line-height: 120%; }a:link { } |