【发布时间】:2018-08-20 12:43:58
【问题描述】:
我一直在尝试在两个 HC-06 蓝牙模块之间建立连接。配对已完成。两个模块正在通信。我的目标是从一个模块发送一封信并从另一个模块接收确认。主模块的代码如下。
#include <SoftwareSerial.h>
SoftwareSerial BTserial(2,3); // RX, TX
char c;
char s[]="Matched";
int t[]="NotMatched";
void setup()
{
// start the serial communication with the computer
Serial.begin(9600);
Serial.println("Arduino with HC-06 is ready");
// start communication with the HC-06 using 38400
BTserial.begin(38400);
Serial.println("Bluetooth serial started at 38400");
}
void loop()
{
// Read from HC-06 and send to Arduino Serial Monitor
if (BTserial.available())
{
c=(BTserial.read());
if (c=='a')
{
Serial.write(s);
}
else
{
Serial.write(t);
}
}
// Read from Arduino Serial Monitor and send to HC-06
if (Serial.available())
{
c = Serial.read();
Serial.write(c);
BTserial.write(c);
}
}
类似的代码用于从模块。除了代码中的“其他”部分之外,一切都运行正常。我收到确认以及 else 部分被打印两次的代码的 if 和 else 部分,即“匹配不匹配不匹配”在收到字符“a”时打印,“不匹配不匹配不匹配”打印时打印它接收除 'a' 以外的任何内容。您能否就可能出现的问题给我一些建议。
【问题讨论】:
标签: bluetooth arduino-uno master-slave