Arduino:将串行和软件串行与蓝牙模块一起使用 [英] Arduino: using Serial and Software Serial with bluetooth module

查看:138
本文介绍了Arduino:将串行和软件串行与蓝牙模块一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My purpose is to use Arduino to set up communication between a PC and an Android device using an HC-05 bluetooth module.

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

I use the USB communication between the PC and the Arduino (Serial Monitor) and a SoftwareSerial to connect to the HC-05.

我的问题是,从BT到PC的通讯正常,但不能以其他方式正常工作.从PC发送到BT时,仅当我关闭PC上的串行监视器或重置Arduino时,BT设备才会接收所有发送的字符.

My problem is that the communication works well from BT to the PC, but doesn't work as expected in the other way. When sending from the PC to BT all the characters sent are received by the BT device only when I close the Serial Monitor on the PC or when I reset the Arduino.

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

I've excluded a problem with the BT Module or the Android application because if in Arduino I implement an "ECHO" code (write in Android and the send in Android) everything works fine.

将Arduino代码张贴在下面的预期行为是:Arduino重置->发送Hello字,打开串行监视器->没有任何反应,在串行监视器上写入字符->在BT上接收字符,在BT上写入字符->接收到字符在串行监视器上,串行监视器关闭->什么也没发生.

With the Arduino code posted below the expected behaviour is: Arduino reset-> Hello word sent, Serial monitor opened-> nothing happens, character written on serial monitor-> character received on BT, character written on BT-> character received on Serial Monitor, Serial monitor closed-> nothing happens.

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

The real behaviour is: Arduino reset-> Hello word sent, Serial monitor opened-> 2 Hello word on BT and 1 ("goodnight") on PC, character written on serial monitor-> nothing, character written on BT-> character received on Serial Monitor, Serial monitor closed-> previous written character(s) in serial monitor received + 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);

  }
}

推荐答案

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

This code is working for me on an Arduino Mini Pro (should be the same as UNO) with an HC-05. I have the HC-05 paired with my laptop. Using HyperTerminal on the COM port associated with the HC-05 and the Arduino serial console, I can send messages bidirectionally. The Serial.println statements show up in the Hyperterminal window like they should.

#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);
  }
}

这篇关于Arduino:将串行和软件串行与蓝牙模块一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆