Impossible management of magnetic card reader

This is a discussion on Impossible management of magnetic card reader within the C Programming forums, part of the General Programming Boards category; Hi. I'm developing a new pay-machine for a company, but I have a big problem: the magnetic card reader for ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    1

    Red face Impossible management of magnetic card reader

    Hi.

    I'm developing a new pay-machine for a company, but I have a big problem: the magnetic card reader for credit/debit cards.

    I'ts a Hitachi-Omrom V4KF with serial-RS232 interface to PC.

    C with Linux.

    I try to write/read on/from V4KF with no action/response at all.

    The manual says its syncs speed with first reset. Configuration is 8 bit data, even pariity. Always from manual, you send commands in ASCII text preceeded by a "C". No action. No response at all. I've changed reader, computer and cables. The same. None of the none.

    No information in Google. None.

    Anybody with experiencie in this kind of devices? I'm desperated becaus if I cant manage reader I had to left project loosing a lot of money... Help please...

    Below is the C code I use...

    Anybody knows something about this never-working-impossible-manage devices? Thanks in advance.

    C code for send commands:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <errno.h>
    #include <termios.h>
    
    int main (int argc,char *argv[]) {
    
            int fd,n;
            long baud;
            struct termios options,tp;
    
            baud=B38400;
    
            fd=open("/dev/ttyS0",O_RDWR|O_NOCTTY|O_NDELAY);
    
            if (fd>=0) {
                    fcntl(fd,F_SETFL,FNDELAY);
            } else {
                    printf("Imposible abrir /dev/ttyS0\n");
                    exit(-1);
            }
    
            if (tcgetattr(fd,&tp)<0) {
                    printf("No puedo leer los atributos del terminal.\n");
                    exit(-1);
            }
    
    
            printf("Atributos originales del serial:\n");
    
            printf("iflag - 0%o; oflag - 0%o; cflag - 0%o; lflag - 0%o\n",
                            tp.c_iflag, tp.c_oflag, tp.c_cflag, tp.c_lflag);
    
            options.c_cflag |= (CLOCAL|CREAD|baud);
            options.c_cflag |= PARENB;
            options.c_cflag &= ~PARODD;
            options.c_cflag &= ~CSTOPB;
            options.c_cflag &= ~CSIZE;
            options.c_cflag |= CS8;
            // options.c_cflag|=CRTSCTS;
    
            tcsetattr(fd,TCSAFLUSH,&options);
    
            if (tcsetattr(fd,TCSANOW,&options)<0) {
                    printf("No puedo poner los atributos al terminal.\n");
                    exit(-1);
            }
    
            cfsetispeed(&options,baud);
            cfsetospeed(&options,baud);
    
            if (tcgetattr(fd,&tp)< 0) {
                    printf("No puedo leer los atributos del terminal.\n");
                    exit(-1);
            }
    
            printf ("Nuevos atributos del terminal:\n");
    
            printf("iflag - 0%o; oflag - 0%o; cflag - 0%o; lflag - 0%o\n",
                            tp.c_iflag, tp.c_oflag, tp.c_cflag, tp.c_lflag);
    
            printf("Inicializa lector.\n);
            n=write(fd,"C00",3);
            printf("Sent: %d\n",n);
            printf("Sent: %d\n",n);
            if (n<0) fputs("Error al escribir en el lector\n",stderr);
    
            printf("Enciende LED.\n");
            n=write(fd,"CP7",3);
            printf("Sent: %d\n",n);
            if (n<0) fputs("Error al escribir en el lector\n",stderr);
    
            sleep(1);
    
            printf("Parpadea LED.\n");
            n=write(fd,"CP8",3);
            printf("Sent: %d\n",n);
            if (n<0) fputs("Error al escribir en el lector\n",stderr);
    
            sleep(1);
    
            printf("Apaga LED.\n");
            n=write(fd,"CP6",3);
            printf("Sent: %d\n",n);
            if (n<0) fputs("Error al escribir en el lector\n",stderr);
    
            close (fd);
    
            return(0);
    C code for read responses:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <errno.h>
    #include <termios.h>
    
    #define FALSE 0
    #define TRUE 1
    
    int main (int argc,char *argv[]) {
    
            int fd,n,stop;
            long baud;
            char buf[255];
            struct termios options,tp;
    
            baud=B38400;
            stop=FALSE;
    
            fd=open("/dev/ttyS0",O_RDWR|O_NOCTTY|O_NDELAY);
    
            if (fd>=0) {
                    fcntl(fd,F_SETFL,FNDELAY);
            } else {
                    printf("Imposible abrir /dev/ttyS0\n");
                    exit(-1);
            }
    
            if (tcgetattr(fd,&tp)< 0) {
                    printf("No puedo leer los atributos del terminal.\n");
                    exit(-1);
            }
    
            printf("Atributos originales del serial:\n");
    
            printf("iflag - 0%o; oflag - 0%o; cflag - 0%o; lflag - 0%o\n",
                            tp.c_iflag, tp.c_oflag, tp.c_cflag, tp.c_lflag);
    
            options.c_cflag |= (CLOCAL|CREAD|baud);
            options.c_cflag |= PARENB;
            options.c_cflag &= ~PARODD;
            options.c_cflag &= ~CSTOPB;
            options.c_cflag &= ~CSIZE;
            options.c_cflag |= CS8;
            // options.c_cflag|=CRTSCTS;
    
            tcflush(fd, TCIFLUSH);
            tcsetattr(fd,TCSAFLUSH,&options);
    
            if (tcsetattr(fd,TCSANOW,&options)<0) {
                    printf("No puedo poner los atributos al terminal.\n");
                    exit(-1);
            }
    
            cfsetispeed(&options,baud);
            cfsetospeed(&options,baud);
    
            if (tcgetattr(fd,&tp)< 0) {
                    printf("No puedo leer los atributos del terminal.\n");
                    exit(-1);
            }
    
            printf ("Nuevos atributos del terminal:\n");
    
            printf("iflag - 0%o; oflag - 0%o; cflag - 0%o; lflag - 0%o\n",
                            tp.c_iflag, tp.c_oflag, tp.c_cflag, tp.c_lflag);
    
            while (stop==FALSE) {
                    n=read(fd,buf,255);
                    buf[n]=0;
                    printf(":%s:%d\n",buf,n);
                    if (buf[0]=='z') stop=TRUE;
             }
    
            close(fd);
    
            return(0);
    
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,752
    Have you tried using a terminal program, such as minicom, GTKterm?

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    Several points

    1. Test the serial port with say "minicom" or "putty" or another serial program of your choice. If it's all ASCII data, it would be easy enough to see and type.

    There are several things to do to the code.
    1. Your options contains junk basically before you start setting things. After the 'get', do something like
    options = tp;
    Then start messing with the flags.

    2. tcsetattr(fd,TCSAFLUSH,&options);
    Since you follow with a "NOW", there is little point in doing this as well.

    3. cfsetispeed(&options,baud);
    You need to do these before calling setattr()

    4. Check n before using it as a subscript.
    > n=read(fd,buf,255);
    > buf[n]=0;
    Also, if you're using it to append a \0, then you need to make sure the size passed to read is one less than the actual buffer size.
    sizeof(buf)-1
    would be good.

    > I'm desperated becaus if I cant manage reader I had to left project loosing a lot of money
    Do we get a bonus if any of this works?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise Unwanted Output
    By pobri19 in forum C++ Programming
    Replies: 4
    Last Post: 09-15-2008, 04:07 AM
  2. Vector out of range program crash.
    By Shamino in forum C++ Programming
    Replies: 11
    Last Post: 01-18-2008, 04:37 PM
  3. Blackjack
    By Tommo in forum C Programming
    Replies: 10
    Last Post: 06-20-2007, 08:07 PM
  4. How can I access a struct (from a header file)?
    By loxslay in forum C++ Programming
    Replies: 3
    Last Post: 10-07-2006, 01:25 PM
  5. magnetic card reader prog.
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-28-2002, 09:23 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21