使用App Inventor在raspberry pi hc 05和Android app之间进行蓝牙通信

时间:2016-05-04 08:12:01

标签: android python python-2.7 bluetooth app-inventor

我已经编写了一个python代码

import time

import serial
port = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=3.0)
state = "4"
var = 6
while 1:
    port.flushInput()

    state = port.read()
    #print state
    if state == "1":
        print "ON"
        #relay on
        #start logging script

    if state == "0":
        print "OFF"
        #relay off
        #stop data logging script

    if state == "2":

        print "SENDING"
        print var
        final = str(var)

        print final
        port.flushInput()
        port.writelines(final)


    else:
        continue

port.close()

此代码主要用于从hc 05读取数据,这是由Android应用程序发送的... Android应用程序是使用 App Inventor

设计的

App Inventor的代码块如下所示

enter image description here

运行应用时出现以下错误

Traceback (most recent call last):   File "try11.py", line 10, in <module>
    state = port.read()   File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 456, in read
    raise SerialException('device reports readiness to read but returned no data (device disconnected?)') serial.serialutil.SerialException: device reports readiness to read but returned no data (device disconnected?)

我不明白导致此错误的原因。

0 个答案:

没有答案