gpt4 book ai didi

c++ - 如何通过带有 UARTSerial 类的 USB 在 nucleo f446re 和 mbed 之间发送字节数组?

转载 作者:太空宇宙 更新时间:2023-11-04 12:34:43 27 4
gpt4 key购买 nike

我必须使用 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
[...]

如您所见,结果与我预期的不同。

我已经尝试通过循环一次发送一个字节,但结果是一样的。

我不明白为什么我无法读取 USB 我已经尝试在电脑和 nucleo 板上刷新 USB。

最佳答案

你必须使用解码器来解码来自串口的字节请参阅以下链接: https://codereview.stackexchange.com/questions/200846/a-simple-and-efficient-packet-frame-encoder-decoder

关于c++ - 如何通过带有 UARTSerial 类的 USB 在 nucleo f446re 和 mbed 之间发送字节数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56936179/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com