Python Twisted从TCP接收命令写入串行设备返回响应

发布于 2024-10-12 13:57:59 字数 2114 浏览 3 评论 0原文

我已经成功连接到 USB 调制解调器,并且客户端可以通过 tcp 连接到我的reactor.listenTCP,从调制解调器收到的数据将发送回客户端。我想获取从客户端接收的数据并将其发送到调制解调器。我正在努力使其工作。任何帮助将不胜感激!代码:

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()

I've managed to connect to usb modem and a client can connect via tcp to my reactor.listenTCP,the data received from modem will be send back to client. I'm want to take dataReceived from client and send this to modem..I'm struggling to get this to work.Any help will be highly appreciated! the code:

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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

春风十里 2024-10-19 13:57:59

你的问题不是关于twisted,而是关于python。阅读此常见问题解答条目:

如何使一个连接上的输入导致另一个连接上的输出?

事情是,如果你想在串行连接协议中向 TCP 连接的客户端发送内容,只需向协议传递对工厂的引用,这样你就可以使用该引用来建立桥接。

下面是一些大致执行此操作的示例代码:

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)

初始化时,传递引用:

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

Your problem is not about twisted, but about python. Read this FAQ entry:

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

Thing is, if you want to send stuff to a TCP-connected client in your serial-connected protocol, just pass to the protocol a reference to the factory, so you can use that reference to make the bridge.

Here's some example code that roughly does this:

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)

When initializing, pass the reference:

tcpfactory = CommandRxFactory()
reactor.listenTCP(8000, tcpfactory)
SerialPort(USBClient(tcpfactory), 'COM8', reactor, baudrate='19200')
reactor.run()
榆西 2024-10-19 13:57:59

谢谢,让它工作。无法让 def notificationAll 将数据发送到我的调制解调器。是这样的:对于 client_list 中的 cli:cli.transport.write(data)
新代码:

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()

Thanks, got it to work.Could not get def notifyAll to send data to my modem..did it like this:for cli in client_list: cli.transport.write(data)
new 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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文