Arduino 与 HC-05 连接时,串行通信无法正常工作
Serial Communication is not working properly in Arduino connection with HC-05
我是 Arduino 编程新手。
这段代码工作正常。它提供正确的输出并且运行良好。但我想自动编写命令而不用重复输入。
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // CONNECT BT RX PIN TO ARDUINO 11 PIN | CONNECT BT TX PIN TO ARDUINO 10 PIN
void setup()
{
pinMode(9, OUTPUT); // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
digitalWrite(9, HIGH);
Serial.begin(38400);
Serial.println("Enter AT commands:");
BTSerial.begin(38400); // HC-05 default speed in AT command more
}
void loop()
{
if (BTSerial.available()) {
Serial.write(BTSerial.read());
}
if(Serial.available()){
BTSerial.write(Serial.read());
}
}
所以我尝试在上面的代码中进行以下更改,但我无法收到任何响应。
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // CONNECT BT RX PIN TO ARDUINO 11 PIN | CONNECT BT TX PIN TO ARDUINO 10 PIN
void setup()
{
pinMode(9, OUTPUT); // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
digitalWrite(9, HIGH);
Serial.begin(38400);
Serial.println("Enter AT commands:");
BTSerial.begin(38400); // HC-05 default speed in AT command more
BTSerial.write("AT");
}
void loop()
{
if (BTSerial.available()) {
Serial.write(BTSerial.read());
}
}
我也试过了BTSerial.print("AT");
还是没有反应
AT 命令区分大小写,并且应该以结束符结尾,例如 enter
击键或 \r\n
.
应该这样做:
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // CONNECT BT RX PIN TO ARDUINO 11 PIN | CONNECT BT TX PIN TO ARDUINO 10 PIN
void setup()
{
pinMode(9, OUTPUT); // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
digitalWrite(9, HIGH);
Serial.begin(38400);
Serial.println("Enter AT commands:");
BTSerial.begin(38400); // HC-05 default speed in AT command more
BTSerial.write("AT\r\n");
}
void loop()
{
if (BTSerial.available()) {
Serial.write(BTSerial.read());
}
}
我是 Arduino 编程新手。
这段代码工作正常。它提供正确的输出并且运行良好。但我想自动编写命令而不用重复输入。
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // CONNECT BT RX PIN TO ARDUINO 11 PIN | CONNECT BT TX PIN TO ARDUINO 10 PIN
void setup()
{
pinMode(9, OUTPUT); // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
digitalWrite(9, HIGH);
Serial.begin(38400);
Serial.println("Enter AT commands:");
BTSerial.begin(38400); // HC-05 default speed in AT command more
}
void loop()
{
if (BTSerial.available()) {
Serial.write(BTSerial.read());
}
if(Serial.available()){
BTSerial.write(Serial.read());
}
}
所以我尝试在上面的代码中进行以下更改,但我无法收到任何响应。
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // CONNECT BT RX PIN TO ARDUINO 11 PIN | CONNECT BT TX PIN TO ARDUINO 10 PIN
void setup()
{
pinMode(9, OUTPUT); // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
digitalWrite(9, HIGH);
Serial.begin(38400);
Serial.println("Enter AT commands:");
BTSerial.begin(38400); // HC-05 default speed in AT command more
BTSerial.write("AT");
}
void loop()
{
if (BTSerial.available()) {
Serial.write(BTSerial.read());
}
}
我也试过了BTSerial.print("AT");
还是没有反应
AT 命令区分大小写,并且应该以结束符结尾,例如 enter
击键或 \r\n
.
应该这样做:
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // CONNECT BT RX PIN TO ARDUINO 11 PIN | CONNECT BT TX PIN TO ARDUINO 10 PIN
void setup()
{
pinMode(9, OUTPUT); // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
digitalWrite(9, HIGH);
Serial.begin(38400);
Serial.println("Enter AT commands:");
BTSerial.begin(38400); // HC-05 default speed in AT command more
BTSerial.write("AT\r\n");
}
void loop()
{
if (BTSerial.available()) {
Serial.write(BTSerial.read());
}
}