You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

添加BTSerial.available()后Arduino HC-06蓝牙模块无法连接问题

问题分析:HC-06蓝牙模块无法连接的原因

我尝试使用HC-06蓝牙模块将Arduino Nano上SD卡中的test.txt文件内容发送至电脑。原本的Arduino代码会持续通过蓝牙串口发送数据,为了优化,我添加了while (BTSerial.available() > 0)循环,结果设备完全无法连接,请问可能是什么原因?

现有代码

Arduino代码

#include <SoftwareSerial.h>
#include <SD.h>

const byte rxPin = 9;
const byte txPin = 8;
SoftwareSerial BTSerial(rxPin, txPin);
File myFile;

void setup() {
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  BTSerial.begin(9600);
  Serial.begin(9600);

  if (!SD.begin(4)){
    Serial.println("Init Failed");
    while(1);
  } else {
    Serial.println("Init Complete");
  }
}

void loop() {
  while (BTSerial.available() > 0) {
    Serial.println("connected...");
    myFile = SD.open("test.txt");
    if (myFile) {
      Serial.println("test.txt:");
      while (myFile.available()) {
        BTSerial.write(myFile.read());
      }
      BTSerial.write("\n");
      myFile.close();
    }
    else {
      Serial.println("error opening test.txt");
    }
  }
}

Python代码

# Python 3.12 on Windows
import serial

s = serial.Serial('COM5', 9600, timeout=10)
print("connected")
data=s.read_until()

print(data)

问题原因

  • 逻辑错误:你添加的while (BTSerial.available() > 0)是等待蓝牙串口接收缓冲区有数据才执行发送逻辑,但你的Python代码仅打开串口,没有发送任何触发指令,Arduino一直处于等待输入的状态,不会主动发送数据,导致两边无交互,看起来像是“无法连接”。
  • 误解BTSerial.available()的作用:这个函数仅用于检测蓝牙串口是否收到了数据,不能用来判断蓝牙是否连接。HC-06的连接状态需要通过模块上的LED判断(连接成功后慢闪,未连接快闪),你把它当成连接检测条件,完全搞错了逻辑。
  • Python代码无触发信号:你的Python代码打开串口后直接等待接收,但Arduino需要收到一个明确的指令(比如一个字符)才会启动发送流程,两边没有交互触发,自然无法完成数据传输。

修正方案

调整Arduino代码(添加指令触发逻辑)

#include <SoftwareSerial.h>
#include <SD.h>

const byte rxPin = 9;
const byte txPin = 8;
SoftwareSerial BTSerial(rxPin, txPin);
File myFile;

void setup() {
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  BTSerial.begin(9600);
  Serial.begin(9600);

  if (!SD.begin(4)){
    Serial.println("Init Failed");
    while(1);
  } else {
    Serial.println("Init Complete");
  }
}

void loop() {
  if (BTSerial.available() > 0) {
    // 读取电脑发来的触发指令
    char trigger = BTSerial.read();
    // 收到'r'指令时发送文件内容
    if (trigger == 'r') {
      Serial.println("收到触发指令,开始发送文件");
      myFile = SD.open("test.txt");
      if (myFile) {
        Serial.println("成功打开test.txt");
        // 逐字节发送文件内容
        while (myFile.available()) {
          BTSerial.write(myFile.read());
        }
        // 发送结束标记
        BTSerial.write("\n");
        myFile.close();
      } else {
        Serial.println("打开test.txt失败");
        BTSerial.write("Error: File not found\n");
      }
    }
    // 清空接收缓冲区,避免残留数据重复触发
    while(BTSerial.available()) {
      BTSerial.read();
    }
  }
}

修改Python代码(发送触发指令)

# Python 3.12 on Windows
import serial

# 打开蓝牙串口
s = serial.Serial('COM5', 9600, timeout=10)
print("串口已打开")
# 发送触发指令'r'
s.write(b'r')
# 等待接收数据
data = s.read_until()

# 解码并打印数据
print("收到数据:", data.decode('utf-8'))
# 关闭串口
s.close()

额外检查项

  • 确认HC-06的物理连接:Arduino的TX引脚接HC-06的RX引脚,Arduino的RX引脚接HC-06的TX引脚,电源需匹配HC-06的电压(一般为3.3V)。
  • 通过HC-06的LED确认连接状态:快闪表示未连接,慢闪表示已成功与电脑蓝牙配对连接。

内容的提问来源于stack exchange,提问作者Agent859

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 10:34:57