簡體   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