diff --git a/pyad2usb/devices.py b/pyad2usb/devices.py index 8a0fbb6..d3e0bc0 100644 --- a/pyad2usb/devices.py +++ b/pyad2usb/devices.py @@ -205,12 +205,21 @@ class USBDevice(Device): """ Reads a line from the device. """ - start_time = time.time() + def timeout_event(): + timeout_event.reading = False + + timeout_event.reading = True + got_line = False ret = None + timer = None + if timeout > 0: + timer = threading.Timer(timeout, timeout_event) + timer.start() + try: - while self._running: + while timeout_event.reading: buf = self._device.read_data(1) if buf != '': @@ -228,10 +237,7 @@ class USBDevice(Device): else: self._buffer = self._buffer[:-1] - if timeout > 0 and time.time() - start_time > timeout: - raise util.TimeoutError('Timeout while waiting for line terminator.') - - time.sleep(0.1) + time.sleep(0.001) except (usb.core.USBError, FtdiError), err: raise util.CommError('Error reading from AD2USB device: {0}'.format(str(err))) @@ -242,6 +248,9 @@ class USBDevice(Device): self.on_read(ret) + if timer: + timer.cancel() + return ret @@ -380,12 +389,22 @@ class SerialDevice(Device): """ Reads a line from the device. """ - start_time = time.time() + + def timeout_event(): + timeout_event.reading = False + + timeout_event.reading = True + got_line = False ret = None + timer = None + if timeout > 0: + timer = threading.Timer(timeout, timeout_event) + timer.start() + try: - while self._running: + while timeout_event.reading: buf = self._device.read(1) if buf != '' and buf != "\xff": # AD2SERIAL specifically apparently sends down \xFF on boot. @@ -403,8 +422,7 @@ class SerialDevice(Device): else: self._buffer = self._buffer[:-1] - if timeout > 0 and time.time() - start_time > timeout: - raise util.TimeoutError('Timeout while waiting for line terminator.') + time.sleep(0.001) except (OSError, serial.SerialException), err: raise util.CommError('Error reading from AD2SERIAL device: {0}'.format(str(err))) @@ -415,6 +433,9 @@ class SerialDevice(Device): self.on_read(ret) + if timer: + timer.cancel() + return ret class SocketDevice(Device): @@ -527,12 +548,21 @@ class SocketDevice(Device): """ Reads a line from the device. """ - start_time = time.time() + def timeout_event(): + timeout_event.reading = False + + timeout_event.reading = True + got_line = False ret = None + timer = None + if timeout > 0: + timer = threading.Timer(timeout, timeout_event) + timer.start() + try: - while self._running: + while timeout_event.reading: buf = self._device.recv(1) if buf != '': @@ -550,8 +580,7 @@ class SocketDevice(Device): else: self._buffer = self._buffer[:-1] - if timeout > 0 and time.time() - start_time > timeout: - raise util.TimeoutError('Timeout while waiting for line terminator.') + time.sleep(0.001) except socket.error, err: raise util.CommError('Error reading from Socket device: {0}'.format(str(err))) @@ -562,4 +591,7 @@ class SocketDevice(Device): self.on_read(ret) + if timer: + timer.cancel() + return ret diff --git a/test.py b/test.py index 28ba817..938ba7a 100755 --- a/test.py +++ b/test.py @@ -222,18 +222,28 @@ def test_socket(): a2u.close() def test_no_read_thread(): - a2u = pyad2usb.ad2usb.Overseer.create() + #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_reader_thread=True) + #a2u.on_open += handle_open + #a2u.on_close += handle_close + #a2u.on_read += handle_read + #a2u.on_write += handle_write - print 'alive?', a2u._device._read_thread.is_alive() + #a2u.open(no_reader_thread=True) - a2u.close() + dev = pyad2usb.ad2usb.devices.SerialDevice(interface='/dev/ttyUSB0') + dev.open(no_reader_thread=True) + + #print 'alive?', a2u._device._read_thread.is_alive() + while 1: + line = dev.read_line(timeout=5) + print line + line = dev.read_line(timeout=5) + print line + #time.sleep(0.1) + + dev.close() def test_serial_grep(): re = pyad2usb.devices.SerialDevice.find_all(pattern='VID:PID=9710:7840') @@ -283,10 +293,10 @@ try: #test_socket() #upload_socket() - #test_no_read_thread() + test_no_read_thread() #test_serial_grep() - test_double_panel_write() + #test_double_panel_write() except Exception, err: traceback.print_exc(err)