1*b087521eSChiichen #include <fcntl.h> 2*b087521eSChiichen #include <stdio.h> 3*b087521eSChiichen #include <stdlib.h> 4*b087521eSChiichen #include <string.h> 5*b087521eSChiichen #include <unistd.h> 6*b087521eSChiichen 7*b087521eSChiichen int main() { 8*b087521eSChiichen // 打开设备文件 9*b087521eSChiichen int fd = open("/dev/char/uart:1088", O_WRONLY | O_NONBLOCK); 10*b087521eSChiichen char buf[1] = {0}; 11*b087521eSChiichen int n; 12*b087521eSChiichen memset(buf, 0, 1); 13*b087521eSChiichen while (1) { 14*b087521eSChiichen n = read(fd, buf, 1); 15*b087521eSChiichen close(fd); 16*b087521eSChiichen fd = open("/dev/char/uart:1088", O_WRONLY | O_NONBLOCK); 17*b087521eSChiichen if (n != 0) { // 添加字符串结束符 18*b087521eSChiichen printf("Received: %s\n", buf); // 打印接收到的数据 19*b087521eSChiichen if (buf[0] == 'g') { 20*b087521eSChiichen break; 21*b087521eSChiichen } 22*b087521eSChiichen } 23*b087521eSChiichen } 24*b087521eSChiichen printf("fd: %ld", fd); 25*b087521eSChiichen // 写入字符串 26*b087521eSChiichen char *str = "------fuck-----"; 27*b087521eSChiichen int len = write(fd, str, strlen(str)); 28*b087521eSChiichen printf("len: %ld", len); 29*b087521eSChiichen // 关闭文件 30*b087521eSChiichen close(fd); 31*b087521eSChiichen return 0; 32*b087521eSChiichen }