0

I've searched for quite some time now for a solution to my problem. I'd like to read RFID tags on my Raspberry, but I want to do it in C-code, as the rest of my project is written in C.

I have a few questions to the following C-code I found here on stackoverflow.com:

  1. What .h includes do I need for the code below?
  2. Where is the interface setup, that I'm using? (/dev/ttyAMA0)

Please help me to get this code snippet working right, as it's been holding me up for a few days already.

char read_rfid(char* rfid_num) {
    fd_set input_fdset;
    ssize_t length;
    int done;

    for(done=0; done < 14; ) {
        FD_ZERO(&input_fdset);
        FD_SET(fd,&input_fdset);

        if(select(fd+1 ,&input_fdset, NULL,NULL,NULL) == -1) {
            if (errno == EAGAIN) continue;
            perror("Terminal select() failed");
            return -1;
        }

        if(FD_ISSET(fd,&input_fdset)) {
            if((length = read(fd,rfid_num+done,14-done)) == -1) {
                if (errno == EAGAIN) continue;
                perror("Terminal: read() failed");
                return -1;
            }
            write(STDOUT_FILENO,rfid_num+done,length);
            done += length;
        }
    }
    return 0;
}


int setupRS232()
{
    struct termios term_attr;

    if((fd = open(RFID,O_RDWR)) == -1)
    {
        perror("Can't open Device");
        return(1);
    }
    if(tcgetattr(fd,&term_attr) != 0)
    {
        perror("terminal: tcgetattr() failed");
        return(1);
    }
    term_attr.c_cflag = BAUD|CS8|CRTSCTS|CLOCAL|CREAD;
    term_attr.c_iflag = 0;
    term_attr.c_oflag = 0;
    term_attr.c_lflag = 0;
    if(tcsetattr(fd,TCSAFLUSH,&term_attr) != 0)
    {   
        perror("terminal: tcsetattr() failed");
        return(1);
    }
}


int main(int argc, char** argv)
{

    char rfid_num[14];
    int i;


    if(setupRS232() == 1)
        return(1);

    puts("Waiting for transponder...");
    read_rfid(rfid_num);
    for(i=0;i<20;i++)
    {
        printf("%x\n",rfid_num[i]);
    }     
}
wildplasser
  • 43,142
  • 8
  • 66
  • 109
user274950
  • 35
  • 6

1 Answers1

0
#include <unistd.h> // for read & write functions
#include <sys/select.h> // fd_set functions
#include <stdio.h> // for perror & printf family
#include <sys/types.h> // for open related function
#include <sys/stat.h> // for open related function
#include <fcntl.h> // for open related function
#include <termios.h> // for terminal functions
#include <errno.h> // for error code

#define RFID "path_to_rfid" // FIXME <- you should set this properly
char read_rfid(char* rfid_num, int fd)
{
    fd_set input_fdset;
    ssize_t length;
    int done;

    for(done=0; done < 14; ) {
        FD_ZERO(&input_fdset);
        FD_SET(fd,&input_fdset);

        if(select(fd+1 ,&input_fdset, NULL,NULL,NULL) == -1) {
            perror("Terminal select() failed");
            return -1;
        }

        if(FD_ISSET(fd,&input_fdset)) {
            if((length = read(fd,rfid_num+done,14-done)) == -1) {
                perror("Terminal: read() failed");
                return -1;
            }
            write(STDOUT_FILENO,rfid_num+done,length);
            done += length;
        }
    }
    return 0;
}


int setupRS232()
{
    struct termios term_attr;
    int fd = 0;

    if((fd = open(RFID,O_RDWR)) == -1) {
        perror("Can't open Device");
        return(-1);
    }
    if(tcgetattr(fd,&term_attr) != 0)
    {
        perror("terminal: tcgetattr() failed");
        close(fd);
        return(-1);
    }

    term_attr.c_cflag = CBAUD|CS8|CRTSCTS|CLOCAL|CREAD;
    term_attr.c_iflag = 0;
    term_attr.c_oflag = 0;
    term_attr.c_lflag = 0;
    if(tcsetattr(fd,TCSAFLUSH,&term_attr) != 0) {
        perror("terminal: tcsetattr() failed");
        close(fd);
        return(-1);
    }
    return (fd);
}

int main(int argc, char** argv)
{

    char rfid_num[14];
    int i;
    int fd;

    if((fd = setupRS232()) == -1) {
        return(-1);
    }

    puts("Waiting for transponder...");
    read_rfid(rfid_num, fd);
    for(i=0;i<20;i++) {
        printf("%x\n",rfid_num[i]);
    }

    return 0;
}

Now your code is ready to be compiled, at least in my machine I could compile with no errors.

campescassiano
  • 809
  • 5
  • 18
  • I recommend you to use man-pages to find out what you need to include in a given source-code. For exemple, you see the functions the code is using and then you can find this function in man-page to see what is needed to include in your code. Also I could notice that in function setupRS232() may be missing the fd variable. You could fix it passing the fd as a parameter-> setupRS232(int fd); – campescassiano Aug 15 '15 at 16:21
  • Thank you Ccampes, that absolutely made my day! I really appreciate that you took the time and answered my question! Thank you so much! – user274950 Aug 16 '15 at 02:39