forked from nonolith/pixelpulse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tornado_serial: fall back to polling if we can't attach a handler to …
…the serial port (Windows)
- Loading branch information
1 parent
6fdde50
commit 6f4d960
Showing
1 changed file
with
10 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
# (C) 2011 Kevin Mehall (Nonolith Labs) <[email protected]> | ||
|
||
import serial | ||
from tornado.ioloop import IOLoop | ||
from tornado.ioloop import IOLoop, PeriodicCallback | ||
import os | ||
|
||
class TornadoSerial(object): | ||
|
@@ -12,11 +12,16 @@ def __init__(self, port=None, baud=9600, on_receive=None, on_error=None, *args): | |
self.on_error = on_error | ||
self.serial = serial.Serial(port, baud, timeout=0, *args) | ||
self.io_loop = IOLoop.instance() | ||
self.io_loop.add_handler( | ||
self.serial.fileno(), self._serial_event, | ||
IOLoop.READ) | ||
try: | ||
fileno = self.serial.fileno() | ||
self.io_loop.add_handler(fileno, self._serial_event, IOLoop.READ) | ||
except: | ||
print "Using polled serial IO" | ||
# Fall back to polling if we can't set up a Tornado handler (Windows) | ||
self.poll = PeriodicCallback(self._serial_event, 10) | ||
self.poll.start() | ||
|
||
def _serial_event(self, fd, events): | ||
def _serial_event(self, *ignore): | ||
try: | ||
data = self.serial.read(self.serial.inWaiting()) | ||
except IOError, e: | ||
|
@@ -62,7 +67,4 @@ def check_port(p): | |
exit(1) | ||
else: | ||
print "Using port", p | ||
elif not os.path.exists(p): | ||
print "Serial port %s not found"%p | ||
exit(1) | ||
return p |