使用蓝牙配合与Arduino UNO和Mac进行串行通信

chi*_*oms 5 macos bluetooth arduino serial-communication

我正在尝试通过蓝牙在Arduino和Mac之间建立串行通信并遇到问题.

我的环境是这样的:

  • Arduino UNO
  • 来自sparkfun的蓝牙伴侣
  • MacBook,OS X 10.7

首先,我编写了如下所示的arduino,如本教程所示.

/***********************
 Bluetooth test program
***********************/

int counter = 0;
int incomingByte;

void setup() {
  Serial.begin(115200);
}

void loop() {
  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
    // if it's a capital R, reset the counter
    if (incomingByte == 'R') {
      Serial.println("RESET");
      counter=0;
    }
  }

  Serial.println(counter);
  counter++;

  delay(250);
}
Run Code Online (Sandbox Code Playgroud)

当Arduino连接USB时,它工作得很好.(Arduino控制台接收数字序列,如1,2,3,4 ......带换行符.)

然后我用一些电线连接Arduino UNO和蓝牙伴侣,并成功与Mac配对.

当我在iTerm上运行这一行时,我只收到了问号序列.

$ sudo cu -s 115200 -l /dev/tty.name-of-port
Connected.
??????????????????????????????
Run Code Online (Sandbox Code Playgroud)

我也试过screen /dev/tty.name-of-port,或Arduino控制台,但结果是一样的.

如何解决这些乱码信号并接收正确的字符?谢谢.

red*_*yes -1

可能是波特率高的原因。尝试将其减少到 9600。我感觉 115200 对于串行连接来说太多了。