#include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <linux/input.h> #define DEBUG_ENV #ifdef DEBUG_ENV #define DEBUGF(fmt,args...) printf(fmt ,##args) #else #define DEBUGF(fmt,args...) #endif #define KEYWD "Sycreader RFID Technology" typedef struct _EvcMap { char code; char chr; } _EvcMap_t; _EvcMap_t _map_[] = { {KEY_1, '1'}, {KEY_2, '2'}, {KEY_3, '3'}, {KEY_4, '4'}, {KEY_5, '5'}, {KEY_6, '6'}, {KEY_7, '7'}, {KEY_8, '8'}, {KEY_9, '9'}, {KEY_0, '0'}, }; char evc_map(struct input_event* ev) { int i; for(i = 0; i < sizeof(_map_) / sizeof(_EvcMap_t); i++) { if(_map_[i].code == ev->code) { return _map_[i].chr; } } DEBUGF("No map:%d!\n", ev->code); return 0; } int ReadRFID (char *target, int time_sec, char* read_code) { struct timeval Timeout; struct input_event t; char time_flag=0; fd_set readfs; char path[256]; int keys_fd; char ret[2]; int rv=0; char getnum[16]; int i=0; *read_code = 0; if (time_sec > 0 && time_sec < 1200) time_flag=1; if (time_sec > 1200) { DEBUGF("set time to long\n"); return -1; } if (time_sec == 0) time_flag=0; sprintf(path, "/dev/input/%s", target); DEBUGF("path=[%s]\n", path); keys_fd = open (path, O_RDONLY); if (keys_fd <= 0) { DEBUGF ("open %s device error!\n", path); return -1; } /* set timeout value within input loop */ Timeout.tv_usec = 0; /* milliseconds */ Timeout.tv_sec = time_sec; /* seconds */ for(;;) { if(time_flag) { rv = select(keys_fd+1, &readfs, NULL, NULL, &Timeout); if (rv==0) { DEBUGF("timeout!!\n"); return -1; } } if(read(keys_fd, &t, sizeof (t)) == sizeof (t)) { if (t.type == EV_KEY && t.value == 0) { if((*read_code = evc_map(&t))) read_code++; else break; } } } close (keys_fd); return 0; } int FindPath (char **target) { char str[512]; char kwd[128]; char *pwd=NULL; char flag=0; FILE *fp = fopen ("/proc/bus/input/devices", "r"); int i=0, j=0; if (fp==NULL) { DEBUGF("Open Error!Check if the file is exist and you have the permission!\n"); return -1; } while(!feof(fp)) if(fgets(str,128,fp)!=NULL) { pwd=strstr(str, KEYWD); if (pwd) { DEBUGF("1-%s", pwd); flag=1; break; } } if(flag) { //target at 4th row fgets(str,128,fp); fgets(str,128,fp); fgets(str,128,fp); fgets(str,128,fp); DEBUGF("2-%s\n", str); pwd=strstr(str, "event"); DEBUGF("3-[%s]\n", pwd); strcpy(*target, pwd); pwd=*target; for(;;) { if(*pwd!=0) switch(*pwd) { case 0x20: case '\r': case '\n': *pwd=0; break; default: *(*target+i)= *pwd; pwd++; i++; break; } else break; } return 0; } else { target = NULL; return -1; } fclose(fp); } int main () { char *target; char read_code[32]; int i=0; target=malloc(sizeof(char)*8); i = FindPath(&target); if (i < 0) { DEBUGF("Can't find the RFID devices\n"); return -1; } DEBUGF("target=[%s]\n", target); ReadRFID(target, 0, read_code); printf("RFID:%s\n", read_code); free(target); return 0; }
2016年5月12日 星期四
Read RFID data
Read RFID data...
因為只需要數字 0~9,所以 map 只寫到 0~9 (感謝solar協助)
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言