歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Linux下RFID卡(門禁卡,Mifare卡)的編程

Linux下RFID卡(門禁卡,Mifare卡)的編程

日期:2017/3/1 10:24:28   编辑:Linux編程

我使用的是串口讀卡器,RFID卡是philips的Mifare-M1卡。操作讀卡器,就是操作串口設備。串口設備的基礎只是,請參考 https://www.ibm.com/developerworks/cn/linux/l-serials/ ,此文講得很詳細。

在嵌入式平台下,串口設置需要做得更全一些,以避免一些特殊字符問題。本文描述了一種用select進行非阻塞方式讀取的方法,方便了應用程序進行整合。

1,打開串口

  1. int open_comm (const char* device) {
  2. if (device == NULL) return -1;
  3. int fd = open (device, O_RDWR|O_NOCTTY|O_NDELAY);
  4. return fd;
  5. }
2,設置串口
  1. static int speed_arr[] = {B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300,
  2. B38400, B19200, B9600, B4800, B2400, B1200, B300, };
  3. static int name_arr[] = { 115200, 38400, 19200, 9600, 4800, 2400, 1200, 300,
  4. 38400, 19200, 9600, 4800, 2400, 1200, 300, };
  5. int set_options (int fd, int speed, int databits, int stopbits, int parity) {
  6. struct termios options;
  7. int status = 0;
  8. if ((status = tcgetattr (fd, &options)) != 0) {
  9. ERROR ("fail: status=%d, %s", status, strerror (errno));
  10. return -1;
  11. }
  12. int bSpeed = -1;
  13. for (int i = 0; i < sizeof(speed_arr) / sizeof(int); i++) {
  14. if (speed == name_arr[i]) {
  15. bSpeed = speed_arr[i];
  16. break;
  17. }
  18. }
  19. if (bSpeed == -1) {
  20. ERROR ("wrong speed=%d", speed);
  21. return -1;
  22. }
  23. cfsetispeed (&options, bSpeed);
  24. cfsetospeed (&options, bSpeed);
  25. /*允許接收並且設置為本地模式*/
  26. options.c_cflag |= (CLOCAL|CREAD);
  27. /*設置數據位數*/
  28. options.c_cflag &= ~CSIZE;
  29. switch (databits)
  30. {
  31. case 7:
  32. options.c_cflag |= CS7;
  33. break;
  34. case 8:
  35. options.c_cflag |= CS8;
  36. break;
  37. default:
  38. ERROR ("Unsupported data size");
  39. return -1;
  40. }
  41. switch (parity)
  42. {
  43. case 'n':
  44. case 'N':
  45. options.c_cflag &= ~PARENB; /* Clear parity enable */
  46. options.c_iflag &= ~INPCK; /* Enable parity checking */
  47. break;
  48. case 'o':
  49. case 'O':
  50. options.c_cflag |= (PARODD | PARENB); /* 設置為奇效驗*/
  51. options.c_iflag |= INPCK; /* Disnable parity checking */
  52. break;
  53. case 'e':
  54. case 'E':
  55. options.c_cflag |= PARENB; /* Enable parity */
  56. options.c_cflag &= ~PARODD; /* 轉換為偶效驗*/
  57. options.c_iflag |= INPCK; /* Disnable parity checking */
  58. break;
  59. case 'S':
  60. case 's': /*as no parity*/
  61. options.c_cflag &= ~PARENB;
  62. options.c_cflag &= ~CSTOPB;
  63. break;
  64. default:
  65. ERROR ("Unsupported parity");
  66. return -1;
  67. }
  68. /* 設置停止位*/
  69. switch (stopbits)
  70. {
  71. case 1:
  72. options.c_cflag &= ~CSTOPB;
  73. break;
  74. case 2:
  75. options.c_cflag |= CSTOPB;
  76. break;
  77. default:
  78. ERROR ("Unsupported stop bits");
  79. return -1;
  80. }
  81. /* Set input parity option */
  82. //if (parity != 'n')
  83. // options.c_iflag |= INPCK;
  84. options.c_cc[VINTR] = 0;
  85. options.c_cc[VQUIT] = 0;
  86. options.c_cc[VERASE] = 0;
  87. options.c_cc[VKILL] = 0;
  88. options.c_cc[VEOF] = 0;
  89. options.c_cc[VTIME] = 1;
  90. options.c_cc[VMIN] = 0;
  91. options.c_cc[VSWTC] = 0;
  92. options.c_cc[VSTART] = 0;
  93. options.c_cc[VSTOP] = 0;
  94. options.c_cc[VSUSP] = 0;
  95. options.c_cc[VEOL] = 0;
  96. options.c_cc[VREPRINT] = 0;
  97. options.c_cc[VDISCARD] = 0;
  98. options.c_cc[VWERASE] = 0;
  99. options.c_cc[VLNEXT] = 0;
  100. options.c_cc[VEOL2] = 0;
  101. //options.c_cc[VTIME] = 150; // 15 seconds
  102. //options.c_cc[VMIN] = 0;
  103. tcflush (fd,TCIFLUSH); /* Update the options and do it NOW */
  104. if ((status = tcsetattr (fd,TCSANOW,&options)) != 0) {
  105. ERROR ("fail: status=%d, %s", status, strerror (errno));
  106. return -1;
  107. }
  108. return 0;
  109. }
Copyright © Linux教程網 All Rights Reserved