繁体   English   中英

蓝牙错误(11,资源暂时不可用)

[英]bluetooth error(11,resource are temporarily unavailable)

当我在树莓派上运行程序以将数据发送到Arduino时,它可以正常工作,但突然停止发送数据并返回错误

错误信息 ”

socket.send('0') 
bluetooth error(11,resource are temporarily unavailable)

该程序旨在向Arduino发送0信号,如果Arduino接收到0个蜂鸣器将不会发出警报,否则它将发出警报。.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   
    } 
}

我想您正在充斥它,因为您没有时间延迟。 您生成的数据发送速度比实际发送速度快,最终将填满缓冲区。 只需在您的while添加一个time.sleep(0.5) ,然后通过测试哪一个最适合而不填充缓冲区就可以将值减小为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实际上回答了您的请求,并且填满了入站缓冲区。 如果您不时清空它,它将溢出并且您的套接字将变得不可用

  • 监视连接错误,并尝试通过关闭并重新打开套接字从错误中恢复

我也会像这样修改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   
    } 
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM