我试图通过串行通信将数据发送到Arduino。我使用缓冲区存储字节,我想发送然后使用fwrite
命令将它们写入串口。
发件人(Matlab)
disp(bufferSend);
fwrite(xbee,bufferSend,'uint8');
disp('sent:');
xbee.ValuesSent
display命令显示已发送 255 字节:
2 1 0 0 0 1 1 13 17 0 30 0 12 14 0 0 0 1 0
[...] 0 0 0 [...] 0
sent:
255
接收者(Arduino)
接收值正确,直到第7个字节。仅收到 70字节;
Arduino串行监视器使用以下代码打印一个字节:
byte getData;
[...]
while(xbee.available()>0)
{
getData = xbee.read();
Serial.print("Received: ");
Serial.print(getData);
Serial.print(" Bin: ");
Serial.print(getData,BIN);
Serial.print(" Hex: ");
Serial.print(getData,HEX);
Serial.print(" cont: ");
Serial.print(cont);
Serial.println();
[...]
}
这是输出:
Matlab communication established.
Received: 2 Bin: 10 Hex: 2 cont: 2
Received: 1 Bin: 1 Hex: 1 cont: 3
Received: 0 Bin: 0 Hex: 0 cont: 4
Received: 0 Bin: 0 Hex: 0 cont: 5
Received: 0 Bin: 0 Hex: 0 cont: 6
Received: 1 Bin: 1 Hex: 1 cont: 7
Received: 1 Bin: 1 Hex: 1 cont: 8
Received: 141 Bin: 10001101 Hex: 8D cont: 9
Received: 145 Bin: 10010001 Hex: 91 cont: 10
Received: 128 Bin: 10000000 Hex: 80 cont: 11
Received: 143 Bin: 10001111 Hex: 8F cont: 12
Received: 128 Bin: 10000000 Hex: 80 cont: 13
Received: 134 Bin: 10000110 Hex: 86 cont: 14
Received: 135 Bin: 10000111 Hex: 87 cont: 15
Received: 128 Bin: 10000000 Hex: 80 cont: 16
Received: 128 Bin: 10000000 Hex: 80 cont: 17
Received: 128 Bin: 10000000 Hex: 80 cont: 18
Received: 128 Bin: 10000000 Hex: 80 cont: 19
Received: 128 Bin: 10000000 Hex: 80 cont: 20
Received: 128 Bin: 10000000 Hex: 80 cont: 21
Received: 128 Bin: 10000000 Hex: 80 cont: 22
Received: 128 Bin: 10000000 Hex: 80 cont: 23
Received: 128 Bin: 10000000 Hex: 80 cont: 24
Received: 128 Bin: 10000000 Hex: 80 cont: 25
Received: 128 Bin: 10000000 Hex: 80 cont: 26
Received: 128 Bin: 10000000 Hex: 80 cont: 27
Received: 128 Bin: 10000000 Hex: 80 cont: 28
Received: 128 Bin: 10000000 Hex: 80 cont: 29
Received: 128 Bin: 10000000 Hex: 80 cont: 30
Received: 128 Bin: 10000000 Hex: 80 cont: 31
Received: 128 Bin: 10000000 Hex: 80 cont: 32
Received: 128 Bin: 10000000 Hex: 80 cont: 33
Received: 128 Bin: 10000000 Hex: 80 cont: 34
Received: 64 Bin: 1000000 Hex: 40 cont: 35
Received: 64 Bin: 1000000 Hex: 40 cont: 36
Received: 64 Bin: 1000000 Hex: 40 cont: 37
Received: 64 Bin: 1000000 Hex: 40 cont: 38
Received: 64 Bin: 1000000 Hex: 40 cont: 39
Received: 64 Bin: 1000000 Hex: 40 cont: 40
Received: 64 Bin: 1000000 Hex: 40 cont: 41
Received: 64 Bin: 1000000 Hex: 40 cont: 42
Received: 64 Bin: 1000000 Hex: 40 cont: 43
Received: 64 Bin: 1000000 Hex: 40 cont: 44
Received: 64 Bin: 1000000 Hex: 40 cont: 45
Received: 64 Bin: 1000000 Hex: 40 cont: 46
Received: 64 Bin: 1000000 Hex: 40 cont: 47
Received: 64 Bin: 1000000 Hex: 40 cont: 48
Received: 64 Bin: 1000000 Hex: 40 cont: 49
Received: 64 Bin: 1000000 Hex: 40 cont: 50
Received: 64 Bin: 1000000 Hex: 40 cont: 51
Received: 64 Bin: 1000000 Hex: 40 cont: 52
Received: 64 Bin: 1000000 Hex: 40 cont: 53
Received: 64 Bin: 1000000 Hex: 40 cont: 54
Received: 64 Bin: 1000000 Hex: 40 cont: 55
Received: 64 Bin: 1000000 Hex: 40 cont: 56
Received: 64 Bin: 1000000 Hex: 40 cont: 57
Received: 64 Bin: 1000000 Hex: 40 cont: 58
Received: 64 Bin: 1000000 Hex: 40 cont: 59
Received: 32 Bin: 100000 Hex: 20 cont: 60
Received: 32 Bin: 100000 Hex: 20 cont: 61
Received: 32 Bin: 100000 Hex: 20 cont: 62
Received: 32 Bin: 100000 Hex: 20 cont: 63
Received: 32 Bin: 100000 Hex: 20 cont: 64
Received: 0 Bin: 0 Hex: 0 cont: 65
Received: 0 Bin: 0 Hex: 0 cont: 66
Received: 0 Bin: 0 Hex: 0 cont: 67
Received: 0 Bin: 0 Hex: 0 cont: 68
Received: 0 Bin: 0 Hex: 0 cont: 69
Received: 0 Bin: 0 Hex: 0 cont: 70
Received: 0 Bin: 0 Hex: 0 cont: 71
[...]
如果将eigth值(即13)放在缓冲区的第一个位置,Arduino会正确接收它。
它与fwrite
函数和缓冲区维度有关吗?这是缓冲区溢出的情况吗?
解决方案
降低波特率!在我的情况下,19200工作
答案 0 :(得分:1)
这看起来像硬件问题。看起来这些值的位被破坏了。
141是13,第一位翻转
145是17,第一位翻转
128为0,第一位翻转
143向右移动30,第一位翻转
你还没有说过matlab是如何与xbee连接的。问题可能是不兼容的电压水平或波特率?像这样的问题会显示第一个字节的症状是正常的,然后随着通信不同步,字节变坏。如果这是问题,那么错误会出现一些随机性。对于给定的字节集,接收的值是否始终相同?
xbee模块之间的通信可能没问题,因为应该有一个校验和来确保数据包没有损坏。
它留下了matlab和第一个xbee之间的串行通信,以及屏蔽和arduino之间的串行通信。
您还可以尝试Serial.readBytes()
来看看是否会产生影响。
答案 1 :(得分:1)
你所经历的是一个简单的例子,太多的事情,没有足够的时间去做。这就是为什么最好的串行I / O例程运行一个中断的原因,它基本上强制所有程序级处理在一个字节进入接收移位寄存器(通常是UART)时停止,这样它就可以保存在在下一个字节进入之前的缓冲区。在没有中断例程的情况下,您无法编写代码来处理每个字节,因为如您所见,您可能会面临无法返回主要任务的风险。快速保存字节。你可能不认为自己是"处理"每个字节,但print()函数有很大的开销。
我建议重新编写你的接收例程,除了将字节添加到缓冲区,保持接收的字节数,并密切关注收到NO DATA已经过了多少时间。在这里,您必须做出多长时间才能确定传入消息的结束时间。然后......当消息完全存储后,您可以返回并打印出您复活的分析。按照这种方法,您几乎肯定能够在接近最大波特率的情况下运行! ; - )