python - Raspberry Pi (Debian) 上的 Twisted Python 脚本,用于通过 USB 与 Arduino 通信

标签 python serial-port arduino twisted

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

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

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

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

如您所见,代码只是在串行端口上寻找任何东西,但我似乎无法让魔法发生。在此先感谢您的帮助!

最佳答案

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

因此:

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)

试试这个,看看它是否有效。

关于python - Raspberry Pi (Debian) 上的 Twisted Python 脚本,用于通过 USB 与 Arduino 通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17739955/

相关文章:

python - 我可以将 cookie 从 python 导出到 Chrome 吗?

linux - termios VMIN VTIME 和阻塞/非阻塞读取操作

linux - 如何在不更改任何引脚的情况下在 linux 中打开串口?

Java:观察者或synchronized+notify()对串口传入数据

c - 在arduino中拆分字符串以滚动液晶屏

c++ - String 对象和以 null 结尾的字符数组之间的区别

python - 最大化具有多个参数的函数(python)

python - 在 Pandas 中绘图的日期采样/平均

python - 在 python 中解码 json

JavaFX ComboBox 不显示 COM 端口