【问题标题】:ESP32 Bluetooth Connection disconnects when finished with reading bufferESP32 蓝牙连接在读取缓冲区完成后断开
【发布时间】:2019-10-23 04:32:36
【问题描述】:

我正在对 ESP32 进行编程以接受蓝牙命令并使用串行配置文件将蓝牙数据发送回我的手机。为此,我正在使用 Arduino Espressif 蓝牙串行库。每当我向 ESP32 发送内容时,它都会对其进行处理,然后突然关闭蓝牙连接。

据我所知,我已经尝试了各种延迟,因为我认为处理器可能没有跟上其他东西,因此它崩溃了。 但是,当通过 USB 使用串行连接进行监控时,它仍然会不断发送状态更新。 除此之外,我真的找不到解决方案(也在互联网上)。

因为我几乎是一个初学者,所以我不想尝试构建自己的串行蓝牙库。 ESP 在发送数据时不会崩溃。它还继续处理发送的数据。我可以看到它发送的字符是我在收集它们后使用串行接口通过蓝牙发送的。

在这次事件之后,无论等待多久,连接都无法重建。

我的 Main 函数,包含函数调用以及用于将结果写入的缓冲区,因为我认为我可能误用了它。

void loop() {

  if (ESP_BT.available() > 0)
  {
    char *buffer = (char*) malloc(InputSize);
    getCurrentMessage(ESP_BT, buffer, InputSize);
    Serial.println(buffer);
    strncpy(currentMessage, buffer, InputSize);
    free(buffer);
  }
  if (millis() %2000 == 0){
    Serial.println("Debug");
    delay(1);
  }
} 

被调用的函数应该将 BluetoothSerial 的 inputBuffer 读入我的 Buffer。

void getCurrentMessage(BluetoothSerial ESP_BT, char* receivedChars, int InputSize)
{
 Serial.println("DEBUG: getCurrentMessageInit");
 static byte ndx = 0;

 char rc;

 while (ESP_BT.available() > 0){
   ESP_BT.println("DEBUG: Message Available");    
   Serial.println("DEBUG: Message Available");
   rc = ESP_BT.read();
   receivedChars[ndx] = rc;
   ndx++;
   delay(100);
   if (ndx >= InputSize){
     while(ESP_BT.available() > 0){
       ESP_BT.read();
     }
   }
 }
} 

我希望蓝牙连接能够继续工作。它不这样做。 我还收到错误代码“queue.c:1442 (xQueueGenericReceive)-assert failed!”当不使用延迟时,ESP 然后重新启动。 在我包含延迟后它不会这样做。

【问题讨论】:

    标签: c bluetooth arduino disconnect esp32


    【解决方案1】:

    问题是我没有通过引用调用我的蓝牙对象。 而不是给我的函数蓝牙对象我应该指向它:

    void getCurrentMessage(BluetoothSerial* ESP_BT, char* receivedChars, int InputSize)
    {
     Serial.println("DEBUG: getCurrentMessageInit");
     static byte ndx = 0;
    
     char rc;
    
     while (ESP_BT->available() > 0){
       ESP_BT->println("DEBUG: Message Available");    
       Serial.println("DEBUG: Message Available");
       rc = ESP_BT.read();
       receivedChars[ndx] = rc;
       ndx++;
       delay(100);
       if (ndx >= InputSize){
         while(ESP_BT->available() > 0){
           ESP_BT->read();
         }
       }
     }
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-01
      • 1970-01-01
      • 2021-06-29
      • 2017-01-13
      • 2012-10-09
      • 2018-06-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多