6

我的目的是使用 Arduino 建立 PC 和使用 HC-05 蓝牙模块的 Android 设备之间的通信。

我使用 PC 和 Arduino(串行监视器)和 SoftwareSerial 之间的 USB 通信连接到 HC-05。

我的问题是从 BT 到 PC 的通信工作良好,但在其他方式下不能按预期工作。从 PC 发送到 BT 时,只有当我关闭 PC 上的串行监视器或重置 Arduino 时,BT 设备才会接收到所有发送的字符。

我已经排除了 BT 模块或 Android 应用程序的问题,因为如果在 Arduino 中实现“ECHO”代码(在 Android 中编写并在 Android 中发送),一切正常。

使用下面发布的 Arduino 代码,预期的行为是:Arduino reset-> Hello word sent,串行监视器打开-> 没有任何反应,串行监视器上写入的字符-> BT 上接收到的字符,BT 上写入的字符-> 串行监视器上接收到的字符, 串行监视器关闭-> 没有任何反应。

真正的行为是:Arduino重置->发送你好字,串行监视器打开-> BT上的2个你好字和PC上的1(“晚安”),串行监视器上写的字符->没有,BT上写的字符->收到字符在串行监视器上,串行监视器已关闭-> 接收到串行监视器中先前写入的字符 + Hello Word。

我该如何解决这个问题?

代码:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
int a=0;
char c;
char d;
void setup() {
  Serial.begin(9600);
  Serial.println("Goodnight moon!");
  mySerial.begin(9600);
  mySerial.println("Hello, world?");
}
void loop() {
  delay(10);
  if (Serial.available()) {
    c=Serial.read();
    delay(10);
    Serial.write(c);
  }
  delay(10);
  if (mySerial.available()) {
    d=mySerial.read();
    delay(10);
    mySerial.write(d);

  }
}
4

6 回答 6

4

此代码适用于带有 HC-05 的 Arduino Mini Pro(应该与 UNO 相同)。我将 HC-05 与我的笔记本电脑配对。在与 HC-05 和 Arduino 串行控制台关联的 COM 端口上使用超级终端,我可以双向发送消息。Serial.println 语句像应有的那样显示在超级终端窗口中。

#include <SoftwareSerial.h>

#define rxPin 8
#define txPin 7

SoftwareSerial mySerial(rxPin, txPin); // RX, TX
char myChar ; 

void setup() {
  Serial.begin(9600);   
  Serial.println("Goodnight moon!");

  mySerial.begin(9600);
  mySerial.println("Hello, world?");
}

void loop(){
  while(mySerial.available()){
    myChar = mySerial.read();
    Serial.print(myChar);
  }

  while(Serial.available()){
   myChar = Serial.read();
   mySerial.print(myChar);
  }
}
于 2013-11-01T03:35:41.447 回答
2

我已经实现了 Arduino Uno 和 PC 之间的串行通信,这是我的代码,希望它可以帮助:

int data;
char character;
int start_flag = 0;

void setup() {
  Serial.begin(921600); 
  pinMode(2, OUTPUT); 
}
void loop() {
  if(Serial.available() > 0){
    character = (char) Serial.read();
    if(character == 's') {
      start_flag = 1;
    }
    if(character == 't') {
      start_flag = 0;
    }
  }
  if (start_flag == 1) {
    Serial.print(data); //data that was acquired by internal ADC
  }
}
于 2013-09-25T18:35:50.337 回答
0

使用此串行设置。使用此代码,我可以从串行监视器接收日期并将其发送到蓝牙

void setup(){
  Serial.begin(9600); // Begin the serial monitor at 9600bps
  bluetooth.begin(115200); // The Bluetooth Mate defaults to 115200bps
  bluetooth.print("$"); // Print three times individually
  bluetooth.print("$");
  bluetooth.print("$"); // Enter command mode
  delay(100); // Short delay, wait for the Mate to send back CMD
  bluetooth.println("U,9600,N"); // Temporarily Change the baudrate to 9600, no parity
  // 115200 can be too fast at times for NewSoftSerial to relay the data reliably
  bluetooth.begin(9600); // Start bluetooth serial at 9600
  pinMode(led, OUTPUT);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
}

欲了解更多信息,请访问 http://www.circuitmagic.com/arduino/arduino-and-bluetooth-hc-06-to-control-the-led-with-android-device/

于 2015-02-02T18:24:05.213 回答
0

你可以试试这个。这是您在测试 Arduino 蓝牙 <-> C# 通信时可以使用的最简单的代码。注意:通过连接 PIN1(TX) <-> MODULE RX、PIN2(RX) <-> MODULE TX 并将 PIN1(TX) 5V 分压为 2.5V,然后再将其馈送到模块来测试代码。

希望这对所有正在尝试这个的人有所帮助!

于 2014-08-26T08:59:32.180 回答
0

我建议使用此应用程序进行测试:

https://play.google.com/store/apps/details?id=com.vagoscorp.virtualterminal

它让您可以将字节作为字节查看和发送(从 0b00000000 到 0b11111111(十进制为 0 到 255)的数字),因此您可以制作一个简单的回显固件来测试您的波特率是否正确,并且在该工作正常的情况下,开始发送命令以打开/关闭一些 LED

这是一个回显代码示例:

char dato = 0;

void setup() {
 Serial.begin(9600);//9600 is the default baudrate of the HC-05 (you can change it by AT commands, google it if you want)
 //pinMode(13, OUTPUT); //enable this pin if you want to use de LED idea
 //digitalWrite(13,  HIGH);
}
//////////////////////////////////////////////////////////////////////////
void serialEvent() {  //if you have received serial data
    while (Serial.available() > 0) {
        char dato = (byte)Serial.read();//save the byte
        Serial.write(dato);//send the just received byte (echo)
    }
}
///////////////////////////////////////////////////////////////////////////
void loop() {

}

希望对你有帮助

于 2015-08-10T18:51:05.670 回答
0

遇到同样的问题,您必须在有线端和无线电端将 BT 模块视为 2 个不同的波特率。无线电端由您通过腻子连接的任何东西设置,有线端通过 AT 命令编程。HC-05 默认为 38400。

于 2015-12-08T11:02:15.920 回答