gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 02:50:07 26 4
gpt4 key购买 nike

我已经成功连接到 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()

最佳答案

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

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

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

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

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

关于Python Twisted 从 TCP 接收命令写入串行设备返回响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4715340/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com