简体   繁体   English

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

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

when I running program on raspberry pi to send data to Arduino it work properly but stopped sending data suddenly and return back an error 当我在树莓派上运行程序以将数据发送到Arduino时,它可以正常工作,但突然停止发送数据并返回错误

error message " 错误信息 ”

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

this program aims to send 0 to Arduino if Arduino receives 0 buzzers will not alarm else it alarm ..for 2 minutes all things going well but suddenly buzzer alarm but the 2 Bluetooth in 'pi' and 'Arduino' still connected not disconnected. 该程序旨在向Arduino发送0信号,如果Arduino接收到0个蜂鸣器将不会发出警报,否则它将发出警报。.2分钟内一切正常,但蜂鸣器突然发出警报,但仍在“ pi”和“ Arduino”中连接2个蓝牙但未断开连接。

I search for the error and find it is because the buffer in pi is full and it turns into the block but I can't solve the problem anyone can help me? 我搜索错误,发现这是因为pi中的缓冲区已满,并且变成了块,但是我无法解决任何人都可以帮助我的问题? thanks. 谢谢。

here is the code 这是代码

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 code 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   
    } 
}

I think you are flooding it, as you have no delay in your while. 我想您正在充斥它,因为您没有时间延迟。 You are generating data to send faster than it can actually send them, which eventually fills up the buffer. 您生成的数据发送速度比实际发送速度快,最终将填满缓冲区。 Simply add a time.sleep(0.5) in your while and decrease the value 0.5 by testing which one works best without filling up the buffer. 只需在您的while添加一个time.sleep(0.5) ,然后通过测试哪一个最适合而不填充缓冲区就可以将值减小为0.5

This is my try at making your code more resilient : 这是我使您的代码更具弹性的尝试:

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()

What this does is : 这是什么:

  • Wait a little before sending a new packet : Prevent your computer from generating data faster than it can send it, which eventually fills the buffer 在发送新数据包之前,请稍等:防止计算机生成数据的速度超过其发送数据的速度,从而最终填满缓冲区

  • Read the incomming data, thus consuming it from the inbound buffer : the arduino actually answers your requests, and that fills up the inbound buffer. 读取传入的数据,从而从入站缓冲区中使用它:arduino实际上回答了您的请求,并且填满了入站缓冲区。 If you don't empty it once in a while, it will overflow and your socket will be rendered unusable 如果您不时清空它,它将溢出并且您的套接字将变得不可用

  • Monitor for connection errors, and tries to recover from them by closing and reopening the socket 监视连接错误,并尝试通过关闭并重新打开套接字从错误中恢复

I would also modify the arduino code like this : 我也会像这样修改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.

相关问题 随机出现“ socket.error:[Errno 11]资源暂时不可用” - “socket.error: [Errno 11] Resource temporarily unavailable” appears randomly Python错误:发送图像时“socket.error:[Errno 11]资源暂时不可用” - Python error: “socket.error: [Errno 11] Resource temporarily unavailable” when sending image python:X服务器上的致命IO错误11(资源暂时不可用):0.0 - python: Fatal IO error 11 (Resource temporarily unavailable) on X server :0.0 部署 Python Web 应用程序后出现 App Engine 错误 - BlockingIOError: [Errno 11] 资源暂时不可用 - App Engine error after Python web app is deployed - BlockingIOError: [Errno 11] Resource temporarily unavailable Python中的子进程模块资源暂时不可用错误 - Resource temporarily unavailable error with subprocess module in Python 在Python 2.xx中修复“[Errno 11]资源暂时不可用”? - Fix “[Errno 11] Resource temporarily unavailable” in Python 2.x.x? OSError:[Errno 11]资源暂时不可用。 是什么导致这个? - OSError: [Errno 11] Resource temporarily unavailable. What causes this? 如何使用uwsgi + nginx解决[Errno 11]资源暂时不可用 - How to solve [Errno 11] Resource temporarily unavailable using uwsgi + nginx 套接字设置阻止提高OSError:[Errno 11]资源暂时不可用 - socket set blocking raising OSError: [Errno 11] Resource temporarily unavailable gunicorn + django + nginx — recv()未准备就绪(11:资源暂时不可用) - gunicorn + django + nginx — recv() not ready (11: Resource temporarily unavailable)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM