Python 2.7 通过串行蓝牙从raspberry pi向arduino请求数据
我正试图将数据从一个带有集成ble的arduino(来自DFRobot的bluno nano)发送到一个raspberry pi 2,它有一个hm10 ble模块连接到它的串行gpio14和15个引脚 我目前正在使用此草图:Python 2.7 通过串行蓝牙从raspberry pi向arduino请求数据,python-2.7,arduino,serial-port,raspberry-pi2,Python 2.7,Arduino,Serial Port,Raspberry Pi2,我正试图将数据从一个带有集成ble的arduino(来自DFRobot的bluno nano)发送到一个raspberry pi 2,它有一个hm10 ble模块连接到它的串行gpio14和15个引脚 我目前正在使用此草图: float flow = 500.06; void setup() { Serial.begin( 9600 );} void loop() { if (Serial.available()>0) { if (Serial.read() =
float flow = 500.06;
void setup() {
Serial.begin( 9600 );}
void loop() {
if (Serial.available()>0) {
if (Serial.read() == 'R') {
Serial.print(flow)
Serial.write("\n");
}
}
}
我用这个python来阅读它:
#! /usr/bin/env python
import serial
from time import sleep
ser = serial.Serial(port='/dev/serial0',parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,bytesize=serial.EIGHTBITS,timeout=1)
# Wait to read from Arduino
while 1:
try:
time.sleep(10)
ser.write("R")
myData = ser.readline()
print myData
except KeyboardInterrupt:
exit()
这将使树莓在运行脚本10秒后向arduino发送R。但是我每十秒钟就会在树莓终端上看到一行空白。我猜那是打印我的数据行,它是空白的
我刚刚试着通过usb串行连接bluno nano和raspberry pi 2,运行了这个代码,rpi收到了82,这是R的ascii等价物。我修改了代码并添加了一个图像来显示这些发现。我还是不明白为什么我没有得到回应,当它通过蓝牙串行
Arduino需要等待树莓Pi发出的信号。
例如,通过读取字符
'R'
浮子流量=500.06;
无效设置(){
Serial.begin(9600);
}
void循环(){
如果(Serial.available()>0){
if(Serial.read()=='R'){
串行打印(流);
串行写入(“\n”);
}
}
}
在从Arduino读取数据之前,树莓Pi需要发送字符'R'
导入序列号
导入时间
ser=serial.serial(端口='/dev/serial0',奇偶校验=serial.parity\u NONE,
停止位=串行。停止位\u一,字节大小=串行。八位,超时=1)
#等待Arduino的消息
而1:
尝试:
时间。睡眠(10)
顺序书写(“R”)
myData=ser.readline()
打印我的数据
除键盘中断外:
塞尔克洛斯()
退出()
我修改了OP以包含您的解决方案,但仍然缺少一些内容。我不认为有任何数据来自Arduino。我测试了你的代码,在修复了明显的编译器错误后,代码运行良好。我现在在OP中的那个?你是在用蓝牙还是串口?我只是在Windows上用串口。如果您的蓝牙已配对并连接,则该代码应能正常工作。您是否只能从Arduino发送数据,但无法接收数据?否。如果我在Arduino IDE串行监视器中键入,这些值将显示在raspberry终端上。我还可以将数据从arduino草图发送到raspberry终端。这是我从树莓中发出的,在Arduino一侧没有出现或似乎没有被认出。