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