[Twisted-Python] Add serial connection to a daemon
Joel Tremblet
joel.tremblet at openflyers.com
Wed Feb 6 22:29:19 EST 2013
Hi
I build a daemon from the example
http://twistedmatrix.com/documents/current/core/howto/tutorial/factory.html
application = service.Application('opensource')
f = OpensourceService()
webRoot = resource.Resource()
webRoot.putChild('',AdminPage())
serviceCollection = service.IServiceCollection(application)
internet.TCPClient(ADDR, PORT, f.getClientFactory()
).setServiceParent(serviceCollection)
internet.TCPServer(SERVICE_PORT, server.Site(f.getWebResource())
).setServiceParent(serviceCollection)
internet.TCPServer(WEB_ADMIN_PORT, server.Site(webRoot)
).setServiceParent(serviceCollection)
It's work fine but I want to add a serial connection to send a order
to my Client when a event occur on the USB connector
I read my serial data with twisted.internet.serialport
from twisted.internet import reactor
from twisted.internet.protocol import Factory, Protocol
from twisted.internet.serialport import SerialPort
COM_PORT='COM5'
BAUD_RATE=9600
class USBClient(Protocol):
def __init__(self, network):
self.network = network
def dataReceived(self, data):
print "Data received", repr(data)
self.network.notifyAll(data)
class CommandRx(Protocol):
def connectionMade(self):
self.factory.client_list.append(self)
def connectionLost(self, reason):
if self in self.factory.client_list:
self.factory.client_list.remove(self)
class CommandRxFactory(Factory):
protocol = CommandRx
def __init__(self):
self.client_list = []
def notifyAll(self, data):
for cli in self.client_list:
cli.transport.write(data)
tcpfactory = CommandRxFactory()
reactor.listenTCP(8001, tcpfactory)
SerialPort(USBClient(tcpfactory), COM_PORT, reactor, baudrate=BAUD_RATE)
reactor.run()
Can I integrated this serial connection into my daemon
Thanks for your help
Joel
More information about the Twisted-Python
mailing list