Home > OS >  RFID embedded to read and write the source code
RFID embedded to read and write the source code

Time:10-07

Have bosses have to read and write the source code for RFID, in the embedded Linux ARM cortex a8 chip system C language source code, if you have read card system based on the source is very better, help!

CodePudding user response:

 
/* * * * * * * * * * * * * * * * * rfid in c * * * * * * * * * * * * * * * * */
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include


# ifdef __cplusplus
# if __cplusplus
Extern "C" {
# endif
__cplusplus # endif/* */

Volatile unsigned int cardid;
The static struct timeval timeout.

/* Settings window parameters: the rate of 9600 */
Void init_tty (int fd)
{
//statement set serial structure
Struct termios termios_new;
Clears out the structure//
Bzero (& amp; Termios_new, sizeof (termios_new));
//cfmakeraw () sets the end attribute, is to set the various parameters of a termios structure,
Cfmakeraw (& amp; Termios_new);
//set the baud rate
//termios_new. C_cflag=(B9600);
Cfsetispeed (& amp; Termios_new B9600);
Cfsetospeed (& amp; Termios_new B9600);
//CLOCAL and CREAD respectively used for local connection and can accept that, therefore, first of all, through a mask to activate these two options,
Termios_new. C_cflag |=CLOCAL | CREAD;
//by mask Settings data bits for eight
Termios_new. C_cflag & amp;=~ CSIZE;
Termios_new. C_cflag |=CS8 gives;
//set and white parity
Termios_new. C_cflag & amp;=~ PARENB;
//a stop bit
Termios_new. C_cflag & amp;=~ CSTOPB;
Tcflush (fd, TCIFLUSH);
//can be set up to receive character and waiting time, no special requirements can set it to 0
Termios_new. C_cc [VTIME]=10;
Termios_new. C_cc [VMIN]=1;
//used to clear the input/output buffer
Tcflush (fd, TCIFLUSH);
//after complete the configuration, you can use the following function to activate a serial port Settings
If (tcsetattr (fd, TCSANOW, & amp; Termios_new))
Printf (" Setting the serial1 failed! \n");

}


/* to calculate the checksum */
Unsigned char CalBCC (unsigned char * buf, int n)
{
int i;
Unsigned char BCC=0;
For (I=0; I & lt; n; I++)
{
BCC ^=* (buf + I);
}
Return (BCC) ~;
}


/* */request within the scope of antenna card
Int PiccRequest (int fd)
{
RBuf unsigned char WBuf [128], [128].
Int ret.
Fd_set RDFD;

Memset (WBuf, 0, 128);
Memset (RBuf, 1128);
WBuf [0]=0 x07;//frame length=7 Byte
WBuf [1]=0 x02;//package number=0, command type=0 x01
WBuf [2]=0 x41;//command='C'
WBuf [3]=0 x01;//message length=0
WBuf [4]=0 x52;//request mode: ALL=0 x52
WBuf [5]=CalBCC (WBuf, WBuf [0] - 2);//the checksum
WBuf [6]=0 x03;//end mark

FD_ZERO (& amp; RDFD);
FD_SET (fd, & amp; RDFD);

Write (fd, WBuf, 7);
Ret=select (fd + 1, & amp; RDFD, NULL, NULL, & amp; The timeout);
The 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 & lt; 0)
{
//printf (" ret=% d, % m \ n ", ret, errno); Q
break;
}
If (RBuf [2]==0 x00)//reply frame state part of 0 is request success
{
return 0;
}
break;
}
The return - 1;
}

Within the scope of collision/* and get maximum ID */
Int PiccAnticoll (int fd)
{
RBuf unsigned char WBuf [128], [128].
Int ret.
Fd_set RDFD;;
Memset (WBuf, 0, 128);
Memset (RBuf, 0128);
WBuf [0]=0 x08;//frame length=8 Byte
WBuf [1]=0 x02;//package number=0, command type=0 x01
WBuf [2]=0 x42;//command='B'
WBuf [3]=0 x02;//message length=2
WBuf [4]=0 x93;//collision 0 x93 collision - level
WBuf [5]=0 x00;//count 0
WBuf [6]=CalBCC (WBuf, WBuf [0] - 2);//the checksum
WBuf [7]=0 x03;//end mark

FD_ZERO (& amp; RDFD);
FD_SET (fd, & amp; RDFD);
Write (fd, WBuf, 8);

Ret=select (fd + 1, & amp; RDFD, NULL, NULL, & amp; The timeout);
The switch (ret)
{
Case 1:
Perror (" select error \ n ");
break;
Case 0:
Perror (" Timeout: ");
break;
Default:
Ret=read (fd, RBuf, 10);
If (ret & lt; 0)
{
//printf (" ret=% d, % m \ n ", ret, errno);
break;
}
If (RBuf [2]==0 x00)//response frame state part of 0 is obtained success ID
{
Cardid=(RBuf [4] <24) | (RBuf [5] <16) | (RBuf [6] <8) | RBuf [7];
return 0;
}
}
The return - 1;
}

/* read card 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 & lt; 0)
{
Fprintf (stderr, "open/dev/s3c2410_serial1 fail! \n");
The return - 1;
}
/* initialize serial port */
Init_tty (fd);
The timeout. Tv_sec=0;
The timeout. Tv_usec=200000;

/* request scope of antenna card */
If (PiccRequest (fd))
{
Printf (" The request failed! \n");
Close (fd);
The return - 1;
}
Within the scope of antenna, the collision/* to obtain the biggest ID */
If (PiccAnticoll (fd))
{
Printf (" Couldn 't get card - id! \n");
Close (fd);
The return - 1;
}
Printf (" card ID=% x \ n ", cardid);
* arg=cardid;
Close (fd);
return 1;
}



Int main ()
{

Unsigned int carID;

While (1)
{
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related