1. <legend id='nn8LL'><style id='nn8LL'><dir id='nn8LL'><q id='nn8LL'></q></dir></style></legend>

      <tfoot id='nn8LL'></tfoot>

        <small id='nn8LL'></small><noframes id='nn8LL'>

        <i id='nn8LL'><tr id='nn8LL'><dt id='nn8LL'><q id='nn8LL'><span id='nn8LL'><b id='nn8LL'><form id='nn8LL'><ins id='nn8LL'></ins><ul id='nn8LL'></ul><sub id='nn8LL'></sub></form><legend id='nn8LL'></legend><bdo id='nn8LL'><pre id='nn8LL'><center id='nn8LL'></center></pre></bdo></b><th id='nn8LL'></th></span></q></dt></tr></i><div id='nn8LL'><tfoot id='nn8LL'></tfoot><dl id='nn8LL'><fieldset id='nn8LL'></fieldset></dl></div>
          <bdo id='nn8LL'></bdo><ul id='nn8LL'></ul>

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

        Python Twisted receive command from TCP write to Serial device return response(Python Twisted 从 TCP 接收命令写入串行设备返回响应)

        <small id='uKSnT'></small><noframes id='uKSnT'>

          <legend id='uKSnT'><style id='uKSnT'><dir id='uKSnT'><q id='uKSnT'></q></dir></style></legend>

                • <bdo id='uKSnT'></bdo><ul id='uKSnT'></ul>
                  <tfoot id='uKSnT'></tfoot>
                    <tbody id='uKSnT'></tbody>

                • <i id='uKSnT'><tr id='uKSnT'><dt id='uKSnT'><q id='uKSnT'><span id='uKSnT'><b id='uKSnT'><form id='uKSnT'><ins id='uKSnT'></ins><ul id='uKSnT'></ul><sub id='uKSnT'></sub></form><legend id='uKSnT'></legend><bdo id='uKSnT'><pre id='uKSnT'><center id='uKSnT'></center></pre></bdo></b><th id='uKSnT'></th></span></q></dt></tr></i><div id='uKSnT'><tfoot id='uKSnT'></tfoot><dl id='uKSnT'><fieldset id='uKSnT'></fieldset></dl></div>
                  本文介绍了Python Twisted 从 TCP 接收命令写入串行设备返回响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  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
                  ')
                  
                      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 + "
                  ")
                  
                      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()
                  

                  推荐答案

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

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

                  如何在一个连接上进行输入导致另一个连接上的输出?

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

                  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)
                  

                  初始化时,传递引用:

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

                  这篇关于Python Twisted 从 TCP 接收命令写入串行设备返回响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Pythonic and efficient way of finding adjacent cells in grid(在网格中查找相邻单元格的 Pythonic 和有效方法)
                  map a hexagonal grid in matplotlib(在 matplotlib 中映射六边形网格)
                  Execute arbitrary python code remotely - can it be done?(远程执行任意 python 代码 - 可以吗?)
                  Python - Plotting colored grid based on values(Python - 根据值绘制彩色网格)
                  Is there a GUI design app for the Tkinter / grid geometry?(是否有 Tkinter/网格几何图形的 GUI 设计应用程序?)
                  tkinter Canvas Scrollbar with Grid?(带有网格的 tkinter 画布滚动条?)

                  <small id='XAWTT'></small><noframes id='XAWTT'>

                      <tbody id='XAWTT'></tbody>

                    <i id='XAWTT'><tr id='XAWTT'><dt id='XAWTT'><q id='XAWTT'><span id='XAWTT'><b id='XAWTT'><form id='XAWTT'><ins id='XAWTT'></ins><ul id='XAWTT'></ul><sub id='XAWTT'></sub></form><legend id='XAWTT'></legend><bdo id='XAWTT'><pre id='XAWTT'><center id='XAWTT'></center></pre></bdo></b><th id='XAWTT'></th></span></q></dt></tr></i><div id='XAWTT'><tfoot id='XAWTT'></tfoot><dl id='XAWTT'><fieldset id='XAWTT'></fieldset></dl></div>
                        <legend id='XAWTT'><style id='XAWTT'><dir id='XAWTT'><q id='XAWTT'></q></dir></style></legend>
                          <bdo id='XAWTT'></bdo><ul id='XAWTT'></ul>
                        • <tfoot id='XAWTT'></tfoot>