扭曲的Python的树莓派(Debian的)脚本,以通过USB与Arduino的沟通 [英] Twisted Python script on Raspberry Pi (Debian) to communicate with Arduino via USB

查看:174
本文介绍了扭曲的Python的树莓派(Debian的)脚本,以通过USB与Arduino的沟通的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在一个Arduino /树莓派项目中,我发现自己学习不只是Python的,但扭曲的Python为好;所以我提前道歉我newbness。我试图保持它的简单,现在,只是想在两台设备之间任何一次发送一个字符。

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.

到目前为止,我能够从树莓派发送到Arduino并有效地将其LED /关闭只是预期。不过,我似乎无法产生扭曲code,将检测到任何来自Arduino的来到RPI串行端口。我验证了Arduino是发送每2秒字符与RPI的运行中的Arduino编程串行监控应用程序。

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运行code,接收一个GET请求,并通过串口到Arduino通过一些数据。我似乎无法得到这code听同一串行端口虽然。 :/我一直在这一段有点过了一个月,似乎被卡住。我似乎无法在网上找到了扭曲的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()

正如你可以看到code为只为串口什么,但我似乎无法使该魔术发生。在此先感谢,任何帮助是AP preciated!

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!

推荐答案

从这个情况来看:<一href=\"http://twistedmatrix.com/trac/browser/tags/releases/twisted-12.3.0/twisted/internet/_win32serialport.py#L84\" rel=\"nofollow\">http://twistedmatrix.com/trac/browser/tags/releases/twisted-12.3.0/twisted/internet/_win32serialport.py#L84你应该看dataReceived(个体经营,...)的协议的子类的方法

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.

这篇关于扭曲的Python的树莓派(Debian的)脚本,以通过USB与Arduino的沟通的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆