main.c 742 B

1234567891011121314151617181920212223242526272829303132
  1. #include <fcntl.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  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: %ld", fd);
  24. // 写入字符串
  25. char *str = "------fuck-----";
  26. int len = write(fd, str, strlen(str));
  27. printf("len: %ld", len);
  28. // 关闭文件
  29. close(fd);
  30. return 0;
  31. }