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