手机版
你好,游客 登录 注册
背景:
阅读新闻

Linux下RFID卡(门禁卡,Mifare卡)的编程

[日期:2012-04-18] 来源:Linux社区  作者:fgwf1 [字体:大 中 小]
3,在一定超时时间内读取数据
  1. static int cmd_read_timeout (int fd, unsigned char* buf, int msec) {  
  2.     if (fd < 0 || buf == NULL) {  
  3.         ERROR ("fd = %d, buf=%p", fd, buf);  
  4.         return -1;  
  5.     }  
  6.   
  7.     int maxfd = fd;  
  8.     fd_set fdread;  
  9.   
  10.     FD_ZERO (&fdread);  
  11.     FD_SET (fd, &fdread);  
  12.   
  13.     /* set a suitable timeout to play around with */  
  14.     struct timeval timeout;  
  15.     timeout.tv_sec = 0;  
  16.     timeout.tv_usec = msec*1000;  
  17.   
  18.     int nread = 0;  
  19.     int rc = select (maxfd+1, &fdread, NULL, NULL, &timeout);  
  20.     switch (rc) {  
  21.     case -1:  
  22.         ERROR ("error: %s", strerror (errno));  
  23.         /* select error */  
  24.         nread = -1;  
  25.         break;  
  26.     case 0:  
  27.         //DEBUG ("timeout!");   
  28.         nread = -1;  
  29.         break;  
  30.     default:  
  31.     {  
  32.         /* readable sockets */  
  33.         if (FD_ISSET (fd, &fdread)) {  
  34.             nread = read (fd, buf, 512);  
  35.             //DEBUG ("read(%d):%s\n", nread, buf);   
  36.             if ((buf[1] + 2) == nread) {  
  37.                 // checksum   
  38.                 unsigned char checksum = 0;  
  39.                 for (int i = 0; i < nread; i++) {  
  40.                     checksum = checksum^buf[i];  
  41.                 }  
  42.                 if (checksum == 0) {  
  43.                     // get right command.   
  44.                 } else {  
  45.                     nread = 0;  
  46.                 }  
  47.             } else {  
  48.                 // ERROR cmd lenght;   
  49.                 DEBUG ("cmd length not right! got data len=%d, require=%d", nread, buf[1]+2);  
  50.                 nread = 0;  
  51.             }  
  52.         } else {  
  53.             ERROR ("fd not set! maybe error!");  
  54.             nread = -1;  
  55.         }  
  56.         break;  
  57.     }  
  58.     }  
  59.     return nread;  
  60. }  
4,读取唯一的卡号和sector0上block1的数据。
  1. //============================================   
  2. //  Command List, preamble + length + command   
  3. //============================================   
  4. static const unsigned char SelectCard[]=      {0xBA,0x02,0x01 };  
  5. static const unsigned char LoginSector0[]=    {0xBA,0x0A,0x02,0x00,0xAA,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};  
  6. static const unsigned char LoginSector1[]=    {0xBA,0x0A,0x02,0x01,0xAA,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};  
  7. static const unsigned char ReadBlock1[]=      {0xBA,0x03,0x03,0x01};  
  8. static const unsigned char WriteBlock1[]=     {0xBA,0x13,0x04,0x01,0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF};  
  9. static const unsigned char ReadValue[]=       {0xBA,0x03,0x05,0x05};  
  10. static const unsigned char InitializeValue[]=   {0xBA,0x07,0x06,0x04,0x00,0x00,0x00,0x01};  
  11. static const unsigned char IncrementValue[]=  {0xBA,0x07,0x08,0x04,0x00,0x00,0x00,0x20};  
  12. static const unsigned char DecrementValue[]=  {0xBA,0x07,0x09,0x04,0x03,0x00,0x00,0x00};  
  13. static const unsigned char CopyValue[]=       {0xBA,0x04,0x0A,0x04,0x05};  
  14. static const unsigned char ReadULPage5[]=     {0xBA,0x03,0x10,0x05};  
  15. static const unsigned char WriteULPage5[]=    {0xBA,0x07,0x11,0x05,0x11,0x22,0x33,0x44};  
  16. static const unsigned char TurnOnRedLed[]=    {0xBA,0x03,0x40,0x01};  
  17. static const unsigned char TurnOffRedLed[]=   {0xBA,0x03,0x40,0x00};  
  18.   
  19. static int wrap_data (unsigned char* dest, const unsigned char* src, int len) {  
  20.     if (dest == NULL || src == NULL || len == 0) return -1;  
  21.     memcpy (dest, src, len);  
  22.     unsigned char checksum = 0;  
  23.     for (int i = 0; i < len; i++) {  
  24.         checksum = checksum ^ src[i];  
  25.     }  
  26.     dest[len] = checksum;  
  27.     return 0;  
  28. }  
  29.   
  30. int clear_comm (int fd) {  
  31.     tcflush(fd,TCIFLUSH);  
  32.     return 0;  
  33. }  
  34.   
  35. int read_card_id_name (int fd, unsigned char* id, unsigned char* name) {  
  36.     if (id == NULL || name == NULL) return -1;  
  37.     unsigned char data_send[512] = {0};  
  38.     unsigned char data_recv[512] = {0};  
  39.     int nWrite = 0;  
  40.     int nRead = 0;  
  41.   
  42.     tcflush(fd,TCIFLUSH);  
  43.   
  44.     wrap_data (data_send, SelectCard, sizeof (SelectCard));  
  45.     nWrite = write (fd, data_send, sizeof (SelectCard) + 1);  
  46.     if ((nRead = cmd_read_timeout (fd, data_recv, 300)) <= 0) {  
  47.         return -1;  
  48.     }  
  49.     //DEBUG ("(%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", nRead, data_recv[0], data_recv[1], data_recv[2], data_recv[3], data_recv[4], data_recv[5], data_recv[6], data_recv[7], data_recv[8], data_recv[9]);   
  50.     if ((data_recv[2] != 0x01) || (data_recv[3] != 0)) {  
  51.         return -1;  
  52.     }  
  53.   
  54.     int retcode = 0;  
  55.     unsigned char card_type = data_recv[data_recv[1]];  
  56.     switch (card_type) {  
  57.     case 1:  
  58.     case 4:  
  59.         memcpy (id, &data_recv[4], 4);  
  60. #if 1   
  61.         // verify password of sector0   
  62.         wrap_data (data_send, LoginSector0, sizeof (LoginSector0));  
  63.         nWrite = write (fd, data_send, sizeof (LoginSector0) + 1);  
  64.         if ((nRead = cmd_read_timeout (fd, data_recv, 300)) <= 0) {  
  65.             return -1;  
  66.         }  
  67.         if ((data_recv[2] != 0x02) || (data_recv[3] != 0x02)) {  
  68.             return -1;  
  69.         }  
  70.   
  71.         // read data from block1   
  72.         wrap_data (data_send, ReadBlock1, sizeof (ReadBlock1));  
  73.         nWrite = write (fd, data_send, sizeof (ReadBlock1) + 1);  
  74.         if ((nRead = cmd_read_timeout (fd, data_recv, 300)) <= 0) {  
  75.             return -1;  
  76.         }  
  77.         if ((data_recv[2] != 0x03) || (data_recv[3] != 0)) {  
  78.             return -1;  
  79.         }  
  80.         memcpy (name, &data_recv[4], 16);  
  81. #endif   
  82.         retcode = 0;  
  83.         break;  
  84.     case 3:  
  85.         memcpy (id, &data_recv[4], 7);  
  86.         retcode = -1;  
  87.         break;  
  88.     case 2:  
  89.     case 5:  
  90.     case 6:  
  91.     default:  
  92.         retcode = -1;  
  93.         break;  
  94.     }  
  95.     if (retcode != 0) {  
  96.         return retcode;  
  97.     }  
  98.     // Glare Red Led to indicate working ok   
  99.     wrap_data (data_send, TurnOnRedLed, sizeof (TurnOnRedLed));  
  100.     nWrite = write (fd, data_send, sizeof (TurnOnRedLed) + 1);  
  101.     nRead = cmd_read_timeout (fd, data_recv, 300);  
  102.     usleep (200*1000);  
  103.     wrap_data (data_send, TurnOffRedLed, sizeof (TurnOffRedLed));  
  104.     nWrite = write (fd, data_send, sizeof (TurnOffRedLed) + 1);  
  105.     nRead = cmd_read_timeout (fd, data_recv, 300);  
  106.   
  107.     return 0;  
  108. }  

5,闪烁红灯,提示操作成功。

  1. int GlareLed (int fd) {  
  2.     int retcode = 0;  
  3.     unsigned char data_send[512] = {0};  
  4.     unsigned char data_recv[512] = {0};  
  5.     int nWrite = 0;  
  6.     int nRead = 0;  
  7.   
  8.     // Glare Red Led to indicate working ok   
  9.     wrap_data (data_send, TurnOnRedLed, sizeof (TurnOnRedLed));  
  10.     nWrite = write (fd, data_send, sizeof (TurnOnRedLed) + 1);  
  11.     nRead = cmd_read_timeout (fd, data_recv, 300);  
  12.     DEBUG ("get %d bytes", nRead);  
  13.     usleep (200*1000);  
  14.     wrap_data (data_send, TurnOffRedLed, sizeof (TurnOffRedLed));  
  15.     nWrite = write (fd, data_send, sizeof (TurnOffRedLed) + 1);  
  16.     nRead = cmd_read_timeout (fd, data_recv, 300);  
  17.     DEBUG ("get %d bytes", nRead);  
  18.     return 0;  
  19. }  

6,主函数

  1. int main(int argc, char **argv)  
  2. {  
  3.     const char *dev ="/dev/ttyUSB0";  
  4.     if (argc > 1) dev = argv[1];  
  5.   
  6.     int fd = open (dev, O_RDWR|O_NOCTTY|O_NDELAY);         //| O_NOCTTY | O_NDELAY   
  7.     if (fd < 0) {  
  8.         ERROR ("Can't Open Serial Port");  
  9.         return -1;  
  10.     }  
  11.     if (set_options (fd, B115200, 8, 1, 'N') != 0) {  
  12.         ERROR ("Set Parity Error");  
  13.         close (fd);  
  14.         return -1;  
  15.     }  
  16.   
  17.     while (1) {  
  18.         unsigned char id[10];  
  19.         unsigned char name[20];  
  20.         int ret = read_card_id_name (fd, id, name);  
  21.         if (ret == 0) {  
  22.             printf ("%02x%02x%02x%02x\n ", id[0], id[1], id[2], id[3]);  
  23.         }  
  24.         //GlareLed (fd);   
  25.         //DEBUG ("");   
  26.         //string content = getdata (fd);   
  27.         //printf ("%s", content.c_str ());   
  28.         //write(fd, "AT1", 3);   
  29.         usleep (300*1000);  
  30.     }  
  31.     close(fd);  
  32.     return 0;  
  33. }  
7,遇到问题
在开发过程中,发现在嵌入式环境中,接收的数据经常会莫名丢失,0x13,0x11经常会不见了,丢失的都是这两个数,后来加上如下这段就好了。
  1. options.c_cc[VINTR] = 0;  
  2. options.c_cc[VQUIT] = 0;  
  3. options.c_cc[VERASE] = 0;  
  4. options.c_cc[VKILL] = 0;  
  5. options.c_cc[VEOF] = 0;  
  6. options.c_cc[VTIME] = 1;  
  7. options.c_cc[VMIN] = 0;  
  8. options.c_cc[VSWTC] = 0;  
  9. options.c_cc[VSTART] = 0;  
  10. options.c_cc[VSTOP] = 0;  
  11. options.c_cc[VSUSP] = 0;  
  12. options.c_cc[VEOL] = 0;  
  13. options.c_cc[VREPRINT] = 0;  
  14. options.c_cc[VDISCARD] = 0;  
  15. options.c_cc[VWERASE] = 0;  
  16. options.c_cc[VLNEXT] = 0;  
  17. options.c_cc[VEOL2] = 0;  
linux
相关资讯       Linux编程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款