t_fm11rf005.c

来自「RFID手持设备的读写代码,具有读RFID卡和进行数据管理的代码,同时还包含串口」· C语言 代码 · 共 289 行

C
289
字号
/*FM11RF005 Test program
  Wang Yue 2005-8-26
 */

#include <stdio.h>
#include <stdlib.h>
#include <api.h>
#include <ExEH0218.h>
#include <contactless.h>
#include "common.h" 

unsigned char FM_PASS[6] = "\xFF\xFF\xFF\xFF\x00\x00";

static short FM11RF005_read(void);
static short FM11RF005_write(void);


short FM11RF005_test_mini(void)
{
	BROWINFO  	INFO_sub;		
	int	  	retint;	
	unsigned char	menu_info[50];
		
	if(Get_Language()==LANG_CHN)
	{
		strcpy(menu_info,"1.读测试        2.写测试        ");		
	}
	else
	{
		strcpy(menu_info,"1.Read Test     2.Write Test    ");		
	}	     
        				
	INFO_sub.lPtr = 0;
	INFO_sub.cPtr = 0;
	INFO_sub.startLine = 0;
	while(1)
	{
		clr_scr();
		INFO_sub.startLine = 0;
		INFO_sub.iStr = menu_info;
		INFO_sub.mInt = 2;
		INFO_sub.lineMax = 16;
		INFO_sub.sFont = 0;
		INFO_sub.numEnable = 0;		
		INFO_sub.qEvent = EXIT_KEY_F1;		
		INFO_sub.autoexit = 0;			
		INFO_sub.dispLines = 2;	
				
		retint = brow_select(&INFO_sub);	
		switch( retint )
		{
			case 0:
				FM11RF005_read();
				break;
			case 1:
				FM11RF005_write();			
				break;
			default:				
				return -1;					
				break;									
		}
	}
	
	return 0;		
}


static short FM11RF005_read() 
{
	char 		ret;
	unsigned char 	ATQ[3];
	unsigned char 	sno[4];
	unsigned char	sak;
	unsigned char 	databuf[80];
	unsigned char 	tmpbuf[30];		
	unsigned char	trycnt;
	unsigned char	read_ok = 0;
	unsigned char	bno=0;		
	unsigned char 	hintstr1[20];
	unsigned char 	hintstr2[20];
	unsigned char 	hintstr3[20];
	unsigned char 	hintstr4[20];
	//unsigned char	hintstr5[20];
	//unsigned char	hintstr6[20];
	unsigned char 	errstr[30];
		
	if(Get_Language()==LANG_CHN)
	{
		strcpy(errstr,"接口打开错误");
		strcpy(hintstr1,"请在%d秒内刷卡");
		strcpy(hintstr2,"未读到合法卡!");
		strcpy(hintstr3,"读错误");
		strcpy(hintstr4,"第%d块内容为:");
		//strcpy(hintstr5,"读序列号错误");
		//strcpy(hintstr6,"认证错误");
	}	
	else
	{
		strcpy(errstr,"Interface Error!");
		strcpy(hintstr1,"put card:%d sec");
		strcpy(hintstr2,"no legal card");
		strcpy(hintstr3,"read error");
		strcpy(hintstr4,"Block %d:");
		//strcpy(hintstr5,"Read SN Error!");
		//strcpy(hintstr6,"Authen Error!");
	}
	
	if(RCX_Init(CARD_TYPE_14443A))
	{
		DispStr_CE(0,3,errstr,DISP_POSITION|DISP_CLRSCR);
		delay_and_wait_key(0,EXIT_KEY_ALL,0);
		return -1;
	}  
	
	if( input_block_num(0,15,&bno) != 0 )
	{
		RCX_Close();
		return 2;
	}	
			
	SPT_set(1);
	for ( trycnt=11; trycnt>0; )	
	{
		if(!SPT_read())
		{			
			sprintf(tmpbuf,hintstr1,--trycnt);
			DispStr_CE(0,0,tmpbuf,DISP_POSITION|DISP_CLRLINE);
			SPT_set(64);
  		}
  		
		ret = FM11RF_Request(PICC_REQALL,ATQ);
		if( (ret==RCX_OK)&&(ATQ[0]==0x05) )
		{
			FM11RF_Select1(sno,&sak);
			memset(sno,0x00,sizeof(sno));
			ret = FM11RF_Read4Bytes(1,sno);
		}				
		if( ret==RCX_OK )
		{
			ret = FM11RF_AuthKey(PICC_AUTHENT1A,sno,FM_PASS,bno);
		}
		if(ret==RCX_OK)
		{
			read_ok = 1;
			break;
		}
		RCX_RF_Reset(10);			
	}
	
	if ( !read_ok )
	{		
		RCX_Close();
		DispStr_CE(0,3,hintstr2,DISP_POSITION|DISP_CLRSCR);		
		warning_beep(1);		
		delay_and_wait_key( 0, EXIT_KEY_ALL, 0 );
		return 1;
	}
	
	clr_scr();			
	memset(databuf,0x00,sizeof(databuf));
	ret = FM11RF_Read4Bytes(bno,databuf);	
	if(ret != RCX_OK)
	{
		DispStr_CE(0,3,hintstr3,DISP_POSITION|DISP_CLRLINE);		
		RCX_Close();
		delay_and_wait_key( 0, EXIT_KEY_ALL, 0 );
		return 1;
	}
	
	RCX_Close();
	sprintf(tmpbuf,hintstr4,bno);
	DispStr_CE(0,2,tmpbuf,DISP_POSITION|DISP_CLRLINE);
	sprintf(tmpbuf,"%02X %02X %02X %02X",databuf[0],databuf[1],databuf[2],databuf[3]);
	DispStr_CE(0,4,tmpbuf,DISP_CENTER|DISP_CLRLINE);
	warning_beep(0);		
	delay_and_wait_key( 0, EXIT_KEY_ALL, 0 );
	return 0;	
}


static short FM11RF005_write()
{
	unsigned char 	ATQ[3];
	unsigned char 	sno[4];
	unsigned char	sak;
	unsigned char 	databuf[80];
	unsigned char 	tmpbuf[30];
	char 		ret;
		
	unsigned char		trycnt;
	unsigned char		read_ok = 0;
	unsigned char		bno=9;		
	unsigned char 		hintstr1[20];
	unsigned char 		hintstr2[20];
	unsigned char 		hintstr3[20];
	unsigned char 		hintstr4[20];
	//unsigned char		hintstr5[20];
	//unsigned char		hintstr6[20];
	unsigned char 		errstr[30];	
	
	if(Get_Language()==LANG_CHN)
	{
		strcpy(errstr,"接口打开错误");
		strcpy(hintstr1,"请在%d秒内刷卡");
		strcpy(hintstr2,"未读到合法卡!");
		strcpy(hintstr3,"写错误");
		strcpy(hintstr4,"写成功");
		//strcpy(hintstr5,"读序列号错误");
		//strcpy(hintstr6,"认证错误");
	}	
	else
	{
		strcpy(errstr,"Interface Error!");
		strcpy(hintstr1,"put card:%d sec");
		strcpy(hintstr2,"no legal card");
		strcpy(hintstr3,"write error");
		strcpy(hintstr4,"write success");
		//strcpy(hintstr5,"Read SN Error!");
		//strcpy(hintstr6,"Authen Error!");	
	}
			
	if(RCX_Init(CARD_TYPE_14443A))
	{
		DispStr_CE(0,3,errstr,DISP_POSITION|DISP_CLRSCR);
		delay_and_wait_key(0,EXIT_KEY_ALL,0);
		return -1;
	}  
	
	if( input_block_num(9,15,&bno) != 0 )
	{
		RCX_Close();
		return 2;
	}
	
	if(input_block_content(databuf)!=0)
	{
		RCX_Close();
		return 2;
	}
	ASCIIHex2Char(databuf,8);	
	clr_scr();
		
	SPT_set(1);
	for ( trycnt=11; trycnt>0; )	
	{
		if(!SPT_read())
		{			
			sprintf(tmpbuf,hintstr1,--trycnt);
			DispStr_CE(0,0,tmpbuf,DISP_POSITION|DISP_CLRLINE);
			SPT_set(64);
  		}
  		
		ret = FM11RF_Request(PICC_REQALL,ATQ);
		if( (ret==RCX_OK)&&(ATQ[0]==0x05) )
		{
			FM11RF_Select1(sno,&sak);
			memset(sno,0x00,sizeof(sno));
			ret = FM11RF_Read4Bytes(1,sno);
		}				
		if( ret==RCX_OK )
		{
			ret = FM11RF_AuthKey(PICC_AUTHENT1A,sno,FM_PASS,bno);
		}
		if(ret==RCX_OK)
		{
			read_ok = 1;
			break;
		}
		RCX_RF_Reset(10);			
	}
			
	clr_scr();
	ret = FM11RF_Write4Bytes(bno,databuf);	
	if(ret != RCX_OK)
	{
		DispStr_CE(0,3,hintstr3,DISP_POSITION|DISP_CLRSCR);		
		warning_beep(1);		
		delay_and_wait_key( 0, EXIT_KEY_ALL, 0 );
		return 1;
	}
		
	RCX_Close();
	DispStr_CE(0,3,hintstr4,DISP_POSITION|DISP_CLRLINE);
	warning_beep(0);		
	delay_and_wait_key( 0, EXIT_KEY_ALL, 0 );
	return 0;	
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?