⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 14443source.c

📁 非接触式IC卡串口读写程序源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/********************************************************************************               				14443 OPERATED FUNCTION * Filename  	:   	14443Source.c** Description   :  		functions used fo operating RFID** Author    	:   	Jun Jiang** Date      	:   	2008.09.27********************************************************************************//********************************************************************************								INCLUDE HEADER********************************************************************************/#include     <stdio.h>#include     <stdlib.h>#include	 "Port.h"#include	 "14443Source.h"/*********************************************************************************                              MACRO DEFINITION ********************************************************************************/#define		BELL_ON			(1 << 0)#define		LED_ON			(1 << 1)#define		LED_BELL_ON		(1 << 4)#define		AUTO_RD_ON		(1 << 5)/********************************************************************************							VARIABLE DEFINITION	********************************************************************************/static char	LedBellSwitch = 0x00;	// static variable used to control LED and Bell/********************************************************************************							PROTOTYPE DECLEARATION	********************************************************************************/static int SetAR_LED_Bell(int MacNum, int SwitchValue, int Fd, int Baudrate);/*******************************************************************************	*							READ VERSION* * Description:  read version of reader* * Arguments  :  int MacNum      number of machine*               char*p_ version pointer of area stored version value*               int Fd          handler of device*               int Baudrate    value of Baudrate * * Return     :  int*               0               success*               1               parameter error*               2               timeout*               3               acknowledge failed*				4				function:select error*				5				receive incompleted** Note       :	timeout value is 200ms  ********************************************************************************/int ReadVersion(int MacNum, char* p_Version, int Fd, int  Baudrate){    int     event;    int     count;    int		send_len;	int 	recv_len;	char    send_bff[4];    char    recv_bff[15];    if (MacNum > 255 || MacNum < 0	    || NULL == p_Version)        return  1;                          // parameter error!        send_bff[0] = DLE;    send_bff[1] = MacNum & 0xFF;            // machine number    send_bff[2] = 'V';                      // command byte	send_len = 3;							// send 3 bytes	        do {        event = WriteCom(Fd, send_bff, send_len);  // tx command frame    } while (event != send_len); 	recv_len = 14;							// receive data 14 bytes	    event = ReadCom(Fd, recv_bff, recv_len, Baudrate, 200);        if (recv_len == event) {				// receive completed        if ( DLE != recv_bff[0])            return  3;          if ((MacNum & 0xFF) != recv_bff[1])            return  3;          count = recv_bff[2];        memcpy(p_Version, (recv_bff + 3), count);        *(p_Version + count) = '\0';            return 0;                           // success    }       else if (-1 == event) {                 // timeout        return 2;    }	else if (-2 == event) {					// function:select error!		return 4;								}   	else {		return 5;							// receive incompleted	}}/*******************************************************************************	*							SET AUTO READ								* * Description:  config the reader's auto read function* * Arguments  :  int MacNum      number of machine*				int	SwitchValue		switch of auto read*               int	 Fd          handler of device*               int  Baudrate    value of Baudrate * * Return     :  int*               0               success*               1               parameter error*               2               timeout*               3               acknowledge failed*				4				function:select error*				5				receive incompleted** Note       :	timeout value is 200ms  ********************************************************************************/static int SetAR_LED_Bell(int MacNum, int SwitchValue, int Fd, int Baudrate){    int		i;	int     event;	int		send_len;	int		recv_len;	char	chk_code[7] = {DLE, (MacNum & 0xFF), 0x04, 'D', 'o', 'n', 'e'};    char    send_bff[5];    char    recv_bff[8];    if (MacNum > 255 || MacNum < 0)        return  1;                          // parameter error!        send_bff[0] = DLE;    send_bff[1] = MacNum & 0xFF;            // machine number    send_bff[2] = 'B';                      // command byte    send_bff[3] = SwitchValue;		send_len = 4;							// send data is 4 bytes		    do {        event = WriteCom(Fd, send_bff, send_len);  // tx command frame    } while (event != send_len);	recv_len = 7;							// receive data is 7 bytes     event = ReadCom(Fd, recv_bff, recv_len, Baudrate, 200);        if (recv_len == event) {				// receive completed        		for (i = 0; i < recv_len; i++) {			if (recv_bff[i] != chk_code[i])				break;		}		if (recv_len == i)			return 0;                       // success		else			return 3;						// ack failed    }       else if (-1 == event) {                 // timeout        return 2;    }	else if (-2 == event) {					// function:select error!		return 4;								}   	else {		return 5;							// receive incompleted	}}/*******************************************************************************	*							SET AUTO READ 								* * Description:  config the reader's auto read function* * Arguments  :  int MacNum      number of machine*				int Switch	    1 on; 0 off	*               int Fd          handler of device*               int Baudrate    value of Baudrate * * Return     :  int*               0               success*               1               parameter error*               2               timeout*               3               acknowledge failed*				4				function:select error*				5				receive incompleted** Note       :	timeout value is 150ms  ********************************************************************************/int SetAutoRead(int MacNum, int Switch, int Fd, int Baudrate){	switch (Switch) {				case 0:			LedBellSwitch &= (~AUTO_RD_ON);		// turn off auto read		break;				case 1:		default:			LedBellSwitch |= AUTO_RD_ON;		// turn on auto read		break;	}		return SetAR_LED_Bell(MacNum, LedBellSwitch, Fd, Baudrate);}/*******************************************************************************	*							SET LED BELL  								* * Description:  config the reader's LED and Bell * * Arguments  :  int MacNum      number of machine*				int Switch	    1 on; 0 off	*               int Fd          handler of device*               int Baudrate    value of Baudrate * * Return     :  int*               0               success*               1               parameter error*               2               timeout*               3               acknowledge failed*				4				function:select error*				5				receive incompleted** Note       :	timeout value is 150ms  ********************************************************************************/int SetLedBell(int MacNum, int Switch, int Fd, int Baudrate){	switch (Switch) {				case 0:			LedBellSwitch &= (~LED_BELL_ON);	// turn off LED and Bell		break;				case 1:		default:			LedBellSwitch |= LED_BELL_ON;		// turn on LED and Bell		break;	}		return SetAR_LED_Bell(MacNum, LedBellSwitch, Fd, Baudrate);}/*******************************************************************************	*								SET LED * * Description:  config the reader's LED  * * Arguments  :  int MacNum      number of machine*				int Switch	    1 on; 0 off*               int Fd          handler of device*               int Baudrate    value of Baudrate * * Return     :  int*               0               success*               1               parameter error*               2               timeout*               3               acknowledge failed*				4				function:select error*				5				receive incompleted** Note       :	timeout value is 150ms  ********************************************************************************/int SetLed(int MacNum, int Switch, int Fd, int Baudrate){	switch (Switch) {				case 0:			LedBellSwitch &= (~LED_ON);			// turn off LED 		break;				case 1:		default:			LedBellSwitch |= LED_ON;			// turn on LED 		break;	}		return SetAR_LED_Bell(MacNum, LedBellSwitch, Fd, Baudrate);}/*******************************************************************************	*								SET BELL  								* * Description:  config the reader's Bell * * Arguments  :  int MacNum      number of machine*				int Switch		1 on; 0 off*               int Fd          handler of device*               int Baudrate    value of Baudrate * * Return     :  int*               0               success*               1               parameter error*               2               timeout*               3               acknowledge failed*				4				function:select error*				5				receive incompleted** Note       :	timeout value is 150ms  ********************************************************************************/int SetBell(int MacNum, int Switch, int Fd, int Baudrate){	switch (Switch) {				case 0:			LedBellSwitch &= (~BELL_ON);		// turn off Bell		break;				case 1:		default:			LedBellSwitch |= BELL_ON;			// turn on  Bell		break;	}		return SetAR_LED_Bell(MacNum, LedBellSwitch, Fd, Baudrate);}/*******************************************************************************	*						GET MACHINE NUMBER	* * Description:  get number of machine* * Arguments  :  int* p_MacNum	pointer of area stored machine's NO.	*               int Fd          handler of device*               int Baudrate    value of Baudrate * * Return     :  int*               0               success*               1               parameter error*               2               timeout*               3               acknowledge failed*				4				function:select error*				5				receive incompleted** Note       :	timeout value is 200ms  ********************************************************************************/int GetMacNum(int* p_MacNum, int Fd, int Baudrate){    int		i;	int     event;	int		send_len;	int		recv_len;	char	chk_code[6] = {DLE, 0xAA, 0x03, 'O', 'K', '!'};    char    send_bff[4];    char    recv_bff[7];	if (NULL == p_MacNum)		return 1;	    send_bff[0] = DLE;    send_bff[1] = 0xFF;            			// arbitary machine number    send_bff[2] = 'A';                      // command byte    	send_len = 3;							// send data is 3 bytes		    do {        event = WriteCom(Fd, send_bff, send_len);  // tx command frame    } while (event != send_len);	recv_len = 6;							// receive data is 6 bytes     event = ReadCom(Fd, recv_bff, recv_len, Baudrate, 200);//  event = ReadCom(Fd, recv_bff, recv_len, Baudrate, 500);        if (recv_len == event) {				// receive completed        		for (i = 0; i < recv_len; i++) {			if (1 == i)				continue;					// skip over recv_bff[1](MacNum)						if (recv_bff[i] != chk_code[i])				break;		}		if (recv_len == i) {			*p_MacNum = (int)recv_bff[1];			return 0;                       // success		} else			return 3;						// ack failed    }       else if (-1 == event) {                 // timeout        return 2;    }	else if (-2 == event) {					// function:select error!		return 4;								}   	else {		return 5;							// receive incompleted	}}/********************************************************************************						SET NEW MACHINE NUMBER	* * Description:  set new number of machine* * Arguments     int MacNum		machine number*               int NewMacNum	new machine number*				int Fd			handler of device*				int Baudrate	baudrate value*				 * Return     :  int*               0               success*               1               parameter error*               2               timeout*               3               acknowledge failed*				4				function:select error*				5				receive incompleted** Note       :	timeout value is 150ms  ********************************************************************************/int SetNewMacNum(int MacNum, int NewMacNum, int Fd, int Baudrate){    int		i;	int     event;	int		send_len;	int		recv_len;	char	chk_code[7] = {DLE, (MacNum & 0xFF), 0x04, 'D', 'o', 'n', 'e'};    char    send_bff[5];    char    recv_bff[8];	if (MacNum > 255 || MacNum < 0		|| NewMacNum > 255 || NewMacNum < 0)		return 1;							// parameter error	    send_bff[0] = DLE;    send_bff[1] = (MacNum & 0xFF); 			// machine number    send_bff[2] = 'a';                      // command byte    send_bff[3] = (NewMacNum & 0xFF);	send_len = 4;							// send data is 3 bytes		    do {        event = WriteCom(Fd, send_bff, send_len);  // tx command frame    } while (event != send_len);	recv_len = 7;							// receive data is 6 bytes     event = ReadCom(Fd, recv_bff, recv_len, Baudrate, 150);        if (recv_len == event) {				// receive completed        		for (i = 0; i < recv_len; i++) {						if (recv_bff[i] != chk_code[i])				break;		}		if (recv_len == i) {			return 0;                       // success		} else			return 3;						// ack failed     }       else if (-1 == event) {                 // timeout        return 2;    }	else if (-2 == event) {					// function:select error!		return 4;								}   	else {		return 5;							// receive incompleted	}}/********************************************************************************								READ A BLOCK	* * Description:  read a block of card** Arguments     int 	MacNum		machine number*             	int 	KeyAddr		key address*				int 	BlkNum		block number*				char* 	p_BlkData	pointer of area stored block data*				int 	Fd			handler of device*				int 	Baudrate	baudrate value*				 * Return     :  int*               0               success*               1               parameter error*               2               timeout*               3               acknowledge failed*				4				read block failed*				5				function:select error*				6				receive incompleted** Note       :	timeout value is 200ms  ********************************************************************************/int ReadBlk(int MacNum, int KeyAddr, int BlkNum, char* p_BlkData, int Fd, int Baudrate){    int		i;	int     count;	int     event;	int		send_len;	int		recv_len;	char	chk_code[7] = {DLE, (MacNum & 0xFF), 0x04, 'F', 'a', 'i', 'l'};    char    send_bff[6];    char    recv_bff[20];	if ( MacNum > 255 || MacNum < 0		|| (KeyAddr & 0x7F) > 23 || KeyAddr < 0		|| BlkNum > (64 * 4 - 1) || BlkNum < 0		|| NULL == p_BlkData )		return 1;							// parameter error	    send_bff[0] = DLE;    send_bff[1] = (MacNum & 0xFF); 			// machine number    send_bff[2] = 'R';                      // command byte    send_bff[3] = (KeyAddr & 0x7F);			// key address	send_bff[4] = (BlkNum  & 0xFF);			// block number

⌨️ 快捷键说明

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