Python Forum
PyUSB Problem: Failed Commands and Timeout Without Data
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PyUSB Problem: Failed Commands and Timeout Without Data
#1
Hi all,
 
I've never posted before, so I apologize for any quirks or mistakes.

I am using the PyUSB 1.0 module with libusb 1.0 for a backend, Python 2.7, on an embedded Linux system running Debian 7.0 (Wheezy). To describe my issue, I think that I must provide a little background about my project. I am using PyUSB to facilitate USB control of  a spectrum analyzer (i.e., a Spectran HF-60105 V4 X) using the device's API. The embedded computer and spectrum analyzer are a part of a field deployable system, and as such, the system must be able to operate at temperatures below freezing (but of course, above the documented temperature specifications for all hardware). This is where I run into problems. Specifically, my code works without a hitch when it is warm outside, but it seems to run into an issue when it's operating in cooler temperatures (i.e., around freezing). To be clear, I cannot draw a definitive connection between function and temperature, but the correlation is obvious and consistent. Below is a snippet of command line output from the Python script when it is warm outside (i.e., room temperature), and all is working fine. The hex that follows each API command is the device's response. The "verify" response is unique, but the response to all other commands is a generic "/x31/x60/x21/x00", which confirms reception by the device.
 
Verify.....................SUCCESSFUL. /x31/x60/x01/x51/x1a/xf5/xaf
Initial boot calib.........SUCCESSFUL. /x31/x60/x21/x00
Preamplifier...............ON. /x31/x60/x21/x00
Attenuation................OFF. /x31/x60/x21/x00
Start frequency........... 99.5 MHz. /x31/x60/x21/x00
Stop frequency............ 101.5 MHz. /x31/x60/x21/x00
RBW....................... 1000 Hz. /x31/x60/x21/x00
Sweep delay accuracy.......ON. /x31/x60/x21/x00
Sweep reset................SUCCESSFUL. /x31/x60/x21/x00
 
However, when the system is operating in cooler temperatures, near freezing, the device's responses appear to be delayed. Basically, it appears that the
device.read() commands are timing out without a response at first, but the device's responses seem to be queued up in a buffer, because eventually multiple "OK" responses are received by the computer. In the example shown below, all ends up just fine, because the commands eventually are received by the analyzer. However, this doesn't always happen, so this glitch is currently a showstopper for my system. Here's output from the system when it's around freezing:
 
Verify.....................FAILED. /x31/x60
Verify.....................SUCCESSFUL. /x31/x60/x01/x51/x1a/xf5/xaf/x01/x51/x1a/xf5/xaf
Initial boot calib.........FAILED. /x31/x60
Initial boot calib.........FAILED. /x31/x60
Initial boot calib.........FAILED. /x31/x60
Initial boot calib.........SUCCESSFUL. /x31/x60/x21/x00/x21/x00/x21/x00/x21/x00
Preamplifier set...........FAILED. /x31/x60
Preamplifier set...........FAILED. /x31/x60
Preamplifier set...........FAILED. /x31/x60
Preamplifier...............ON. /x31/x60/x21/x00/x21/x00/x21/x00/x21/x00
Attenuator set.............FAILED. /x31/x60
Attenuator set.............FAILED. /x31/x60
Attenuator set.............FAILED. /x31/x60
Attenuation................OFF. /x31/x60/x21/x00/x21/x00/x21/x00/x21/x00
Start frequency set.......FAILED. /x31/x60
Start frequency set.......FAILED. /x31/x60
Start frequency set.......FAILED. /x31/x60
Start frequency........... 99.5 MHz. /x31/x60/x21/x00/x21/x00/x21/x00/x21/x00
Stop frequency set........FAILED. /x31/x60
Stop frequency set........FAILED. /x31/x60
Stop frequency............ 101.5 MHz. /x31/x60/x21/x00/x21/x00/x21/x00
RBW set...................FAILED. /x31/x60
RBW set...................FAILED. /x31/x60
RBW set...................FAILED. /x31/x60
RBW....................... 1000 Hz. /x31/x60/x21/x00/x21/x00/x21/x00/x21/x00
Sweep delay accuracy.......FAILED. /x31/x60
Sweep delay accuracy.......FAILED. /x31/x60
Sweep delay accuracy.......FAILED. /x31/x60
Sweep delay accuracy.......ON. /x31/x60/x21/x00/x21/x00/x21/x00/x21/x00
Sweep reset................FAILED. /x31/x60
Sweep reset................FAILED. /x31/x60
Sweep reset................FAILED. /x31/x60
Sweep reset................SUCCESSFUL. /x31/x60/x21/x00/x21/x00/x21/x00/x21/x00
 
I'm at a complete loss as to why I'm observing this trend. Of course, the hardware setup and code are identical for both cases shown above. I've isolated the embedded board and tested its functionality in the cold; it's just fine. I've communicated with the manufacturers of the spectrum analyzer, and they do not report any issues with the device at these "near freezing" temperatures, being well above the analyzer's lowest operational temperature of -20 C. Furthermore, when I use the manufacturer's proprietary software to interface with the analyzer via USB, it behaves just fine even at the cooler temperatures in question.
 
Tying this back into PyUSB, I'm curious if anyone out there has dealt with something like this and might have recommendations for USB communication parameters to adjust or specific PyUSB features that might help me diagnose my issue. Find some code below for how I'm setting up my USB device.
 
Thanks in advance for your time!
 
Best,
 
Sam

# Set the number of attempts for every command.
ATTNUM = 10

# Number of READSZ byte measurements to make.
NUM = 100

# Size of each NUM measurement. Seems like the max is ~600 bytes.
READSZ = 64

# Spectran ERROR flag.
ERROR = False

# Define title of data file.
counter_str = str(file_counter)
dir = '/root/work/'
file0 = counter_str.zfill(6)+'.dat'
file = dir+file0

# Set status.
commands.getoutput('touch /root/work/LTM_MEASURING')

# Create serial object.
sys.stdout.write("Finding Spectran...........")
spectran = usb.core.find(idVendor=0x0403, idProduct=0xe8d8)
if spectran == None:
     sys.exit("ERROR FINDING SPECTRAN.")
else:
     sys.stdout.write("DONE.\n")

# Setup endpoints.
sys.stdout.write("Setting endpoints..........")
bulkin = spectran[0][(0,0)][0]
bulkout = spectran[0][(0,0)][1]
sys.stdout.write("DONE.\n")

# Check endpoint packet size. Should be 64 for USB comms.
sys.stdout.write('Max packet length..........%d.\n'%bulkin.wMaxPacketSize)

# Set up configuration.
sys.stdout.write("Setting configuration......")
spectran.set_configuration()
sys.stdout.write("DONE.\n")

# Set up USB communication parameters.
sys.stdout.write("Setting comm. parameters...")
spectran._isFTDI = True
spectran._rxthread = None 
spectran._rxactive = False 
spectran.baudrate = 921600 
spectran.parity = 0 
spectran.stopbits = 2 
spectran.databits = 8 
sys.stdout.write("DONE.\n")

# Detach kernel if busy.
if spectran.is_kernel_driver_active(0):
     spectran.detach_kernel_driver(0)
 
#I'll show just one command for brevity....
 
# Open communication with Spectran.
n = 0
check = sc.VERIFY(spectran,bulkin,bulkout) 
while check == False and n < ATTNUM:
check = sc.VERIFY(spectran,bulkin,bulkout)
n += 1
if n >= ATTNUM:
ERROR = True
 
#VERIFY is defined in a separate script, but here it is...
 
# USB timeout for every command.
TIMEOUT = 1000 #ms

#############################################################
def convert(response):
     converted_resp = re.sub(' ','/',' '+' '.join("x{:02x}".format(x) for x in response))
     return converted_resp
#############################################################

#############################################################
def VERIFY(spectran,bulkin,bulkout): #Verify USB communication with Spectran.
     spectran.write(bulkout.bEndpointAddress,'\x01\xa5\x5a\xf1\x1f')
     # Check for 7 byte reply, long sequence means success.
     time.sleep(0.5)
     ans = spectran.read(bulkin.bEndpointAddress,bulkin.wMaxPacketSize,timeout=TIMEOUT)
     ans = convert(ans)
     if '/x51/x1a/xf5/xaf' in ans:
          print 'Verify.....................SUCCESSFUL.', ans
          return True
     else:
          print 'Verify.....................FAILED.', ans
          return False
##############################################################
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Failed to download Qualtrics data using API balaKrishnaV 4 3,820 Sep-16-2023, 03:12 PM
Last Post: Jfreeland
  FTp timeout except korenron 2 3,504 Feb-01-2022, 06:51 AM
Last Post: korenron
  Looking for data/info on a perticular data-proccesing problem. MvGulik 9 3,781 May-01-2021, 07:43 AM
Last Post: MvGulik
  Problem with pexpect.exception.TimeOUT korenron 0 3,243 Apr-12-2021, 03:25 PM
Last Post: korenron
  timeout value in subprocess jonesin1974 2 4,949 Dec-01-2017, 02:18 PM
Last Post: snippsat
  Problem with serial data. How do I ignor incorrect data? donmerch 1 2,892 Jul-11-2017, 01:55 AM
Last Post: donmerch
  Pyusb freeze when using Dev.read(nr,nr,timeout=1000) bojane 3 7,457 Jan-11-2017, 10:17 AM
Last Post: bojane
  AsyncSSH and timeout Standard_user 1 5,445 Nov-03-2016, 06:05 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020