linux RFID读卡器的驱动问题

本人所使用的RFID读卡器的型号为:MC9500,所用的卡为标准的Mifare One S50,以usb口的方式接入电脑,本人所使用的系统为fedora12.

RFID读卡器内部有一块cp2102的usb转串口的芯片,所以使用的驱动系统的usbserial驱动,在插入设备之前通过

insmod usbserial vendor=.... product=.....  ;

insmod cp210x;

命令来加载设备的驱动,cp210x的驱动为系统自带的为cp210x系列usb转串口芯片的驱动程序,在linux2.6.25以上的内核中都自带了,若使用小于2.6.25的内核,可能要手动配备cp210x的驱动,官网上貌似有说明的。vendor ,product这两个参数可以到读卡器配备的window驱动文件中找到,这两个参数是必须的。加载这两个命令后插入RFID读卡器,通过 ls /dev 的命令可以看到多了一个ttyUSB0的设备文件,通过这个设备文件,就可以操作读卡器了。

之后的编程就基本为串口编程了,需要对读卡器进行什么样的操作,就写入什么样的命令即可。可以打电话到厂家索要串口通讯的协议,我的也是打电话后提供的,工作人员的态度还不错。串口编程的时候要注意:发送的数据都是16进制的,读取返回值也要以16进制来进行,而且对字符串操作不能使用string.h下的那几个字符串操作函数,因为写入和读取的数据中间可能有0,而字符串的操作函数都是以'/0'结束的,我就上了这个当。写入数据的时候最后一个字节为效验字,是前面所有字节的异或,这个需要注意一下,否则写入后返回值将全部是0。相关的串口协议和卡的物理结构的说明,在厂家给的文档中会有说明,就不做介绍了。

 

代码如下,各个公司的RFID读卡器的串口协议不同,但大致的流程是一样,都是串口数据的读写,比较简单:

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <sys/time.h>
#include <pthread.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <locale.h>
#include <wchar.h>

#include"fc.h"  //fc.h头文件只是将下面的某几个函数申明了一下,用不用大致都行的。

static unsigned char Device[]={0x9b,0x0e,0x00,0x1a,0x40,0x52,0x46,0x20,0x43,0x61,0x72,0x64,0x52,0x65,0x61,
0x64,0x65,0x72,0x20,0x56,0x65,0x72,0x73,0x69,0x6f,0x6e,0x20,0x31,0x2e,0x30,0x9f,0x00};
static unsigned char Card[]={0x9B,0x05,0x00,0x09,0x30,0x00,0x08};
static unsigned char Auth[]={0x9b,0x0c,0x00,0x02,0x31,0x00,0xa4};
 int fd;

pthread_mutex_t psemread= PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t psemstrcpy= PTHREAD_MUTEX_INITIALIZER;

char lpOutBuffer[30];
unsigned char lpReadbuf[50];

int set_opt()
{
  struct termios newtio,oldtio;
  if ( tcgetattr( fd,&oldtio) != 0) {
    perror("SetupSerial 1");
    return -1;
  }
  bzero( &newtio, sizeof( newtio ) );
  newtio.c_cflag |= CLOCAL | CREAD;
  newtio.c_cflag &= ~CSIZE;

  newtio.c_cflag |= CS8;//data bit

  newtio.c_cflag &= ~PARENB;//ji ou xiao yan bit

  cfsetispeed(&newtio, B9600);//bo te rate
  cfsetospeed(&newtio, B9600);

  newtio.c_cflag &= ~CSTOPB; //stop bit

  newtio.c_cc[VTIME] = 0;
  newtio.c_cc[VMIN] = 0;
  tcflush(fd,TCIFLUSH);
  if((tcsetattr(fd,TCSANOW,&newtio))!=0)
  {
    perror("com set error");
    return -1;
  }
  printf("set done!/n");
  return 0;
}

int open_port()
{
  long vdisable;
 printf("Open Serial port ttyUSB6/n");
  fd = open( "/dev/ttyUSB6", O_RDWR|O_NOCTTY|O_NDELAY);
  if (-1 == fd)
 {
      printf("Can't Open Serial Port ttyUSB6/n");
   printf("Open Serial port ttyUSB0/n");
   fd = open("/dev/ttyUSB0", O_RDWR|O_NOCTTY|O_NDELAY);
   if(-1 == fd)
  {
   perror("Can't Open Serial Port ttyUSB0");
  }
    //  return(-1);
    }


  if(fcntl(fd, F_SETFL, 0)<0)
    printf("fcntl failed!/n");
  else
    printf("fcntl=%d/n",fcntl(fd, F_SETFL,0));
  if(isatty(STDIN_FILENO)==0)
    printf("standard input is not a terminal device/n");
  else
    printf("isatty success!/n");

  printf("fd-open=%d/n",fd);
  return fd;
}
int CheckDevice()
{
 printf("Check Device/n");
 memset(lpOutBuffer,0,30);
 memset(lpReadbuf,0,50);
 lpOutBuffer[0]=0x9b;
 lpOutBuffer[1]=0x0E;
 lpOutBuffer[2]=0x00;
 lpOutBuffer[3]=0x01;
 lpOutBuffer[4]=0x40;
 lpOutBuffer[5]=0xd4;
 
 pthread_mutex_lock(& psemread);

 if(write(fd,lpOutBuffer,sizeof(lpOutBuffer))!=sizeof(lpOutBuffer))
 {
  printf("Write ERROR/n");
  pthread_mutex_unlock(& psemread);
  return -1;
 } 
  sleep(1);
 if(-1==read(fd,lpReadbuf,50))
 {
  printf("READ ERROR/n");
  pthread_mutex_unlock(& psemread);
  return -1;
 }
 pthread_mutex_unlock(& psemread);

 int i=0; 
 for(i=0;i<sizeof(Device);i++)
 {
  if(Device[i] != lpReadbuf[i])
   return -1;
 }
 printf("Find Device OK/n");
 //displayMsg();
 return 0;;
}
void displayMsg()
{
 
 int i=-1;
 while(i++<50)
 {
  printf("%x ",lpReadbuf[i]);
 }
 printf("/n");
}
int ring()
{
 printf("ring/n");
 memset(lpOutBuffer,0,30);
 memset(lpReadbuf,0,50);

 lpOutBuffer[0]=0x9b;
 lpOutBuffer[1]=0x05;
 lpOutBuffer[2]=0x00;
 lpOutBuffer[3]=0x01;
 lpOutBuffer[4]=0x41;
 lpOutBuffer[5]=0xde;
 

 if(write(fd,lpOutBuffer,sizeof(lpOutBuffer))!=sizeof(lpOutBuffer))
 {
  printf("Write ERROR/n");
  return ;
 }
}
int GetCard()
{
 printf("Check Card:/n");
 memset(lpOutBuffer,0,30);
 memset(lpReadbuf,0,50);
 lpOutBuffer[0]=0x9b;
 lpOutBuffer[1]=0x05;
 lpOutBuffer[2]=0x00;
 lpOutBuffer[3]=0x02;
 lpOutBuffer[4]=0x30;
 lpOutBuffer[5]=0x52;
 lpOutBuffer[6]=0xfe;
 
 pthread_mutex_lock(& psemread);

 if(write(fd,lpOutBuffer,sizeof(lpOutBuffer))!=sizeof(lpOutBuffer))
 {
  printf("/nWrite ERROR/n");
  pthread_mutex_unlock(& psemread);
  return -1;
 } 
  sleep(1);
 if(-1==read(fd,lpReadbuf,50))
 {
  printf("/nREAD ERROR/n");
  pthread_mutex_unlock(& psemread);
  return -1;
 }
 pthread_mutex_unlock(& psemread);
// displayMsg();
 int i=0;
 for(i=0;i<sizeof(Card);i++)
 {
  if(Card[i] != lpReadbuf[i])
  {
   printf("/nno card or card type is not allowed!/n");
   return -1;
  }
 }
// printf("/n");
 printf("card ATQ=0x%x 0x%x/n",lpReadbuf[i],lpReadbuf[i+1]);
 printf("card ID=");
 i=i+2;
 while(lpReadbuf[i])
  printf("0x%x ",lpReadbuf[i++]);
 printf("/nCheck Card ok/n");
 return 0;
}
void light()
{
 printf("lighting/n");
 memset(lpOutBuffer,0,30);
 memset(lpReadbuf,0,50);

 lpOutBuffer[0]=0x9b;
 lpOutBuffer[1]=0x05;
 lpOutBuffer[2]=0x02;
 lpOutBuffer[3]=0x01;
 lpOutBuffer[4]=0x42;
 lpOutBuffer[5]=0xde;
 

 if(write(fd,lpOutBuffer,sizeof(lpOutBuffer))!=sizeof(lpOutBuffer))
 {
  printf("Write ERROR/n");
  return ;
 } 
}
//9b 0c 00 13 33 00 01 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 b6
int Writecard(char * add1,char * add2)
{
 printf("Write Card/n");
 memset(lpOutBuffer,0,30);
 memset(lpReadbuf,0,50);
 lpOutBuffer[0]=0x9b;
 lpOutBuffer[1]=0x0c;
 lpOutBuffer[2]=0x00;
 lpOutBuffer[3]=0x13;
 lpOutBuffer[4]=0x33;
 lpOutBuffer[5]=0x00;
 lpOutBuffer[6]=0x02;

// strcat(lpOutBuffer,add1);

 
/* printf("%d/n",strlen(add1));


 for(i=16-strlen(add1);i>0;i--)
  lpOutBuffer[7+strlen(add1)+i]=0x00;
 lpOutBuffer[23]=0xb6;

 for(i=0;i<30;i++)
  printf("0x%x  ",lpOutBuffer[i]);
 printf("/n/n");
*/

 lpOutBuffer[7]=0x55;
 lpOutBuffer[8]=0x55;
 lpOutBuffer[9]=0x55;
 lpOutBuffer[10]=0x55;
 lpOutBuffer[11]=0x55;
 lpOutBuffer[12]=0x55;
 lpOutBuffer[13]=0x55;
 lpOutBuffer[14]=0x55;
 lpOutBuffer[15]=0x55;
 lpOutBuffer[16]=0x55;
 lpOutBuffer[17]=0x55;
 lpOutBuffer[18]=0x55;
 lpOutBuffer[19]=0x55;
 lpOutBuffer[20]=0x55;
 lpOutBuffer[21]=0x55;
 lpOutBuffer[22]=0x55;

 int i=0;
 
 for(i=0;i<strlen(add1);i++)
  printf("0x%x  ",add1[i]&255);
 printf("/n/n");

// for(i=0;i<strlen(add1);i++)
//  lpOutBuffer[8+i]=add1[i];

// lpOutBuffer[14]=0xffffff22;

 lpOutBuffer[23]=0xb6;
 
 
 for(i=0;i<30;i++)
  printf("0x%x  ",lpOutBuffer[i]);
 printf("/n/n");

 pthread_mutex_lock(& psemread);

 if(write(fd,lpOutBuffer,sizeof(lpOutBuffer))!=sizeof(lpOutBuffer))
 {
  printf("Write ERROR/n");
  pthread_mutex_unlock(& psemread);
  return -1;
 } 

  sleep(1);

 if(-1==read(fd,lpReadbuf,50))
 {
  printf("READ ERROR/n");
  pthread_mutex_unlock(& psemread);
  return -1;
 }
 pthread_mutex_unlock(& psemread);
 displayMsg();
 return 0;
}
int Readcard(int sector,int block)
{
//9b 0d 00 03 32 00 00 a7
 printf("Read Card/n");
 memset(lpOutBuffer,0,30);
 memset(lpReadbuf,0,50);
 lpOutBuffer[0]=0x9b;
 lpOutBuffer[1]=0x0d;
 lpOutBuffer[2]=0x00;
 lpOutBuffer[3]=0x03;
 lpOutBuffer[4]=0x32;
 lpOutBuffer[5]=0x00;
 lpOutBuffer[6]=0x01;
 lpOutBuffer[7]=0xa7;
 
 pthread_mutex_lock(& psemread);

 if(write(fd,lpOutBuffer,sizeof(lpOutBuffer))!=sizeof(lpOutBuffer))
 {
  printf("Write ERROR/n");
  pthread_mutex_unlock(& psemread);
  return -1;
 } 
  sleep(1);
 if(-1==read(fd,lpReadbuf,50))
 {
  printf("READ ERROR/n");
  pthread_mutex_unlock(& psemread);
  return -1;
 }
 pthread_mutex_unlock(& psemread);
 displayMsg();
 int i=0;
 if(lpReadbuf[i]!=0x9b)
 {
  printf("Read block error/n");
  return -1;
 }
 i=17;
 char nex[]={0x34,0x00,0x11/*,0xff,0xff,0xf8*/};
 while(i<20)
 { 
  //printf("%x %x/t",lpReadbuf[i],nex[i-17]);
  if(nex[i-17]!=lpReadbuf[i])
  {
   printf("Read block error/n");
   return -1;
  }
  
  i++;
 }
 printf("Read success/n");
 return 0;
}
int Authcard()
{
//9b 0C 00 09 31 60 00 ff ff ff ff ff ff cf
 printf("AUTH Card/n");
 memset(lpOutBuffer,0,30);
 memset(lpReadbuf,0,50);
 lpOutBuffer[0]=0x9b;
 lpOutBuffer[1]=0x0c;
 lpOutBuffer[2]=0x00;
 lpOutBuffer[3]=0x09;
 lpOutBuffer[4]=0x31;
 lpOutBuffer[5]=0x60;
 lpOutBuffer[6]=0x00;
 lpOutBuffer[7]=0xff;
 lpOutBuffer[8]=0xff;
 lpOutBuffer[9]=0xff;
 lpOutBuffer[10]=0xff;
 lpOutBuffer[11]=0xff;
 lpOutBuffer[12]=0xff;
 lpOutBuffer[13]=0xcf;
 
 pthread_mutex_lock(& psemread);

 if(write(fd,lpOutBuffer,sizeof(lpOutBuffer))!=sizeof(lpOutBuffer))
 {
  printf("Write ERROR/n");
  pthread_mutex_unlock(& psemread);
  return -1;
 } 
  sleep(1);
 if(-1==read(fd,lpReadbuf,50))
 {
  printf("READ ERROR/n");
  pthread_mutex_unlock(& psemread);
  return -1;
 }
 pthread_mutex_unlock(& psemread);
 //displayMsg();
 int i=0;
 for(i=0;i<sizeof(Auth);i++)
 {
  if(Auth[i] != lpReadbuf[i])
  {
   printf("Auth ERROR/n");
   return -1;  
  }
 }
 printf("Auth Success/n");
 return 0;
}
void close_port()
{
 close(fd);
}

 

你可能感兴趣的:(编程,linux,struct,input,Terminal,电话)