LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware
User Name
Password
Linux - Hardware This forum is for Hardware issues.
Having trouble installing a piece of hardware? Want to know if that peripheral is compatible with Linux?

Notices



Reply
 
Search this Thread
Old 04-07-2013, 11:43 AM   #1
Marvin_G
LQ Newbie
 
Registered: Dec 2012
Posts: 14

Rep: Reputation: Disabled
Fingerprint UART


Hello guys, i'm new here. i have a fingerprint UART reader and i wanna communicate with it through serial port.

To set the serial port configuration i used this command:

stty -F /dev/ttyUSB0 cs8 -cstopb -ixon raw speed 19200

To send command to the device:

perl -e'print "\xF5\x01\x00\x01\x03\x00\x03\xF5"'>/dev/ttyUSB0

To retrieve the data from the device and put it into .txt file:

cat </dev/ttyUSB0 > output.txt

problem: i got the incorrect format for the output which is this =>> . i've attahced the photo along with this thread (the output in the image quite blur, is actually /F5/'box'/00/00/00/00/'box'/F5)

p/s: its HEX input and output. whenever i get 01,02.03 or other than 00,then it will appear as boxes. please help me experts!

haha.png
 
Old 04-07-2013, 04:54 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 12,152

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
As you have discovered the input and output from this device is binary and therefore non ASCII characters are displayed as boxes. If you want to examine the data in the file you will need to use hex editor i.e. a console editor like hexedit or a GUI application, ghex.

Without knowing the device's communication protocol it is difficult to provide specific help but you will need to use something besides bash. What programming languages are you most familiar with using?
 
Old 04-08-2013, 02:51 AM   #3
Marvin_G
LQ Newbie
 
Registered: Dec 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
As you have discovered the input and output from this device is binary and therefore non ASCII characters are displayed as boxes. If you want to examine the data in the file you will need to use hex editor i.e. a console editor like hexedit or a GUI application, ghex.

Without knowing the device's communication protocol it is difficult to provide specific help but you will need to use something besides bash. What programming languages are you most familiar with using?
hello there,thanks for replying. C programming would be better. I know how to configure in C but how to send the command as above and retrieve the output data? any suggestion?

Last edited by Marvin_G; 04-08-2013 at 04:10 AM.
 
Old 04-08-2013, 04:37 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 12,152

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
I forgot to mention that cutecom is a GUI terminal program that can send/receive hex characters. Here are some guides to serial port programming.

http://www.tldp.org/HOWTO/Serial-Programming-HOWTO/
http://www.easysw.com/~mike/serial/serial.html
 
Old 04-08-2013, 04:42 AM   #5
Marvin_G
LQ Newbie
 
Registered: Dec 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
As you have discovered the input and output from this device is binary and therefore non ASCII characters are displayed as boxes. If you want to examine the data in the file you will need to use hex editor i.e. a console editor like hexedit or a GUI application, ghex.

Without knowing the device's communication protocol it is difficult to provide specific help but you will need to use something besides bash. What programming languages are you most familiar with using?
i have some code here but i dont know how to send command to the device and retrieve the data.
Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <fcntl.h>
    #include <sys/signal.h>
    #include <errno.h>
    #include <termios.h>

    void signal_handler_IO (int status);   /* definition of signal handler */

    int n;
    int fd;
    int connected;
    struct termios termAttr;
    struct sigaction saio;

    int main(int argc, char *argv[])
    {
         fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
         if (fd == -1)
         {
            perror("open_port: Unable to open /dev/ttyO1\n");
            exit(1);
         }

         saio.sa_handler = signal_handler_IO;
         saio.sa_flags = 0;
         saio.sa_restorer = NULL; 
         sigaction(SIGIO,&saio,NULL);

         fcntl(fd, F_SETFL, FNDELAY);
         fcntl(fd, F_SETOWN, getpid());

         tcgetattr(fd,&termAttr);
         baudRate = B19200; 
         cfsetispeed(&termAttr,B19200);
         cfsetospeed(&termAttr,B19200);
         termAttr.c_cflag &= ~PARENB;
         termAttr.c_cflag &= ~CSTOPB;
         termAttr.c_cflag &= ~CSIZE;
         termAttr.c_cflag |= CS8;
         termAttr.c_cflag |= (CLOCAL | CREAD);
         termAttr.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
         termAttr.c_iflag &= ~(IXON | IXOFF | IXANY);
         termAttr.c_oflag &= ~OPOST;
         tcsetattr(fd,TCSANOW,&termAttr);
         printf("UART1 configured....\n");

         connected = 1;
         while(connected == 1){
               // some code
         }

         close(fd);
         exit(0);             
    }

    void signal_handler_IO (int status)
    {
         printf("received data from UART.\n");
    }
 
Old 04-08-2013, 05:04 AM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 12,152

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Look at the first link I posted. It has some examples for sending / receiving data.
sending data
Code:
  write(fd,"\xF5\x01\x00\x01\x03\x00\x03\xF5",8);
Here is an excerpt from the first link for receiving. Basically any characters transmitted are displayed.
Code:
         if (wait_flag==FALSE) { 
            res = read(fd,buf,255);
            buf[res]=0;
            printf(":%s:%d\n", buf, res);
            if (res==1) STOP=TRUE; /* stop loop if only a CR was input */
            wait_flag = TRUE;      /* wait for new input */
          }
 
Old 04-08-2013, 05:14 AM   #7
Marvin_G
LQ Newbie
 
Registered: Dec 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
Look at the first link I posted. It has some examples for sending / receiving data.
sending data
Code:
  write(fd,"\xF5\x01\x00\x01\x03\x00\x03\xF5",8);
Here is an excerpt from the first link for receiving. Basically any characters transmitted are displayed.
Code:
         if (wait_flag==FALSE) { 
            res = read(fd,buf,255);
            buf[res]=0;
            printf(":%s:%d\n", buf, res);
            if (res==1) STOP=TRUE; /* stop loop if only a CR was input */
            wait_flag = TRUE;      /* wait for new input */
          }
thank you so much. i'll try first. anything wrong i'll update here.
 
Old 04-09-2013, 11:42 AM   #8
Marvin_G
LQ Newbie
 
Registered: Dec 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Marvin_G View Post
thank you so much. i'll try first. anything wrong i'll update here.
my code got problem. mind take a look for me? problem is the "received data from UART" didn't printed out.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <errno.h>
#include <termios.h>

void signal_handler_IO (int status);   /* definition of signal handler */

int n;
int fd;
int connected;
struct termios termAttr;
struct sigaction saio;

int main(int argc, char *argv[])
{
     fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
     if (fd == -1)
     {
        perror("open_port: Unable to open /dev/ttyO1\n");
        exit(1);
     }

     saio.sa_handler = signal_handler_IO;
     saio.sa_flags = 0;
     saio.sa_restorer = NULL; 
     sigaction(SIGIO,&saio,NULL);

     fcntl(fd, F_SETFL, FNDELAY);
     fcntl(fd, F_SETOWN, getpid());
     fcntl(fd, F_SETFL,  O_ASYNC ); /**<<<<<<------This line made it work.**/

     tcgetattr(fd,&termAttr);
     //baudRate = B115200;          /* Not needed */
     cfsetispeed(&termAttr,B115200);
     cfsetospeed(&termAttr,B115200);
     termAttr.c_cflag &= ~PARENB;
     termAttr.c_cflag &= ~CSTOPB;
     termAttr.c_cflag &= ~CSIZE;
     termAttr.c_cflag |= CS8;
     termAttr.c_cflag |= (CLOCAL | CREAD);
     termAttr.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
     termAttr.c_iflag &= ~(IXON | IXOFF | IXANY);
     termAttr.c_oflag &= ~OPOST;
     tcsetattr(fd,TCSANOW,&termAttr);
     printf("UART1 configured....\n");

     connected = 1;
     while(connected == 1){
           // some code

     }

     close(fd);
     exit(0);             
}

void signal_handler_IO (int status)
{
     printf("received data from UART.\n");
}
 
Old 04-09-2013, 06:52 PM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 12,152

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Did you actually add some code to print received characters? Take a look the excerpt from the links I posted.


Code:
        /* loop while waiting for input. normally we would do something
           useful here */ 
        while (STOP==FALSE) {
          printf(".\n");usleep(100000);
          /* after receiving SIGIO, wait_flag = FALSE, input is available
             and can be read */
          if (wait_flag==FALSE) { 
            res = read(fd,buf,255);
            buf[res]=0;
            printf(":%s:%d\n", buf, res);
            if (res==1) STOP=TRUE; /* stop loop if only a CR was input */
            wait_flag = TRUE;      /* wait for new input */
          }
        }
        /* restore old port settings */
        tcsetattr(fd,TCSANOW,&oldtio);
      }
        
      /***************************************************************************
      * signal handler. sets wait_flag to FALSE, to indicate above loop that     *
      * characters have been received.                                           *
      ***************************************************************************/
        
      void signal_handler_IO (int status)
      {
        printf("received SIGIO signal.\n");
        wait_flag = FALSE;
      }
 
Old 04-10-2013, 01:51 AM   #10
Marvin_G
LQ Newbie
 
Registered: Dec 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
Did you actually add some code to print received characters? Take a look the excerpt from the links I posted.


Code:
        /* loop while waiting for input. normally we would do something
           useful here */ 
        while (STOP==FALSE) {
          printf(".\n");usleep(100000);
          /* after receiving SIGIO, wait_flag = FALSE, input is available
             and can be read */
          if (wait_flag==FALSE) { 
            res = read(fd,buf,255);
            buf[res]=0;
            printf(":%s:%d\n", buf, res);
            if (res==1) STOP=TRUE; /* stop loop if only a CR was input */
            wait_flag = TRUE;      /* wait for new input */
          }
        }
        /* restore old port settings */
        tcsetattr(fd,TCSANOW,&oldtio);
      }
        
      /***************************************************************************
      * signal handler. sets wait_flag to FALSE, to indicate above loop that     *
      * characters have been received.                                           *
      ***************************************************************************/
        
      void signal_handler_IO (int status)
      {
        printf("received SIGIO signal.\n");
        wait_flag = FALSE;
      }
i did look through this, what SIGIO means? sorry i really sucks in C. Actually what i'm trying to do is after send the command to the device, i need to wait between 1-5 sec because the device will output data at random time between 1-5sec,then only i read the data. How can that be done? any idea?

i got this error when compiling the code in the first link u gave me for Asynchronous Input:
osp.c: In function ‘main’:
osp.c:28:42: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
osp.c:32:22: error: incompatible types when assigning to type ‘__sigset_t’ from type ‘int’
p/s: i did change the baudrate and tty.

Last edited by Marvin_G; 04-10-2013 at 02:05 AM.
 
Old 01-27-2014, 05:38 AM   #11
anuarls
LQ Newbie
 
Registered: Jan 2014
Posts: 1

Rep: Reputation: Disabled
correction in the code

// gcc -g rf.c -lm -o rf
// tested compile in Debian 6. But i don't try yet to run using my raspberrypi with rf tesbed.
// I have setup testbed for passive & active adversaries to attack my attestation protocol
// using cryptography primitives.

#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <stdlib.h> // exit()

#define BAUDRATE B38400
#define MODEMDEVICE "/dev/ttyS1"
#define _POSIX_SOURCE 1 /* POSIX compliant source */
#define FALSE 0
#define TRUE 1

volatile int STOP=FALSE;

void signal_handler_IO (int status); /* definition of signal handler */
int wait_flag=TRUE; /* TRUE while no signal received */

main()
{
int fd,c, res;
struct termios oldtio,newtio;
struct sigaction saio; /* definition of signal action */
char buf[255];

/* open the device to be non-blocking (read will return immediatly) */
fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd <0)
{ perror(MODEMDEVICE);
exit(1);
}

/* install the signal handler before making the device asynchronous */
saio.sa_handler = signal_handler_IO;
//saio.sa_mask = 0; // must not set to 0 but use sigemptyset() & sigaddset()
sigemptyset(&saio.sa_mask);
sigaddset(&saio.sa_mask, SIGINT);
saio.sa_flags = 0;
saio.sa_restorer = NULL;
sigaction(SIGIO,&saio,NULL);

/* allow the process to receive SIGIO */
fcntl(fd, F_SETOWN, getpid());
/* Make the file descriptor asynchronous (the manual page says only
O_APPEND and O_NONBLOCK, will work with F_SETFL...) */
fcntl(fd, F_SETFL, FASYNC);

tcgetattr(fd,&oldtio); /* save current port settings */
/* set new port settings for canonical input processing */
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR | ICRNL;
newtio.c_oflag = 0;
newtio.c_lflag = ICANON;
newtio.c_cc[VMIN]=1;
newtio.c_cc[VTIME]=0;
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);

/* loop while waiting for input. normally we would do something
useful here */
while (STOP==FALSE) {
printf(".\n");usleep(100000);
/* after receiving SIGIO, wait_flag = FALSE, input is available
and can be read */
if (wait_flag==FALSE) {
res = read(fd,buf,255);
buf[res]=0;
printf(":%s:%d\n", buf, res);
if (res==1) STOP=TRUE; /* stop loop if only a CR was input */
wait_flag = TRUE; /* wait for new input */
}
}

/* restore old port settings */
tcsetattr(fd,TCSANOW,&oldtio);
}

/***************************************************************************
* signal handler. sets wait_flag to FALSE, to indicate above loop that *
* characters have been received. *
***************************************************************************/

void signal_handler_IO (int status)
{
printf("received SIGIO signal.\n");
wait_flag = FALSE;
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Where does uart console get its value of uart port membase? unifoxz Linux - Kernel 1 12-20-2011 03:28 PM
fingerprint match against collection of previos fingerprint mukesh.methaniya Linux - Software 0 05-22-2011 03:31 AM
LPC47M10x UART Niraj Kulkarni Linux - Hardware 0 02-07-2011 05:08 AM
LPC47M10x for UART Niraj Kulkarni Linux - Newbie 0 02-02-2011 04:12 AM
uart pantera Programming 5 05-21-2005 07:50 PM


All times are GMT -5. The time now is 09:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration