gpt4 book ai didi

c - 为什么 UART 上的 rx 没有收到来自环回 tx 的数据?

转载 作者:行者123 更新时间:2023-11-30 14:39:58 25 4
gpt4 key购买 nike

我有a system运行Linux,将rx从UART1连接到tx(环回)并执行以下代码:

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>

int main(void) {
int iFd = open("/dev/ttyTHS0", O_RDWR);
if (iFd < 0) {
fprintf(stdout, "Error when opening file.\n");
return -1;
}

while (1) {
if (write(iFd, "A", strlen("A")) == -1) {
fprintf(stdout, "Failed to write\n");
return -1;
}

char buff[10] = {
0
};

if (read(iFd, buff, sizeof(buff)) < 0) {
fprintf(stdout, "Failed to read.\n");
return -1;
}

fprintf(stdout, "read: %s\n", buff);

}
close(iFd);

return 0;
}

问题是它似乎没有收到任何数据。该代码只是在读取处阻塞。当用示波器查看时,我可以看到 tx 线路上正在传输的数据,但 rx 没有接收到任何内容。我还尝试将此代码拆分为 2 个进程,一个进程用于传输,另一个进程用于接收,但这也不起作用。

最佳答案

这是合乎逻辑的。您可能没有发送任何内容,因为它位于发送缓冲区中。另一个问题是您尝试接收 10 个字节,但只发送一个。在许多系统上,串行超时设置为无限时间,您将永远等待接下来的 9 个字节。

做什么(第 2 点有一些变体):

  1. 刷新 UART 文件

2a。只读取您发送的字节数ioctl(ifd, FIONREAD, &bytes_ready_to_read); 然后只读取可用的数字。

2c。更改超时。

关于c - 为什么 UART 上的 rx 没有收到来自环回 tx 的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55791193/

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