1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
#!/usr/bin/env python
"""
Simple Serial Example
This example shows how to use the ``circuits.io.Serial`` Component
to access serial data. This example simply echos back what it receives
on the serial port.
.. warning:: This example is currently untested.
"""
from circuits import Component, Debugger, handler
from circuits.io import Serial
from circuits.io.events import write
class EchoSerial(Component):
def init(self, port):
Serial(port).register(self)
@handler('read')
def on_read(self, data):
"""
Read Event Handler
This is fired by the underlying Serial Component when there has been
new data read from the serial port.
"""
self.fire(write(data))
# Start and "run" the system.
# Connect to /dev/ttyS0
app = EchoSerial('/dev/ttyS0')
Debugger().register(app)
app.run()
|