有沒有大佬有RFID的讀寫原始碼,在嵌入式Linux ARM cortex a8芯片系統的C語言原始碼,如果有基于這個的讀卡系統的原始碼就很更好了,救命啊!
uj5u.com熱心網友回復:
/*****************rfid.c*****************/
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/select.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#ifdef __cplusplus
#if __cplusplus
extern "C"{
#endif
#endif /* __cplusplus */
volatile unsigned int cardid ;
static struct timeval timeout;
/* 設定視窗引數:9600速率 */
void init_tty(int fd)
{
//宣告設定串口的結構體
struct termios termios_new;
//先清空該結構體
bzero( &termios_new, sizeof(termios_new));
// cfmakeraw()設定終端屬性,就是設定termios結構中的各個引數。
cfmakeraw(&termios_new);
//設定波特率
//termios_new.c_cflag=(B9600);
cfsetispeed(&termios_new, B9600);
cfsetospeed(&termios_new, B9600);
//CLOCAL和CREAD分別用于本地連接和接受使能,因此,首先要通過位掩碼的方式激活這兩個選項。
termios_new.c_cflag |= CLOCAL | CREAD;
//通過掩碼設定資料位為8位
termios_new.c_cflag &= ~CSIZE;
termios_new.c_cflag |= CS8;
//設定無奇偶校驗
termios_new.c_cflag &= ~PARENB;
//一位停止位
termios_new.c_cflag &= ~CSTOPB;
tcflush(fd,TCIFLUSH);
// 可設定接收字符和等待時間,無特殊要求可以將其設定為0
termios_new.c_cc[VTIME] = 10;
termios_new.c_cc[VMIN] = 1;
// 用于清空輸入/輸出緩沖區
tcflush (fd, TCIFLUSH);
//完成配置后,可以使用以下函式激活串口設定
if(tcsetattr(fd,TCSANOW,&termios_new) )
printf("Setting the serial1 failed!\n");
}
/*計算校驗和*/
unsigned char CalBCC(unsigned char *buf, int n)
{
int i;
unsigned char bcc=0;
for(i = 0; i < n; i++)
{
bcc ^= *(buf+i);
}
return (~bcc);
}
/*請求天線范圍內的卡*/
int PiccRequest(int fd)
{
unsigned char WBuf[128], RBuf[128];
int ret;
fd_set rdfd;
memset(WBuf, 0, 128);
memset(RBuf,1,128);
WBuf[0] = 0x07; //幀長= 7 Byte
WBuf[1] = 0x02; //包號= 0 , 命令型別= 0x01
WBuf[2] = 0x41; //命令= 'C'
WBuf[3] = 0x01; //資訊長度= 0
WBuf[4] = 0x52; //請求模式: ALL=0x52
WBuf[5] = CalBCC(WBuf, WBuf[0]-2); //校驗和
WBuf[6] = 0x03; //結束標志
FD_ZERO(&rdfd);
FD_SET(fd,&rdfd);
write(fd, WBuf, 7);;
ret = select(fd + 1,&rdfd, NULL,NULL,&timeout);
switch(ret)
{
case -1:
perror("select error\n");
break;
case 0:
printf("Request timed out.\n");
break;
default:
ret = read(fd, RBuf, 8);
if (ret < 0)
{
// printf("ret = %d, %m\n", ret, errno);q
break;
}
if (RBuf[2] == 0x00) //應答幀狀態部分為0 則請求成功
{
return 0;
}
break;
}
return -1;
}
/*防碰撞,獲取范圍內最大ID*/
int PiccAnticoll(int fd)
{
unsigned char WBuf[128], RBuf[128];
int ret;
fd_set rdfd;;
memset(WBuf, 0, 128);
memset(RBuf,0,128);
WBuf[0] = 0x08; //幀長= 8 Byte
WBuf[1] = 0x02; //包號= 0 , 命令型別= 0x01
WBuf[2] = 0x42; //命令= 'B'
WBuf[3] = 0x02; //資訊長度= 2
WBuf[4] = 0x93; //防碰撞0x93 --一級防碰撞
WBuf[5] = 0x00; //位計數0
WBuf[6] = CalBCC(WBuf, WBuf[0]-2); //校驗和
WBuf[7] = 0x03; //結束標志
FD_ZERO(&rdfd);
FD_SET(fd,&rdfd);
write(fd, WBuf, 8);
ret = select(fd + 1,&rdfd, NULL,NULL,&timeout);
switch(ret)
{
case -1:
perror("select error\n");
break;
case 0:
perror("Timeout:");
break;
default:
ret = read(fd, RBuf, 10);
if (ret < 0)
{
// printf("ret = %d, %m\n", ret, errno);
break;
}
if (RBuf[2] == 0x00) //應答幀狀態部分為0 則獲取ID 成功
{
cardid = (RBuf[4]<<24) | (RBuf[5]<<16) | (RBuf[6]<<8) | RBuf[7];
return 0;
}
}
return -1;
}
/*讀取卡ID*/
int readcard(unsigned int *arg )
{
/* int ret, i; */
int fd;
fd = open("/dev/s3c2410_serial1", O_RDWR | O_NOCTTY | O_NONBLOCK);
// fd = open("/dev/ttySAC1", O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd < 0)
{
fprintf(stderr, "open /dev/s3c2410_serial1 fail!\n");
return -1;
}
/*初始化串口*/
init_tty(fd);
timeout.tv_sec = 0;
timeout.tv_usec = 200000;
/*請求天線范圍的卡*/
if ( PiccRequest(fd) )
{
printf("The request failed!\n");
close(fd);
return -1;
}
/*進行防碰撞,獲取天線范圍內最大的ID*/
if( PiccAnticoll(fd) )
{
printf("Couldn't get card-id!\n");
close(fd);
return -1;
}
printf("card ID = %x\n", cardid);
*arg = cardid;
close(fd);
return 1;
}
int main()
{
unsigned int carID;
while(1)
{
while(1 != readcard(&carID));
printf("carID:%d\n",carID);
sleep(1);
}
return 0;
}
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */
/****************rfid.h****************/
#ifdef __cplusplus
#if __cplusplus
extern "C"{
#endif
#endif /* __cplusplus */
extern volatile unsigned int cardid;
extern int readcard(unsigned int *);
extern int init_tty(int);
extern unsigned char CalBCC(char *, int);
extern int PiccRequest(int);
extern int PiccAnticoll(int);
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/117512.html
標籤:應用程序開發區
上一篇:xv6編譯時報錯
下一篇:Qt圖形界面控制LED
