我有一个用 C 编写的简单程序,它使用 termios 将基本字符串发送到 Raspberry Pi UART 并尝试读取和输出响应。Raspberry Pi 上的 Rx 和 Tx 引脚通过跳线连接,因此应立即接收发送的任何内容。
尽管程序输出它成功发送和接收所选字符串('Hello')的 5 个字符,尝试打印缓冲区的内容只会产生一两个垃圾字符。
该程序:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
int main(int argc, char* argv[]) {
struct termios serial;
char* str = "Hello";
char buffer[10];
if (argc == 1) {
printf("Usage: %s [device]\n\n", argv[0]);
return -1;
}
printf("Opening %s\n", argv[1]);
int fd = open(argv[1], O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) {
perror(argv[1]);
return -1;
}
if …Run Code Online (Sandbox Code Playgroud)