2011-01-17 4 views
4

Ich habe es geschafft, mit USB-Modem verbinden und ein Client kann über TCP zu meinem reactor.listenTCP verbinden, die Daten vom Modem erhalten zurück an den Client gesendet werden. Ich will dataReceived vom Klienten nehmen und dieses zum Modem schicken. Ich kämpfe, um dieses zur Arbeit zu bringen. Jede Hilfe wird sehr geschätzt! Der Code:Python Twisted empfange Befehl von TCP schreibe zu Seriell Gerät Antwort Antwort

from twisted.internet import win32eventreactor 
win32eventreactor.install() 
from twisted.internet import reactor 
from twisted.internet.serialport import SerialPort 
from twisted.internet.protocol import Protocol, Factory 
from twisted.python import log 
import sys 

log.startLogging(sys.stdout) 
client_list = []#TCP clients connecting to me 

class USBClient(Protocol): 

    def connectionFailed(self): 
     print "Connection Failed:", self 
     reactor.stop() 

    def connectionMade(self): 
     print 'Connected to USB modem' 
     USBClient.sendLine(self, 'AT\r\n') 

    def dataReceived(self, data): 
     print "Data received", repr(data) 
     print "Data received! with %d bytes!" % len(data) 
     #check & perhaps modify response and return to client 
     for cli in client_list: 
      cli.notifyClient(data) 
     pass 

    def lineReceived(self, line): 
     print "Line received", repr(line) 

    def sendLine(self, cmd): 
     print cmd 
     self.transport.write(cmd + "\r\n") 

    def outReceived(self, data): 
     print "outReceived! with %d bytes!" % len(data) 
     self.data = self.data + data 

class CommandRx(Protocol): 

    def connectionMade(self): 
     print 'Connection received from tcp..' 
     client_list.append(self) 

    def dataReceived(self, data): 
     print 'Command receive', repr(data) 
     #Build command, if ok, send to serial port 
     #???? 
    def connectionLost(self, reason): 
     print 'Connection lost', reason 
     if self in client_list: 
      print "Removing " + str(self) 
      client_list.remove(self) 

    def notifyClient(self, data): 
     self.transport.write(data) 

class CommandRxFactory(Factory): 
    protocol = CommandRx 
    def __init__(self): 
     client_list = [] 

if __name__ == '__main__': 
    reactor.listenTCP(8000, CommandRxFactory()) 
    SerialPort(USBClient(), 'COM8', reactor, baudrate='19200') 
    reactor.run() 

Antwort

4

Ihr Problem ist nicht über verdreht, sondern über Python. Lesen Sie diesen FAQ-Eintrag:

How do I make input on one connection result in output on another?

Das Ding ist, wenn man Sachen zu einem TCP-verbundenen Client in Ihrem seriellen verbunden Protokoll senden möchten, geben Sie einfach auf das Protokoll einen Verweis auf das Werk , so können Sie diese Referenz verwenden, um die Brücke zu erstellen.

Hier einige Beispiel-Code, der in etwa Folgendes aus:

class USBClient(Protocol): 
    def __init__(self, network): 
     self.network = network 
    def dataReceived(self, data): 
     print "Data received", repr(data) 
     #check & perhaps modify response and return to client 
     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) 

Bei der Initialisierung, die einen Verweis:

tcpfactory = CommandRxFactory() 
reactor.listenTCP(8000, tcpfactory) 
SerialPort(USBClient(tcpfactory), 'COM8', reactor, baudrate='19200') 
reactor.run() 
0

Danke, habe es nicht def notifyAll work.Could erhalten Daten zu senden mein modem..did es wie folgt: für cli in client_list: cli.transport.write (Daten)
neuen Code:

import sys 

from twisted.internet import reactor 
from twisted.internet.protocol import Factory 
from twisted.internet.protocol import Protocol 
from twisted.internet.serialport import SerialPort 
from twisted.python import log 

log.startLogging(sys.stdout) 
client_list = []#TCP clients connecting to me 
usb_list = [] 

class USBClient(Protocol): 

    def __init__(self, network): 
     self.network = network 
     self.usb_list = [] 

    def connectionFailed(self): 
     print "Connection Failed:", self 
     reactor.stop() 

    def connectionMade(self): 
     usb_list.append(self) 
     print 'Connected to USB modem' 
     USBClient.sendLine(self, 'AT\r\n') 

    def dataReceived(self, data): 
     print "Data received", repr(data) 
     print "Data received! with %d bytes!" % len(data) 

     for cli in client_list: 
      cli.transport.write(data) 
     #self.network.notifyAll(data)# !!AArgh..!Could not get this to work 
     pass 

    def lineReceived(self, line): 
     print "Line received", repr(line) 

    def sendLine(self, cmd): 
     print cmd 
     self.transport.write(cmd + "\r\n") 

    def outReceived(self, data): 
     print "outReceived! with %d bytes!" % len(data) 
     self.data = self.data + data 

class CommandRx(Protocol): 


    def connectionMade(self): 
     print 'Connection received from tcp..' 
     client_list.append(self) 

    def dataReceived(self, data): 
     print 'Command receive', repr(data) 
     for usb in usb_list: 
      usb.transport.write(data) 

    def connectionLost(self, reason): 
     print 'Connection lost', reason 
     if self in client_list: 
      print "Removing " + str(self) 
      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('yipee') 

if __name__ == '__main__': 

    tcpfactory = CommandRxFactory() 
    reactor.listenTCP(8000, tcpfactory) 
    SerialPort(USBClient(tcpfactory), '/dev/ttyUSB4', reactor, baudrate='19200') 
    reactor.run()