42 lines
946 B
C
42 lines
946 B
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <fcntl.h>
|
|
#include <termios.h>
|
|
#include <string.h>
|
|
|
|
int main() {
|
|
int fd;
|
|
struct termios options;
|
|
|
|
// 打开 UART 设备文件
|
|
fd = open("/dev/ttyS3", O_RDWR | O_NOCTTY | O_NDELAY);
|
|
if (fd == -1) {
|
|
perror("open_port: Unable to open UART device");
|
|
return -1;
|
|
}
|
|
|
|
// 配置 UART 设备参数
|
|
tcgetattr(fd, &options);
|
|
options.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
|
|
options.c_iflag = IGNPAR;
|
|
options.c_oflag = 0;
|
|
options.c_lflag = 0;
|
|
tcflush(fd, TCIFLUSH);
|
|
tcsetattr(fd, TCSANOW, &options);
|
|
|
|
// 读取和写入 UART 数据
|
|
char buf[128];
|
|
//memset(buf, 0x5A, sizeof(buf));
|
|
//write(fd, buf, 78);
|
|
//sleep(1);
|
|
int n = read(fd, buf, sizeof(buf));
|
|
if (n > 0) {
|
|
printf("Received data: %d, %s\n", n, buf);
|
|
}
|
|
|
|
write(fd, "Hello, UART!\n", 13);
|
|
|
|
close(fd);
|
|
return 0;
|
|
} |