I have an object.
This object has a connect()
method which spawns a pexpect process.
The process that's spawned is a custom serial interface. On launch, this tool prints a menu of serial devices to connect to, like so:
libftdi device (0): A6005jpt libftdi device (1): acFX9DQf Serial device (a) : /dev/cu.Bluetooth-PDA-Sync Select a device by its letter (^D to abort):
My connect()
determines which number to pass based on a given devicename (e.g. 'acFX9DQf'): (self.connection is the pexpect spawn)
USBSERIAL_DEVICE_NAME = "acFX9DQf"
try:
while self.connection.expect(['libftdi device \(([0-9])\): (.*)','Serial device']) == 0:
if self.connection.match.group(2).strip() == USBSERIAL_DEVICE_NAME:
# do stuff
except:
# do stuff
Now, my problem is that I connect()
/kill()
the process multiple times in my main logic and sometimes, one of those times, connect()
decides to throw a pexpect.TIMEOUT
exception, unexpectedly.
For example, when I add the following debug statements to my logic, like so:
USBSERIAL_DEVICE_NAME = "acFX9DQf"
try:
while self.connection.expect(['libftdi device \(([0-9])\): (.*)','Serial device'], timeout=10) == 0:
print "MATCHED A DEVICE LINE!"
if self.connection.match.group(2).strip() == USBSERIAL_DEVICE_NAME:
print "MATCHED THE DESIRED USBSERIAL..."
...I get this output for many calls of connect()
:
libftdi device (0): A6005jpt MATCHED A DEVICE LINE! libftdi device (1): acFX9DQf MATCHED A DEVICE LINE! MATCHED THE DESIRED USBSERIAL... Serial device (a) : /dev/cu.Bluetooth-PDA-Sync Select a device by its letter (^D to abort): 1
...then, one of my connect()
calls will unexpectedly do....
libftdi device (0): A6005jpt MATCHED A DEVICE LINE! libftdi device (1): acFX9DQf Serial device (a) : /dev/cu.Bluetooth-PDA-Sync Select a device by its letter (^D to abort): MATCHED A DEVICE LINE! <<EXCEPTION>>
But, if I revise my code to this:
try:
while self.connection.expect(['libftdi device \(([0-9])\): (.*)','Serial device'], timeout=10) == 0:
devicename = self.connection.match.group(2).strip()
if devicename == USBSERIAL_DEVICE_NAME:
# do stuff
My problems go away! I can run it repeatedly and no problems will occur - no exceptions, no nothin'.
So wot in the heck is going on here? I'm not even sure where to begin with this problem.