• <bdo id='IkFv1'></bdo><ul id='IkFv1'></ul>

    1. <small id='IkFv1'></small><noframes id='IkFv1'>

        <i id='IkFv1'><tr id='IkFv1'><dt id='IkFv1'><q id='IkFv1'><span id='IkFv1'><b id='IkFv1'><form id='IkFv1'><ins id='IkFv1'></ins><ul id='IkFv1'></ul><sub id='IkFv1'></sub></form><legend id='IkFv1'></legend><bdo id='IkFv1'><pre id='IkFv1'><center id='IkFv1'></center></pre></bdo></b><th id='IkFv1'></th></span></q></dt></tr></i><div id='IkFv1'><tfoot id='IkFv1'></tfoot><dl id='IkFv1'><fieldset id='IkFv1'></fieldset></dl></div>
      1. <legend id='IkFv1'><style id='IkFv1'><dir id='IkFv1'><q id='IkFv1'></q></dir></style></legend>
        <tfoot id='IkFv1'></tfoot>
      2. Raspberry Pi (Debian) 上的 Twisted Python 脚本通过 USB 与 Arduino 通

        Twisted Python script on Raspberry Pi (Debian) to communicate with Arduino via USB(Raspberry Pi (Debian) 上的 Twisted Python 脚本通过 USB 与 Arduino 通信)

            1. <small id='b0WD8'></small><noframes id='b0WD8'>

              • <bdo id='b0WD8'></bdo><ul id='b0WD8'></ul>
                  <tbody id='b0WD8'></tbody>

                • <tfoot id='b0WD8'></tfoot>

                  <i id='b0WD8'><tr id='b0WD8'><dt id='b0WD8'><q id='b0WD8'><span id='b0WD8'><b id='b0WD8'><form id='b0WD8'><ins id='b0WD8'></ins><ul id='b0WD8'></ul><sub id='b0WD8'></sub></form><legend id='b0WD8'></legend><bdo id='b0WD8'><pre id='b0WD8'><center id='b0WD8'></center></pre></bdo></b><th id='b0WD8'></th></span></q></dt></tr></i><div id='b0WD8'><tfoot id='b0WD8'></tfoot><dl id='b0WD8'><fieldset id='b0WD8'></fieldset></dl></div>
                  <legend id='b0WD8'><style id='b0WD8'><dir id='b0WD8'><q id='b0WD8'></q></dir></style></legend>
                • 本文介绍了Raspberry Pi (Debian) 上的 Twisted Python 脚本通过 USB 与 Arduino 通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我一直在从事 Arduino/Raspberry Pi 项目,我发现自己不仅在学习 Python,还学习 Twisted Python;所以我提前为我的新手道歉.我现在尽量保持简单,只是尝试在两个设备之间的任何时间发送一个字符.

                  I have been working on an Arduino/Raspberry Pi project where I have found myself learning not just Python but Twisted Python as well; so I apologize in advance for my newbness. I am trying to keep it simple for now and just trying to send a char at any one time between the two devices.

                  到目前为止,我能够从 Raspberry Pi 向 Arduino 发送数据,并按预期有效地关闭/打开其 LED.但是,我似乎无法生成 Twisted 代码来检测从 Arduino 到串行端口上的 RPi 的任何内容.我通过在 RPi 上运行的 Arduino 程序员中的串行监视器应用程序验证了 Arduino 每 2 秒发送一次字符.

                  So far I am able to send from the Raspberry Pi to the Arduino and effectively turn its LED off/on just as expected. However I cannot seem to generate Twisted code which will detect anything coming from the Arduino to the RPi on the serial port. I verified that the Arduino is sending chars every 2 seconds with a serial monitor application in the Arduino programmer running on the RPi.

                  下面的代码在 RPi 上运行,接收 GET 请求并通过串行端口将部分数据传递给 Arduino.不过,我似乎无法让这段代码监听同一个串行端口.:/我已经为此工作了一个多月,似乎被卡住了.我似乎无法找到一个在线 Twisted Python 接收串行数据的好例子;或者至少是我理解的一个例子.无论如何,这是我到目前为止所拥有的:

                  The code below runs on the RPi, receives a GET request and passes some of that data through the serial port to the Arduino. I cannot seem to get this code to listen to that same serial port though. :/ I have been working on this for a bit over a month and seem to be stuck. I just cannot seem to find a good example for Twisted Python online to receive serial data; or at least an example that I understand. Anyway here is what I have so far:

                  import sys
                  from urlparse import urlparse
                  from twisted.web import server, resource
                  from twisted.internet import reactor
                  from twisted.internet.protocol import Factory, Protocol
                  from twisted.internet.serialport import SerialPort
                  
                  serServ = None
                  
                  class USBclient(Protocol):
                      def connectionMade(self):
                          global serServ
                          serServ = self
                          print 'Arduino device: ', serServ, ' is connected.'
                  
                      def cmdReceived(self, cmd):
                          serServ.transport.write(cmd)
                          print cmd, ' - sent to Arduino.'
                          pass
                  
                      def serialReadEvent(self):      #maybe it should be: doRead()? Couldn't get either to work.
                          print 'data from arduino is at the serial port!'
                  
                  class HTTPserver(resource.Resource):
                      isLeaf = True
                      def render_GET(self, request):      #passes the data from the get request
                          print 'HTTP request received'
                          myArduino = USBclient()
                          stringit = str(request)
                          parse = stringit.split()
                          command, path, version = parse
                          myArduino.cmdReceived(path)
                  
                  class cmdTransport(Protocol):
                      def __init__(self, factory):
                          self.factory = factory
                  
                  class cmdTransportFactory(Factory):
                      protocol = cmdTransport
                  
                  if __name__ == '__main__':
                      HTTPsetup = server.Site(HTTPserver())
                      reactor.listenTCP(5000, HTTPsetup)
                      SerialPort(USBclient(), '/dev/ttyACM0', reactor, baudrate='115200')
                      reactor.run()
                  

                  正如您所见,代码只是在串行端口上寻找任何东西,但我似乎无法让这种魔法发生.提前致谢,感谢您的帮助!

                  As you can see the code is just looking for anything on the serial port but I can't seem to make that magic happen. Thanks in advance, any help is appreciated!

                  推荐答案

                  由此判断:http://twistedmatrix.com/trac/browser/tags/releases/twisted-12.3.0/twisted/internet/_win32serialport.py#L84 你应该查看协议子类的 dataReceived(self,...) 方法

                  Judging from this: http://twistedmatrix.com/trac/browser/tags/releases/twisted-12.3.0/twisted/internet/_win32serialport.py#L84 you should be looking at dataReceived(self,...) method of your Protocol subclass

                  因此:

                  class USBclient(Protocol):
                      def connectionMade(self):
                          global serServ
                          serServ = self
                          print 'Arduino device: ', serServ, ' is connected.'
                  
                      def cmdReceived(self, cmd):
                          serServ.transport.write(cmd)
                          print cmd, ' - sent to Arduino.'
                          pass
                  
                      def dataReceived(self,data):      
                          print 'USBclient.dataReceived called with:'
                          print str(data)
                  

                  试试这个看看它是否有效.

                  try this to see if it works.

                  这篇关于Raspberry Pi (Debian) 上的 Twisted Python 脚本通过 USB 与 Arduino 通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 画布滚动条?)

                      • <bdo id='vZ3Gh'></bdo><ul id='vZ3Gh'></ul>
                      • <tfoot id='vZ3Gh'></tfoot>

                      • <small id='vZ3Gh'></small><noframes id='vZ3Gh'>

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

                              <tbody id='vZ3Gh'></tbody>
                          • <legend id='vZ3Gh'><style id='vZ3Gh'><dir id='vZ3Gh'><q id='vZ3Gh'></q></dir></style></legend>