Browse Source

Added no_reader_thread options to open to facilitate easier usage.

pyserial_fix
Scott Petersen 12 years ago
parent
commit
8f1e3e74b9
4 changed files with 61 additions and 21 deletions
  1. +2
    -2
      pyad2usb/ad2usb.py
  2. +31
    -9
      pyad2usb/devices.py
  3. +7
    -6
      pyad2usb/util.py
  4. +21
    -4
      test.py

+ 2
- 2
pyad2usb/ad2usb.py View File

@@ -188,12 +188,12 @@ class AD2USB(object):
"""
pass

def open(self, baudrate=None, interface=None, index=None):
def open(self, baudrate=None, interface=None, index=None, no_read_thread=False):
"""
Opens the device.
"""
self._wire_events()
self._device.open(baudrate=baudrate, interface=interface, index=index)
self._device.open(baudrate=baudrate, interface=interface, index=index, no_read_thread=no_read_thread)

def close(self):
"""


+ 31
- 9
pyad2usb/devices.py View File

@@ -110,7 +110,7 @@ class USBDevice(Device):

self._read_thread = Device.ReadThread(self)

def open(self, baudrate=BAUDRATE, interface=None, index=0):
def open(self, baudrate=BAUDRATE, interface=None, index=0, no_read_thread=False):
"""
Opens the device.
"""
@@ -143,7 +143,8 @@ class USBDevice(Device):
raise util.CommError('Error opening AD2USB device: {0}'.format(str(err)))
else:
self._running = True
self._read_thread.start()
if not no_read_thread:
self._read_thread.start()

self.on_open((self._serial_number, self._description))

@@ -164,7 +165,13 @@ class USBDevice(Device):

self.on_close()

def close_reader(self):
def is_reader_alive(self):
"""
Indicates whether or not the reader thread is alive.
"""
return self._read_thread.is_alive()

def stop_reader(self):
"""
Stops the reader thread.
"""
@@ -266,7 +273,7 @@ class SerialDevice(Device):
"""
pass

def open(self, baudrate=BAUDRATE, interface=None, index=None):
def open(self, baudrate=BAUDRATE, interface=None, index=None, no_read_thread=False):
"""
Opens the device.
"""
@@ -294,7 +301,8 @@ class SerialDevice(Device):
self._running = True
self.on_open((None, "AD2SERIAL")) # TODO: Fixme.

self._read_thread.start()
if not no_read_thread:
self._read_thread.start()

def close(self):
"""
@@ -310,7 +318,13 @@ class SerialDevice(Device):

self.on_close()

def close_reader(self):
def is_reader_alive(self):
"""
Indicates whether or not the reader thread is alive.
"""
return self._read_thread.is_alive()

def stop_reader(self):
"""
Stops the reader thread.
"""
@@ -398,7 +412,7 @@ class SocketDevice(Device):
"""
pass

def open(self, baudrate=None, interface=None, index=0):
def open(self, baudrate=None, interface=None, index=0, no_read_thread=False):
"""
Opens the device.
"""
@@ -416,7 +430,9 @@ class SocketDevice(Device):
self._running = True

self.on_open((None, "AD2SOCKET")) # TEMP: Change me.
self._read_thread.start()

if not no_read_thread:
self._read_thread.start()

def close(self):
"""
@@ -433,7 +449,13 @@ class SocketDevice(Device):

self.on_close()

def close_reader(self):
def is_reader_alive(self):
"""
Indicates whether or not the reader thread is alive.
"""
return self._read_thread.is_alive()

def stop_reader(self):
"""
Stops the reader thread.
"""


+ 7
- 6
pyad2usb/util.py View File

@@ -113,12 +113,13 @@ class Firmware(object):

stage_callback(Firmware.STAGE_START)

# Close the reader thread and wait for it to die, otherwise
# it interferes with our reading.
dev.close_reader()
while dev._read_thread.is_alive():
stage_callback(Firmware.STAGE_WAITING)
time.sleep(1)
if dev.is_reader_alive():
# Close the reader thread and wait for it to die, otherwise
# it interferes with our reading.
dev.stop_reader()
while dev._read_thread.is_alive():
stage_callback(Firmware.STAGE_WAITING)
time.sleep(1)

try:
# Reboot the device and wait for the boot loader.


+ 21
- 4
test.py View File

@@ -55,7 +55,8 @@ def handle_firmware(stage):
sys.stdout.write('.')
sys.stdout.flush()
elif stage == pyad2usb.ad2usb.util.Firmware.STAGE_BOOT:
print "\r\nRebooting device.."
if handle_firmware.wait_tick > 0: print ""
print "Rebooting device.."
elif stage == pyad2usb.ad2usb.util.Firmware.STAGE_LOAD:
print 'Waiting for boot loader..'
elif stage == pyad2usb.ad2usb.util.Firmware.STAGE_UPLOADING:
@@ -73,7 +74,7 @@ def handle_firmware(stage):
def upload_usb():
dev = pyad2usb.ad2usb.devices.USBDevice()

dev.open()
dev.open(no_read_thread=True)
pyad2usb.ad2usb.util.Firmware.upload(dev, 'tmp/ademcoemu_V2_2a_6.hex', handle_firmware)
dev.close()

@@ -204,6 +205,20 @@ def test_socket():

a2u.close()

def test_no_read_thread():
a2u = pyad2usb.ad2usb.Overseer.create()

a2u.on_open += handle_open
a2u.on_close += handle_close
a2u.on_read += handle_read
a2u.on_write += handle_write

a2u.open(no_read_thread=True)

print 'alive?', a2u._device._read_thread.is_alive()

a2u.close()

try:
signal.signal(signal.SIGINT, signal_handler)

@@ -214,11 +229,13 @@ try:
#test_usb_serial()
#test_factory()
#test_factory_watcher()
#upload_usb()
upload_usb()
#upload_usb_serial()

test_socket()
#test_socket()
#upload_socket()

#test_no_read_thread()

except Exception, err:
traceback.print_exc(err)

Loading…
Cancel
Save