Skip to content

Commit

Permalink
tornado_serial: fall back to polling if we can't attach a handler to …
Browse files Browse the repository at this point in the history
…the serial port (Windows)
  • Loading branch information
kevinmehall committed Jun 30, 2011
1 parent 6fdde50 commit 6f4d960
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions tornado_serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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:
Expand Down Expand Up @@ -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

0 comments on commit 6f4d960

Please sign in to comment.