display output from serial port device

From: Robert Ngo (robertngo_at_perridot.com)
Date: 08/26/04


Date: Wed, 25 Aug 2004 16:37:58 -0800

i use the following code to connect to HA3035 proximity access controller
card reader through serial port. the problem is that the output from the
devide is in some strange character. how can i format these output into
human readable form.

#include <stdio.h> /* Standard input/output definitions */
    #include <string.h> /* String function definitions */
    #include <unistd.h> /* UNIX standard function definitions */
    #include <fcntl.h> /* File control definitions */
    #include <errno.h> /* Error number definitions */
    #include <termios.h> /* POSIX terminal control definitions */

    int reader(int fd)
    {
      char buffer[26] /* Input buffer */
      char *bufptr; /* Current char in buffer */
      int nbytes; /* Number of bytes read */
      int tries; /* Number of tries so far */

       /* send an command followed by a CR */
        write(fd, "EXXA\r", 4);

       /* read characters into our string buffer until we get a CR or NL */
        bufptr = buffer;
        while ((nbytes = read(fd, bufptr, buffer + sizeof(buffer) - bufptr
 - 1)) >0)
        {
          bufptr += nbytes;
          if (bufptr[-1] == '\n' || bufptr[-1] == '\r')
            break;
        }

       /* nul terminate the string and see if we got an OK response */
        *bufptr = '\0';

        write(1,buffer,sizeof(buffer));
        if (buffer == 0)
          return (-1);
        else
          return (0);
    }

    int main(void)
    {
        struct termios options;
        char buffer[26];
        int nread;
        int fd; /* File descriptor for the port */
        int run =1;

      fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY);
      if (fd == -1)
      {
       
        perror("open_port: Unable to open /dev/ttyS0 - ");
      }
      else
        fcntl(fd, F_SETFL, 0);

    tcgetattr(fd, &options);

 
    cfsetispeed(&options, B9600);
    cfsetospeed(&options, B9600);

    options.c_cflag |= (CLOCAL | CREAD);
    options.c_iflag |= (INPCK | ISTRIP);
    options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
    options.c_oflag &= ~OPOST;
    options.c_cc[VMIN] = 0;
    options.c_cc[VTIME] = 10;

    tcsetattr(fd, TCSANOW, &options);

   if(reader(fd)==-1)
        write(2, "error sending command to card reader\n",38);
   else
        write(1, "connection to reader is good\n",30);
   close(fd);
}



Relevant Pages

  • Re: electronic id card reader visual basic
    ... This is completely controlled by the card reader. ... If the connection is by serial port, then you would use System.IO.Ports.SerialPort, and the documentation from the card reader manufacturer to decode the actual data received. ... However, it is possible that the device uses a HID USB connection, and then you would have to wrap the USB APIs. ...
    (microsoft.public.dotnet.general)
  • Un-threading my Threads!
    ... I have a thread in my application that is listening to the Serial port using GetCommEvents. ... Now - because the card reader thread generated the event, my entire application seems to be using that thread, and so as soon as the thread gets killed, all my windows disappear, and the main thread is left running in the background with nothing to do. ... Can anybody tell me what I need to do to get around this problem, as I'm sure it's a fairly common problem, but I'm pretty new to multi-threading and it's giving me a headache! ...
    (microsoft.public.dotnet.languages.vb)
  • Re: connect Canon Coolpix 800 to PC (found)
    ... Coolpix 800. ... It indicates that we are talking serial port. ... My ThinkPad R40 has no serial port. ... the card reader should last for many, ...
    (alt.photography)
  • Re: ead from serial port in c
    ... > how to read from serial port with c on a linux server, the serail port is ... > connect to a card reader. ...
    (comp.os.linux.development.apps)