All pastes #1551230 Raw Edit

Serial Server

public python v1 · immutable
#1551230 ·published 2009-09-02 07:17 UTC
rendered paste body
#!/usr/bin/env python#Taken from http://maats.org/tiki-index.php?page=Twisted%20Tutorialfrom twisted.internet import reactorfrom twisted.internet.protocol import ServerFactory from twisted.protocols.basic import LineOnlyReceiverfrom twisted.internet.serialport import SerialPortmsg=""class ArdProtocol():    global msg    def __init__(self):        self.counter=0        self.temp_str=""        self.command=""    def connectionMade(self):        print "Serial connection Made"        print "New connection from "+self.getName()             def lineReceived(self, line):        #Need more intelligence here...        print line                def makeConnection(self, transport):        pass        def dataReceived(self, data):        global msg                  """for each open bracket character in the string, increment a counter (self.counter)             for each close bracket decrement the string,                          if the counter is zero and the current character is a cr (\n) then do something with that string.                    """        #NQR: print data.strip()        #self.count=0        for char in data:            if char == "(":                self.counter=self.counter+1 #start of a section...                                                if char == ")":                self.counter=self.counter-1 # end of a section                        self.temp_str=self.temp_str+ char            #if self.counter==0 and self.temp_str != "":            if char =="\r":                #We do something with the complete string                self.command=self.temp_str.strip()                self.temp_str=""            #print "DEBUG: str: \"%s\" count %d" % (self.temp_str, self.counter)                    print "DEBUG: command: \"%s\"" % (self.command)                    msg=self.command                    #print "DEBUG: str: %s count %d" % (self.temp_str, self.counter)        pass            def version(self):        return 0.020 class ChatProtocol(LineOnlyReceiver):     name = ""     def getName(self):         if self.name!="":             return self.name         return self.transport.getPeer().host     def connectionMade(self):         print "New connection from "+self.getName()         self.sendLine("Welcome to my my chat server.")         self.sendLine("Send '/NAME [new name]' to change your name.")         self.sendLine("Send '/EXIT' to quit.")         self.factory.sendMessageToAllClients(self.getName()+" has joined the party.")         self.factory.clientProtocols.append(self)    def connectionLost(self, reason):         print "Lost connection from "+self.getName()         self.factory.clientProtocols.remove(self)         self.factory.sendMessageToAllClients(self.getName()+" has disconnected.")     def lineReceived(self, line):         print self.getName()+" said "+line         if line[:5]=="/NAME":             oldName = self.getName()             self.name = line[5:].strip()             self.factory.sendMessageToAllClients(oldName+" changed name to "+self.getName())         elif line=="/EXIT":             self.transport.loseConnection()         else:            self.factory.sendMessageToAllClients(self.getName()+" says "+line)                def sendLine(self, line):         self.transport.write(line+"\r\n") class ChatProtocolFactory(ServerFactory):         protocol = ChatProtocol     def __init__(self):         self.clientProtocols = []     def sendMessageToAllClients(self, mesg):         for client in self.clientProtocols:            client.sendLine(mesg)    def sendMessageToSerial(self, msg):                pass #Not sure what to do here    def version(self):        return 0.010print "Starting Server"chatfactory = ChatProtocolFactory()reactor.listenTCP(14000, chatfactory)print "DEBUG: Chat Version: %f " % chatfactory.version()#s = SerialPort(ardTest(), o.opts['port'], reactor, baudrate=baudrate)#s = SerialPort(ardTest(), o.opts['port'], reactor, baudrate=baudrate)", reactor, baudrate=38400)#s = SerialPort(ArdProtocol(), "/dev/arduino", reactor, baudrate=38400)s = SerialPort(ArdProtocol(), "/dev/arduino", reactor, baudrate=38400)print "DEBUG: Arduino Version: %f " % ArdProtocol.version(ArdProtocol())#reactor.run()