我正在通过windows窗体应用程序将一个字节数组传递到arduino板上的bluesmirf中。我能够从蓝牙读取信号,并通过串行打印出来,但输出总是254,我不知道为什么。
下面是C++代码。
const int led = 8;
int incomingByte = 0;
#define RxD 10
#define TxD 11
#include <hmi.h>
SoftwareSerial mySerial(RxD, TxD); // RX pin, TX pin
void setup() {
pinMode(led, OUTPUT);
pinMode(RxD, INPUT);
pinMode(TxD, OUTPUT);
Serial.begin(9600);
mySerial.begin(11520);
}
void loop() {
incomingByte = mySerial.read();
if(incomingByte > 0){
Serial.println("Here is: ");
Serial.println(incomingByte);
digitalWrite(led,HIGH);
}
}这是相关的c#代码。
int MyInt = 1;
byte[] b = BitConverter.GetBytes(MyInt);
serialPort1.Write(b, 0, 4);我能够使用c#代码将代码输出到248:
serialPort1.Write(new Byte[] { 1, 1, 1, 1 }, 0 , 4);192人
byte[] b = { 0, 1, 1, 1, 1, 1, 1, 1 };
serialPort1.Write(b, 0, 7);但我不知道为什么,返回的数字似乎是基于字节数组中传递的元素数,而不管数组中的实际内容是什么。
发布于 2017-04-11 20:42:02
这个问题是随机解决的,而我只是在敲它。
我认为问题的解决办法是,我将波特率设定为11520,而不是Gre_Gor所说的115200。
发布于 2017-04-11 06:48:58
你应该尝试降低波特率。SoftwareSerial占用了大量的CPU,微控制器可能无法处理所有的数据。
参考文献:https://arduino.stackexchange.com/a/30117/32059,http://forum.arduino.cc/index.php?topic=96271.0和http://forum.arduino.cc/index.php?topic=140299.msg1063344#msg1063344
https://stackoverflow.com/questions/43302026
复制相似问题