Temporarily disabling RFID reader in C code
Hey there. This is the first time I've worked with programming any type of peripheral device so this is hopefully a very simple question. I'm using the usb RFID reader to allow people to clock in and out. Through a long week of researching, trial and error, I can get the cards to be read, convert the hex to decimal, and am almost ready to try and tie it into the php code for the timeclock software. The problem is that after a valid card read event, I would like to disable the rfid reader for maybe 2 seconds or so to avoid double reads.
I'm running OpenSUSE 11.0 with kernel 2.6.25.20-0.1 by the way.
Here's the code I have right now.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
/* baudrate settings are defined in <asm/termbits.h>, which is
included by <termios.h> */
#define BAUDRATE B2400
/* change this definition for the correct port */
#define MODEMDEVICE "/dev/ttyUSB1"
#define _POSIX_SOURCE 1 /* POSIX compliant source */
#define CARD_LENGTH 11
#define FALSE 0
#define TRUE 1
volatile int STOP=FALSE;
int main()
{
int fd,c, res;
unsigned int num;
struct termios oldtio,newtio;
char buf[noparse][[/noparse]CARD_LENGTH],ch,*ptr = buf;
/*
Open modem device for reading and writing and not as controlling tty
because we don't want to get killed if linenoise sends CTRL-C.
*/
fd = open(MODEMDEVICE, O_RDWR/* | O_NOCTTY*/ );
if (fd <0) {perror(MODEMDEVICE); exit(-1); }
tcgetattr(fd,&oldtio); /* save current serial port settings */
bzero(&newtio, sizeof(newtio)); /* clear struct for new port settings */
/*
BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed.
CRTSCTS : output hardware flow control (only used if the cable has
all necessary lines. See sect. 7 of Serial-HOWTO)
CS8 : 8n1 (8bit,no parity,1 stopbit)
CLOCAL : local connection, no modem contol
CREAD : enable receiving characters
*/
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
/*
IGNPAR : ignore bytes with parity errors
ICRNL : map CR to NL (otherwise a CR input on the other computer
will not terminate input)
otherwise make device raw (no other input processing)
*/
newtio.c_iflag = IGNPAR | ICRNL;
/*
Raw output.
*/
newtio.c_oflag = 0;
/*
ICANON : enable canonical input
disable all echo functionality, and don't send signals to calling program
*/
newtio.c_lflag = ICANON;
/*
initialize all control characters
default values can be found in /usr/include/termios.h, and are given
in the comments, but we don't need them here
*/
newtio.c_cc[noparse][[/noparse]VINTR] = 0; /* Ctrl-c */
newtio.c_cc[noparse][[/noparse]VQUIT] = 0; /* Ctrl-\ */
newtio.c_cc[noparse][[/noparse]VERASE] = 0; /* del */
newtio.c_cc[noparse][[/noparse]VKILL] = 0; /* @ */
newtio.c_cc[noparse][[/noparse]VEOF] = 4; /* Ctrl-d */
newtio.c_cc[noparse][[/noparse]VTIME] = 0; /* inter-character timer unused */
newtio.c_cc[noparse][[/noparse]VMIN] = 1; /* blocking read until 1 character arrives */
newtio.c_cc[noparse][[/noparse]VSWTC] = 0; /* '\0' */
newtio.c_cc[noparse][[/noparse]VSTART] = 0; /* Ctrl-q */
newtio.c_cc[noparse][[/noparse]VSTOP] = 0; /* Ctrl-s */
newtio.c_cc[noparse][[/noparse]VSUSP] = 0; /* Ctrl-z */
newtio.c_cc[noparse][[/noparse]VEOL] = 0; /* '\0' */
newtio.c_cc[noparse][[/noparse]VREPRINT] = 0; /* Ctrl-r */
newtio.c_cc[noparse][[/noparse]VDISCARD] = 0; /* Ctrl-u */
newtio.c_cc[noparse][[/noparse]VWERASE] = 0; /* Ctrl-w */
newtio.c_cc[noparse][[/noparse]VLNEXT] = 0; /* Ctrl-v */
newtio.c_cc[noparse][[/noparse]VEOL2] = 0; /* '\0' */
/*
now clean the modem line and activate the settings for the port
*/
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
/*
terminal settings done, now handle input
In this example, inputting a 'z' at the beginning of a line will
exit the program.
*/
while (1)
{
res = read(fd,buf,CARD_LENGTH);
if (res == CARD_LENGTH)
{
/* need to disable reader for a few seconds */
printf("card id before parse: %s", buf);
ptr++;
ptr++;
printf("card id after parse: %s",ptr);
sscanf(ptr, "%X", &num);
printf("as a decimal: %d\n",num);
}
}
/* restore the old port settings */
tcsetattr(fd,TCSANOW,&oldtio);
close(fd);
}
Thanks everybody,
gabe
I'm running OpenSUSE 11.0 with kernel 2.6.25.20-0.1 by the way.
Here's the code I have right now.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
/* baudrate settings are defined in <asm/termbits.h>, which is
included by <termios.h> */
#define BAUDRATE B2400
/* change this definition for the correct port */
#define MODEMDEVICE "/dev/ttyUSB1"
#define _POSIX_SOURCE 1 /* POSIX compliant source */
#define CARD_LENGTH 11
#define FALSE 0
#define TRUE 1
volatile int STOP=FALSE;
int main()
{
int fd,c, res;
unsigned int num;
struct termios oldtio,newtio;
char buf[noparse][[/noparse]CARD_LENGTH],ch,*ptr = buf;
/*
Open modem device for reading and writing and not as controlling tty
because we don't want to get killed if linenoise sends CTRL-C.
*/
fd = open(MODEMDEVICE, O_RDWR/* | O_NOCTTY*/ );
if (fd <0) {perror(MODEMDEVICE); exit(-1); }
tcgetattr(fd,&oldtio); /* save current serial port settings */
bzero(&newtio, sizeof(newtio)); /* clear struct for new port settings */
/*
BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed.
CRTSCTS : output hardware flow control (only used if the cable has
all necessary lines. See sect. 7 of Serial-HOWTO)
CS8 : 8n1 (8bit,no parity,1 stopbit)
CLOCAL : local connection, no modem contol
CREAD : enable receiving characters
*/
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
/*
IGNPAR : ignore bytes with parity errors
ICRNL : map CR to NL (otherwise a CR input on the other computer
will not terminate input)
otherwise make device raw (no other input processing)
*/
newtio.c_iflag = IGNPAR | ICRNL;
/*
Raw output.
*/
newtio.c_oflag = 0;
/*
ICANON : enable canonical input
disable all echo functionality, and don't send signals to calling program
*/
newtio.c_lflag = ICANON;
/*
initialize all control characters
default values can be found in /usr/include/termios.h, and are given
in the comments, but we don't need them here
*/
newtio.c_cc[noparse][[/noparse]VINTR] = 0; /* Ctrl-c */
newtio.c_cc[noparse][[/noparse]VQUIT] = 0; /* Ctrl-\ */
newtio.c_cc[noparse][[/noparse]VERASE] = 0; /* del */
newtio.c_cc[noparse][[/noparse]VKILL] = 0; /* @ */
newtio.c_cc[noparse][[/noparse]VEOF] = 4; /* Ctrl-d */
newtio.c_cc[noparse][[/noparse]VTIME] = 0; /* inter-character timer unused */
newtio.c_cc[noparse][[/noparse]VMIN] = 1; /* blocking read until 1 character arrives */
newtio.c_cc[noparse][[/noparse]VSWTC] = 0; /* '\0' */
newtio.c_cc[noparse][[/noparse]VSTART] = 0; /* Ctrl-q */
newtio.c_cc[noparse][[/noparse]VSTOP] = 0; /* Ctrl-s */
newtio.c_cc[noparse][[/noparse]VSUSP] = 0; /* Ctrl-z */
newtio.c_cc[noparse][[/noparse]VEOL] = 0; /* '\0' */
newtio.c_cc[noparse][[/noparse]VREPRINT] = 0; /* Ctrl-r */
newtio.c_cc[noparse][[/noparse]VDISCARD] = 0; /* Ctrl-u */
newtio.c_cc[noparse][[/noparse]VWERASE] = 0; /* Ctrl-w */
newtio.c_cc[noparse][[/noparse]VLNEXT] = 0; /* Ctrl-v */
newtio.c_cc[noparse][[/noparse]VEOL2] = 0; /* '\0' */
/*
now clean the modem line and activate the settings for the port
*/
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
/*
terminal settings done, now handle input
In this example, inputting a 'z' at the beginning of a line will
exit the program.
*/
while (1)
{
res = read(fd,buf,CARD_LENGTH);
if (res == CARD_LENGTH)
{
/* need to disable reader for a few seconds */
printf("card id before parse: %s", buf);
ptr++;
ptr++;
printf("card id after parse: %s",ptr);
sscanf(ptr, "%X", &num);
printf("as a decimal: %d\n",num);
}
}
/* restore the old port settings */
tcsetattr(fd,TCSANOW,&oldtio);
close(fd);
}
Thanks everybody,
gabe
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Engineering