【发布时间】:2019-11-18 01:02:01
【问题描述】:
我必须使用 UARTSerial 类在 nucleo f446re 和带有 ubuntu 的 pc 之间发送数据数组。
我在 mbed 上使用的代码如下:
int main() {
UARTSerial pc(USBTX, USBRX, 921600);
uint8_t buff[256] = {
5, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4
};
pc.sync();
while(true) {
pc.write(buff, 23);
pc.sync();
wait(1);
}
return 0;
}
我在电脑上运行的代码是:
int main() {
struct termios tattr{0};
// open the device in read/write sync
int com = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_SYNC );
if (com == -1)
throw std::runtime_error("ERROR: can't open the serial");
tcgetattr(com, &tattr);
tattr.c_iflag &= ~(INLCR|IGNCR|ICRNL|IXON);
tattr.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET);
tattr.c_cflag = CS8 | CREAD | CLOCAL;
tattr.c_lflag &= ~(ICANON|ECHO);
tattr.c_cc[VMIN] = 1;
tattr.c_cc[VTIME] = 0;
tattr.c_ispeed = 921600;
tattr.c_ospeed = 921600;
tcsetattr (com, TCSAFLUSH, &tattr);
while (true) {
usleep(1000);
tcflush(com, TCIOFLUSH);
uint8_t buff[24];
::read(com, buff, 23);
printf("reading frame... ");
for (auto b : buff) {
printf("%02X ", b);
}
puts("\n");
}
}
我在电脑上收到的输出是:
[...]
reading frame... 00 00 8D 9C 1E 7F 00 00 00 00 00 00 00 00 00 00 70 5B C7 01 AD 55 00 00
reading frame... 00 00 8D 9C 1E 7F 00 00 00 00 00 00 00 00 00 00 70 5B C7 01 AD 55 00 00
[...]
如您所见,结果与我预期的不一样。
我已经尝试使用循环一次发送一个字节,但结果是一样的。
我不明白为什么我无法读取我尝试在 pc 和核板上刷新 USB 的 USB。
【问题讨论】: