当我在raspberry pi上运行程序向Arduino发送数据时,它正常工作,但突然停止发送数据,并返回一个错误。
错误信息“
socket.send('0')
bluetooth error(11,resource are temporarily unavailable)这个程序的目的是发送0到Arduino,如果Arduino收到0蜂鸣器将不会报警,否则它会报警..for 2分钟,一切顺利,但突然蜂鸣器警报,但'pi‘和'Arduino’中的2蓝牙仍然没有断开连接。
我搜索错误并发现它是因为pi中的缓冲区已经满了,它变成了块,但我不能解决问题,谁可以帮助我?谢谢。
这是代码
import bluetooth
import time
bd_addr = "98:D3:31:FB:19:AF"
port = 1
sock = bluetooth.BluetoothSocket (bluetooth.RFCOMM)
sock.connect((bd_addr,port))
while 1:
sock.send("0")
time.sleep(2)
sock.close()arduino码
#include <SoftwareSerial.h>
SoftwareSerial bt (5,6);
int LEDPin = 13; //LED PIN on Arduino
int btdata; // the data given from the computer
void setup() { bt.begin(9600); pinMode (LEDPin, OUTPUT); }
void loop() {
if (bt.available()) {
btdata = bt.read();
if (btdata == '1') {
//if 1
digitalWrite (LEDPin,HIGH);
bt.println ("LED OFF!");
}
else {
//if 0
digitalWrite (LEDPin,LOW);
bt.println ("LED ON!");
}
} else { digitalWrite(LEDPin, HIGH);
delay (100); //prepare for data
}
}发布于 2017-07-06 12:32:31
我认为你是在淹没它,因为你没有拖延的时间。您生成的数据发送速度比实际发送的要快,这最终会填满缓冲区。只需在您的time.sleep(0.5)中添加一个while,并通过测试哪一个在不填充缓冲区的情况下工作得最好,从而降低0.5值。
这是我让您的代码更具弹性的尝试:
import bluetooth
import time
bd_addr = "98:D3:31:FB:19:AF"
port = 1
sock = bluetooth.BluetoothSocket (bluetooth.RFCOMM)
sock.connect((bd_addr,port))
while 1:
try:
sock.send("0")
time.sleep(0.1)
sock.recv(1024)
except bluetooth.btcommon.BluetoothError as error:
print "Caught BluetoothError: ", error
time.sleep(5)
sock.connect((bd_addr,port))
time.sleep(2)
sock.close()这样做的目的是:
我还会像这样修改arduino代码:
#include <SoftwareSerial.h>
SoftwareSerial bt (5,6);
int LEDPin = 13; //LED PIN on Arduino
int btdata; // the data given from the computer
void setup() { bt.begin(9600); pinMode (LEDPin, OUTPUT); }
void loop() {
if (bt.available()) {
btdata = bt.read();
if (btdata == '1') {
//if 1
digitalWrite (LEDPin,HIGH);
bt.println ("LED OFF!");
}
else {
//if 0
digitalWrite (LEDPin,LOW);
bt.println ("LED ON!");
}
delay(1000);
} else { digitalWrite(LEDPin, HIGH);
delay (10); //prepare for data
}
}https://stackoverflow.com/questions/44948710
复制相似问题