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

📄 ext_rfcard.c

📁 奥尔斯公司pxa270试验箱射频卡读写器相关资料
💻 C
字号:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <fcntl.h>      // open() close()
#include <unistd.h>     // read() write()

#include <termios.h>    // set baud rate

#include <fcntl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>


#define DEVICE_TTYS "/dev/ttyS1"
#define MY_BAUD_RATE B19200


#define RECEIVE_BUF_WAIT_0S 0
#define RECEIVE_BUF_WAIT_1S 1
#define RECEIVE_BUF_WAIT_2S 2
#define RECEIVE_BUF_WAIT_3S 3
#define RECEIVE_BUF_WAIT_4S 4
#define RECEIVE_BUF_WAIT_5S 5

char Beep[5];
char GLed_Off[5];
char GLed_On[5];
char GLed_Glint[5];
char Reset_Card[4]; 
char Read_Card[4];
char Addr = 0;

//------------------------------------- read datas from ttyS ------------------------------------------------
// succese return 1
// error   return 0
int read_datas_ttyS(int fd, char *rcv_buf,int rcv_wait)
{
        int retval;
        fd_set rfds;
        struct timeval tv;

        int ret,pos;

        tv.tv_sec = rcv_wait;   
        tv.tv_usec = 100000;

        pos = 0; // point to rceeive buf

        while (1)
        {
                FD_ZERO(&rfds);
                FD_SET(fd, &rfds);

                retval = select(fd+1 , &rfds, NULL, NULL, &tv);

                if (retval == -1)
                {
                        perror("select()");
                        break;
                }
                else if (retval)
                {// pan duan shi fou hai you shu ju
                        ret = read(fd, rcv_buf+pos, 2048);
                        pos += ret;
                        if (rcv_buf[pos-2] == '\r' && rcv_buf[pos-1] == '\n')
                        {
                                FD_ZERO(&rfds);
                                FD_SET(fd, &rfds);

                                retval = select(fd+1 , &rfds, NULL, NULL, &tv);

                                if (!retval) break;// no datas, break
                        }
                }
                else
                {
//                        printf("No data\n");
                        break;
                }
        }

        return 1;
} // end read_datas_ttyS


//------------------------------------- send the command -------------------------------------
int send_command(int fd, char *cmd_buf, int size)
{	
	ssize_t ret;
	int i;

	// send datas
	ret = write(fd,cmd_buf,size);
        if (ret == -1) {
        	printf ("write device %s error\n", DEVICE_TTYS);
                return -1;
        }
	
	return 0;

} // end func_485_receive


//------------------------------------- init seriel port  ---------------------------------------------------
void init_ttyS(int fd)
{
        struct termios options;

        bzero(&options, sizeof(options));       	// clear options

        cfsetispeed(&options,MY_BAUD_RATE);            	// setup baud rate
        cfsetospeed(&options,MY_BAUD_RATE);

        options.c_cflag |= ( CS8 | CLOCAL | CREAD);
        options.c_iflag = IGNPAR;

        tcflush(fd, TCIFLUSH);

        tcsetattr(fd, TCSANOW, &options);

}//end init_ttyS


//------------------------------------- make command ----------------------------------------------------------------
int make_command(void)
{
	// initialize the command 
	//Beep
	Beep[0] = Addr;
        Beep[1] = 3;
        Beep[2] = 2;
        Beep[3] = 10;
        Beep[4] = Beep[0] ^ Beep[1] ^ Beep[2] ^ Beep[3];
	
	//Turn off Green Led 
	GLed_Off[0] = Addr;
	GLed_Off[1] = 3;
	GLed_Off[2] = 3;
	GLed_Off[3] = 0;
	GLed_Off[4] = GLed_Off[0] ^ GLed_Off[1] ^ GLed_Off[2] ^ GLed_Off[3]; 

	//Turn on Green Led
	GLed_On[0] = Addr;
        GLed_On[1] = 3;
        GLed_On[2] = 3;
        GLed_On[3] = 1;
        GLed_On[4] = GLed_On[0] ^ GLed_On[1] ^ GLed_On[2] ^ GLed_On[3];

	//Green Led glint
	GLed_Glint[0] = Addr;
	GLed_Glint[1] = 3;
	GLed_Glint[2] = 4;
	GLed_Glint[3] = 10;
	GLed_Glint[4] = GLed_Glint[0] ^ GLed_Glint[1] ^ GLed_Glint[2] ^ GLed_Glint[3];

	//Reset the card
	Reset_Card[0] = Addr;
	Reset_Card[1] = 2;
	Reset_Card[2] = 5;
	Reset_Card[3] = Reset_Card[0] ^ Reset_Card[1] ^ Reset_Card[2];

	//Read Card Info
	Read_Card[0]= Addr;
	Read_Card[1] = 2;
	Read_Card[2] = 1;
	Read_Card[3] = Read_Card[0] ^ Read_Card[1] ^ Read_Card[2];	
}


//------------------------------------- main ----------------------------------------------------------------
int main(void)
{
        int fd;
	int i;
	char option;
	char rcv_buf[128];
	int addr;
	int cmd_type;
	int datalen;

        // open seriel port
        fd = open(DEVICE_TTYS, O_RDWR);

        if (fd == -1) {
                printf("open device %s error\n",DEVICE_TTYS);
        }

	init_ttyS(fd);  	// init device

	while(1) {
		make_command();
		printf(
"Plese select the operation:
 1:	Beep
 2:	Turn off the led
 3:	Turn on the led
 4:	Led glint
 5:     Reset the card
 6:	Read the card
 a:	Change the address
 e:	Exit
 ---------------------------\n");

		scanf("%c", &option);
		getchar();

		switch(option) {
			case '1':
				send_command(fd, Beep, sizeof(Beep));
				break;
			case '2': 
				send_command(fd, GLed_Off, sizeof(GLed_Off));
				break;
			case '3': 
				send_command(fd, GLed_On, sizeof(GLed_On));
				break;
			case '4':
				send_command(fd, GLed_Glint, sizeof(GLed_Glint));
				break;
			case '5':
                                send_command(fd, Reset_Card, sizeof(Reset_Card));
                                break;
			case '6':
				send_command(fd, Read_Card, sizeof(Read_Card));
				break;
			case 'a':
				printf("Input the new address with enter!\n");
				scanf("%d", &addr);
				if(addr <= 255 && addr >= 0) {
					Addr = addr;
				} else {
					printf("Bad address!\n");
				}
				break;
			case 'e':
                		// close ttyS
				if (close(fd)!=0) printf("close device %s error",DEVICE_TTYS);
				return 0;
				break;
			default:
				printf("Bad options\n");
				continue;
				break;
		}			

		//Clear the receive buffer
		bzero(rcv_buf, sizeof(rcv_buf));

		//Read the return value
		if (read_datas_ttyS(fd,rcv_buf,RECEIVE_BUF_WAIT_0S)) {

		//For debug purpose
/*                	for(i=1; i<rcv_buf[1]; i++) {
				printf("%x ", rcv_buf[i+1]);
			}
			printf("\n");
*/
		}
                else {
                        printf ("read error\n");
                }

		cmd_type = rcv_buf[2];
		
		switch(cmd_type){
			case 1:
				printf("\n==========================\n");
				if (rcv_buf[3] == 0){
					printf("No Card!\n");
				} else {
					printf("Card Info:\n");
					printf("Card Type: Mifare one\n");
					printf("Serial Number: %x-%x-%x-%x\n", rcv_buf[3], rcv_buf[4], 
									rcv_buf[5], rcv_buf[6]);
				}
				printf("==========================\n\n");
				break;
			case 2:
				printf("\n==========================\n");
				if (rcv_buf[3] == 0){
					printf("Operate buzzer failed!\n");
				} else {
					printf("Operate buzzer succeed!\n");
				}
				printf("==========================\n\n");
				break;
			case 3:
				printf("\n==========================\n");
				if (rcv_buf[3] == 0){
                                        printf("Turn the green LED failed!\n");
                                } else {
                                        printf("Turn the green LED succeed!\n");
                                }
				printf("==========================\n\n");
                                break;
			case 4:
				printf("\n==========================\n");
				if (rcv_buf[3] == 0){
                                        printf("The green LED glint failed!\n");
                                } else {
                                        printf("The green LED glint succeed!\n");
                                }printf("==========================\n\n");
                                break;
			case 5:
				printf("\n==========================\n");
				if (rcv_buf[3] == 0){
                                        printf("Reset failed!\n");
                                } else {
                                        printf("Reset succeed!\n");
                                }
				printf("==========================\n\n");
                                break;
			default:
				break;
		}

		//Resolve the retrun value
	}

        return 0;
}// end main

⌨️ 快捷键说明

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