cash_app.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <errno.h>
  7. #include <sys/select.h>
  8. #include <sys/time.h>
  9. #define BUFFER_SIZE 1024
  10. void print_usage(const char *prog_name)
  11. {
  12. fprintf(stderr, "使用方法: %s <mode> <device>\n", prog_name);
  13. fprintf(stderr, " mode: 1 - 阻塞方式读取\n");
  14. fprintf(stderr, " 0 - 非阻塞方式读取\n");
  15. fprintf(stderr, " device: 字符设备路径 (例如: /dev/cash0)\n");
  16. fprintf(stderr, "示例:\n");
  17. fprintf(stderr, " %s 1 /dev/cash0 # 阻塞方式读取\n", prog_name);
  18. fprintf(stderr, " %s 0 /dev/cash0 # 非阻塞方式读取\n", prog_name);
  19. }
  20. int set_nonblocking(int fd)
  21. {
  22. int flags = fcntl(fd, F_GETFL, 0);
  23. if (flags == -1)
  24. {
  25. perror("fcntl F_GETFL");
  26. return -1;
  27. }
  28. if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
  29. {
  30. perror("fcntl F_SETFL");
  31. return -1;
  32. }
  33. return 0;
  34. }
  35. int set_blocking(int fd)
  36. {
  37. int flags = fcntl(fd, F_GETFL, 0);
  38. if (flags == -1)
  39. {
  40. perror("fcntl F_GETFL");
  41. return -1;
  42. }
  43. if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) == -1)
  44. {
  45. perror("fcntl F_SETFL");
  46. return -1;
  47. }
  48. return 0;
  49. }
  50. // 使用 select 实现阻塞读取
  51. void blocking_read(int fd)
  52. {
  53. fd_set read_fds;
  54. char buffer[BUFFER_SIZE];
  55. int ret;
  56. printf("阻塞模式:等待数据...\n");
  57. while (1)
  58. {
  59. FD_ZERO(&read_fds);
  60. FD_SET(fd, &read_fds);
  61. // NULL 表示无限等待,直到有数据可读
  62. ret = select(fd + 1, &read_fds, NULL, NULL, NULL);
  63. if (ret == -1)
  64. {
  65. if (errno == EINTR)
  66. {
  67. continue; // 被信号中断,继续等待
  68. }
  69. perror("select error");
  70. break;
  71. }
  72. if (FD_ISSET(fd, &read_fds))
  73. {
  74. memset(buffer, 0, sizeof(buffer));
  75. ssize_t n = read(fd, buffer, sizeof(buffer) - 1);
  76. if (n > 0)
  77. {
  78. buffer[n] = '\0';
  79. printf("读到 %zd 字节: %s\n", n, buffer);
  80. // 如果读到 "quit" 或 "exit" 则退出
  81. if (strncmp(buffer, "quit", 4) == 0 ||
  82. strncmp(buffer, "exit", 4) == 0)
  83. {
  84. printf("收到退出命令\n");
  85. break;
  86. }
  87. }
  88. else if (n == 0)
  89. {
  90. printf("设备已关闭 (EOF)\n");
  91. break;
  92. }
  93. else
  94. {
  95. if (errno != EAGAIN && errno != EWOULDBLOCK)
  96. {
  97. perror("read error");
  98. break;
  99. }
  100. }
  101. }
  102. }
  103. }
  104. // 使用 select 实现非阻塞读取(带超时检测)
  105. void nonblocking_read(int fd)
  106. {
  107. fd_set read_fds;
  108. struct timeval timeout;
  109. char buffer[BUFFER_SIZE];
  110. int ret;
  111. printf("非阻塞模式:\n");
  112. while (1)
  113. {
  114. memset(buffer, 0, sizeof(buffer));
  115. ssize_t n = read(fd, buffer, sizeof(buffer) - 1);
  116. if (n > 0)
  117. {
  118. printf("读到 %zd 字节: %s\n", n, buffer);
  119. }
  120. sleep(1);
  121. }
  122. }
  123. int main(int argc, char *argv[])
  124. {
  125. int mode;
  126. const char *device_path;
  127. int fd;
  128. int open_flags;
  129. // 检查参数
  130. if (argc != 3)
  131. {
  132. print_usage(argv[0]);
  133. return 1;
  134. }
  135. // 解析模式
  136. mode = atoi(argv[1]);
  137. if (mode != 0 && mode != 1)
  138. {
  139. fprintf(stderr, "错误: mode 必须是 0 或 1\n");
  140. print_usage(argv[0]);
  141. return 1;
  142. }
  143. device_path = argv[2];
  144. // 打开设备
  145. // 注意:select 对常规文件总是返回可读,但对字符设备通常能正确工作
  146. // 为了更好的演示,我们以读写方式打开
  147. open_flags = O_RDWR;
  148. fd = open(device_path, open_flags);
  149. if (fd < 0)
  150. {
  151. perror("打开设备失败");
  152. fprintf(stderr, "设备: %s\n", device_path);
  153. return 1;
  154. }
  155. printf("成功打开设备: %s\n", device_path);
  156. // 根据模式设置阻塞/非阻塞
  157. if (mode == 0)
  158. {
  159. // 非阻塞模式:设置 O_NONBLOCK 标志
  160. if (set_nonblocking(fd) < 0)
  161. {
  162. close(fd);
  163. return 1;
  164. }
  165. printf("设置为非阻塞模式\n");
  166. nonblocking_read(fd);
  167. }
  168. else
  169. {
  170. // 阻塞模式:确保清除 O_NONBLOCK 标志
  171. if (set_blocking(fd) < 0)
  172. {
  173. close(fd);
  174. return 1;
  175. }
  176. printf("设置为阻塞模式\n");
  177. blocking_read(fd);
  178. }
  179. close(fd);
  180. printf("程序退出\n");
  181. return 0;
  182. }