我正在尝试使用Arduino Uno和来自Sparkfun的BlueSmirf Bluetooth模块(documentation)进行简单的实验。
我的硬件设置如下:
Arduino(power through USB)->BlueSmirf ---(bluetooth)--> PC(no wired connection the the Arduino)->RealTerm
在Arduino上,以下草图正在运行:
#include <SoftwareSerial.h>
int txPin = 2;
int rxPin = 3;
SoftwareSerial bluetooth(txPin, rxPin);
void setup() {
bluetooth.begin(115200);
delay(100);
}
void loop() {
String textToSend = "abcdefghijklmnopqrstuvw123456789";
bluetooth.print(textToSend);
delay(5000);
}
现在,蓝牙连接到PC很好,但是当我检查RealTerm中的COM端口时,我只得到以下输出:
abdhp1248
其余的字母和数字去了哪里?看起来所有跟随2的幂的字母(即a = 1,b = 2,d = 4,h = 8,p = 16)打印,但其余的都没有。这只是巧合吗?
答案 0 :(得分:1)
我认为您运行串口的速度太快了。根据{{3}}中的sparkfun BlueSmirf示例中的注释 - &#34; 115200有时可能太快,以便NewSoftSerial可靠地中继数据&#34;。
使用下面的代码示例将波特率降低到9600,从上面的网页进行了修改。
/*
Example Bluetooth Serial Passthrough Sketch
by: Jim Lindblom
SparkFun Electronics
date: February 26, 2013
license: Public domain
This example sketch converts an RN-42 bluetooth module to
communicate at 9600 bps (from 115200), and passes any serial
data between Serial Monitor and bluetooth module.
*/
#include <SoftwareSerial.h>
int bluetoothTx = 2; // TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 3; // RX-I pin of bluetooth mate, Arduino D3
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup()
{
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
}
void loop()
{
String textToSend = "abcdefghijklmnopqrstuvw123456789";
bluetooth.print(textToSend);
delay(5000);
}