【发布时间】:2015-03-07 22:07:00
【问题描述】:
我有一个通过 USB 连接到 Arduino Mega 的 Raspberry Pi,并尝试将一个 8 字节的包从 RPI 发送到 Arduino。我正在使用 WiringPi 串行库来完成此操作。 RPI 部分扫描串行缓冲区以获取可用数据。如果没有,它将数据写入串行。
for(;;) {
if (int i = serialDataAvail(f_)) {
printf("RPI received %d bytes\n", i);
uint8_t buf[i];
read(f_, buf, i);
printf("%s",buf);
}
..some code filling variable widthsChar with data...
write(f_, widthsChar, sizeof(widthsChar));
printf("amount sent: %d \n", sizeof(widthsChar));
usleep(3000000); //3 sec;
}
这里有一个 Arduino 部分,它等待 RPI 数据到达并以接收到的字节数进行响应。
while(Serial.available() == 0);
String bytesNum = String(Serial.available());
String newString = "Arduino received " + bytesNum + " bytes\n";
Serial.print(newString);
delay(10000);
我猜,输出应该是这样的:
amount sent: 8
//waits for 3 sec
RPI received 25 bytes
Arduino received 8 bytes
amount sent: 8
//and so far
我在现实中得到的不是我可以解释并要求有人帮助理解它的东西。 就是这样:
amount sent: 8
//nothings happens for 3 sec
amount sent: 8
RPI received 25 bytes
Arduino received 1 bytes
W??1??0mount sent: 8
// I tried to interpret random chars it displays that way
//and so far
【问题讨论】:
-
uint8_t buf[i];- 我不知道这是如何编译的,因为i不是恒定的。您需要动态分配。 -
我相信它每次迭代都会创建一个包含 i 个元素的 uint8_t 数组,该数组在迭代结束后会销毁。我不明白为什么它不应该编译。
-
在 VS2013 中对我不起作用,在单独的范围内也不适用。
-
哦,抱歉 - 如果您在
if条件下声明它确实有效。
标签: c++ serial-port arduino usb raspberry-pi