同时接收多个字符与软件序列 [英] Receiving multiple chars at once with Software Serial

查看:275
本文介绍了同时接收多个字符与软件序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Arduino的乌诺R3和蓝牙伴侣。
当链接的伴侣到Arduino硬件序列(引脚0,1)我可以从我的连接设备一次发送多个字符,但是当我尝试做同样的事情软件序列(例如使用引脚4,2)我只得到第一个字符与字符的其余部分都搞砸了。

I have a Arduino Uno R3 and a Bluetooth Mate. When linking the Mate to the Arduino Hardware Serial (pin 0,1) I can send multiple characters at once from my connected device but when I try to make the same thing with Software Serial (using pin 4,2 for example) I only get the first character and the rest of the chars are messed up.

我的code:

#include <SoftwareSerial.h>  

int bluetoothTx = 4;  
int bluetoothRx = 2;  

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

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

void loop()
{
  if(bluetooth.available())
  {
    Serial.print((char)bluetooth.read());  
  }
}

例如,如果我发送此字符: ABCD 从我的Andr​​oid设备我得到这个在串行显示器:一个±,ö

For example if I send this chars: abcd from my android device I get this in the serial monitor: a±,ö

这是使用硬件串行(我在我的蓝牙连接到引脚0和1)的作品就好这code:

This code that uses Hardware Serial (I link my bluetooth to pins 0 and 1) works just fine:

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

void loop()
{
  if(Serial.available())
  {
    Serial.print((char)Serial.read());  
  }
}

我甚至试图改变波特率,但它并没有帮助

I even tried changing the baud rate but it didn't helped

如果我由一个发送字符之一,它工作正常,但我希望能送他们作为一个字符串。

If I send the chars one by one it works fine but I would like to be able to send them as a string.

推荐答案

随着指出在评论这@hyperflexed是波特率相关的问题。
我不得不采取波特率低至9600,使其工作。

As pointed out by @hyperflexed in the comments this is a baudrate related issue. I had to take the baudrate as low as 9600 to make it work.

这是code的工作:

#include "SoftwareSerial.h";
int bluetoothTx = 4;
int bluetoothRx = 2;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
{
  Serial.begin(9600);
  delay(500);
  bluetooth.begin(115200);
  delay(500);
  bluetooth.print("$$$");
  delay(500);
  bluetooth.println("U,9600,N");
  delay(500);
  bluetooth.begin(9600);
}

void loop()
{
  if(bluetooth.available()) {
    char toSend = (char)bluetooth.read();
    Serial.print(toSend);
  }

  if(Serial.available()) {
    char toSend = (char)Serial.read();
    bluetooth.print(toSend);
  }
}

有关更改波特率我不得不把一些大的延迟,以确保命令被执行,否则它将无法工作。

For changing the baudrate I had to put some big delays to make sure the commands are executed otherwise it won't work.

这篇关于同时接收多个字符与软件序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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