LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-30-2003, 09:49 AM   #1
linux4john
LQ Newbie
 
Registered: Oct 2003
Location: london
Distribution: RH8.0
Posts: 6

Rep: Reputation: 0
serial port problem...


hi
i have a redhat 8.0 linux box and i want to use a finger print scanner which has an rs232 interface and a set of serial commands.

i need to send 20 bytes to the scanner and it will respond with 24 bytes- this an example.like this i can execute the complete set of command set for operation.

the scanner works fine under dos for all commands thru the rs232 port-com1.but i need the same stuff to be done using linux.

i have got the linux serial programming - howto. it says that there are 4 input processing mode. i tried with non-canonical input processing. the command bytes sent are not acknowledged and i feel that i'm not doing the communication in a proper manner.


my need is to send set of hex bytes to the device,which will respond immediately and collect the respose bytes and process them.

can anyone help me.

i'm stuck with withis for 2 days and just now found from my friend that LQ forum is the right place for a newbie like me.

thanks in advance
john
 
Old 10-30-2003, 11:02 PM   #2
linux4john
LQ Newbie
 
Registered: Oct 2003
Location: london
Distribution: RH8.0
Posts: 6

Original Poster
Rep: Reputation: 0
i have posted the code trying to use in linux followed by the code i used under DOS.

i have a redhat 8.0 linux box and i want to use a finger print scanner which has an rs232 interface and a set of serial commands.

i need to send 20 bytes to the scanner and it will respond with 24 bytes- this an example.like this i can execute the complete set of command set for operation.

the scanner works fine under dos for all commands thru the rs232 port-com1.but i need the same stuff to be done using linux.

awaiting ur help.

/*code to interact with a finger print scanner - linux */

#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 */

#define FALSE 0
#define TRUE 1

volatile int STOP=FALSE;

main()
{
int fd,res;
struct termios options,oldtio;

int i ;
//is finger present?
unsigned char data[25] = {0x40,0x28,0x6b,0xfe,0xff,0xff,0x01,0x00,0x34,0x34,0x04,0x00,0x00,0x00,0x00,0x00};
unsigned char buf[255];
unsigned long temp,chksum;

i=15;
chksum=0;
while(i>=4)
{
chksum+=data[i];
i--;
}
chksum=~chksum;
chksum++;
printf("chksum %x\n",chksum);
temp=chksum;
data[16]=(unsigned char) (temp & 0xff);
data[17]=(unsigned char) ((temp >> 8) & 0xff);
data[18]=(unsigned char) ((temp >> 16) & 0xff);
data[19]=(unsigned char) ((temp >> 24) & 0xff);

for(i=0;i<=19;i++)
printf(" %x ",data[i]);
printf("\n");


fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NONBLOCK);
fcntl(fd, F_SETFL, 0);

/* save current port settings */
tcgetattr(fd,&oldtio);

/* get the current options and change them*/
tcgetattr(fd, &options);

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

/* set raw input, 1 second timeout */
options.c_cflag |= (CLOCAL | CREAD);
options.c_oflag &= ~OPOST;
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = 10;

options.c_cflag &= ~PARENB; /* Clear parity enable */
options.c_iflag &= ~INPCK; /* Enable parity checking */
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;

//options.c_cflag &= ~CRTSCTS;

options.c_iflag &= ~(IXON | IXOFF | IXANY); /* no flow control */

options.c_oflag &= ~(IXON | IXOFF | IXANY); /* no flow control */

options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_oflag &= ~OPOST; /* No output processing */
options.c_oflag &= ~ONLCR; /* Don't convert linefeeds */

/* Miscellaneous stuff */
options.c_cflag |= (CLOCAL | CREAD); /* Enable receiver, set local */
/* Linux seems to have problem with the following ??!! */
options.c_cflag |= (IXON | IXOFF); /* Software flow control */
options.c_lflag = 0; /* no local flags */
options.c_cflag |= HUPCL; /* Drop DTR on close */

/* Setup non blocking, return on character */
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = 20;

/* Clear line */
tcflush(fd,TCIFLUSH);

/* Update the options */
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
perror("SetupSerial 3");
return(0);
}

write(fd, data, 20 ) ;

while (STOP==FALSE) /* loop for input */
{
res = read(fd,buf,255); /* returns after 20 chars -input */
buf[res]=0;
printf(":%s:%d\n", buf, res);
if (buf[0]=='z')
STOP=TRUE;
}

tcsetattr(fd,TCSANOW,&oldtio);
close(fd);
return 0 ;
}
-------------------------------------------------------------------------------
/*code to interact with a finger print scanner used in DOS */
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <process.h>
#include <conio.h>
#include <math.h>
#include <string.h>

#define PORT1 0x3F8
#define LSR (PORT1+5)
/* Defines Serial Ports Base Address */
/* COM1 0x3F8 */
/* COM2 0x2F8 */
/* COM3 0x3E8 */
/* COM4 0x2E8 */

void intialize_port()
{
outportb(PORT1 + 1 , 0); /* Turn off interrupts - Port1 */
/* PORT 1 - Communication Settings */

outportb(PORT1 + 3 , 0x80); /* SET DLAB ON */
outportb(PORT1 + 0 , 0x0C); /* Set Baud rate - Divisor Latch Low Byte */
/* Default 0x03 = 38,400 BPS */
/* 0x01 = 115,200 BPS */
/* 0x02 = 57,600 BPS */
/* 0x06 = 19,200 BPS */
/* 0x0C = 9,600 BPS */
/* 0x18 = 4,800 BPS */
/* 0x30 = 2,400 BPS */
/* outportb(PORT1 + 1 , 0x00); /* Set Baud rate - Divisor Latch High Byte */
outportb(PORT1 + 3 , 0x03); /* 8 Bits, No Parity, 1 Stop Bit */
outportb(PORT1 + 2 , 0xC7); /* FIFO Control Register*/
/* outportb(PORT1 + 2 , 0xC6); /* FIFO Control Register */
/* outportb(PORT1 + 4 , 0x0B); /* Turn on DTR, RTS, and OUT2 */
}

main()
{
/*is finger present? - command*/
unsigned char data[25]={0x40,0x28,0x6b,0xfe,0xff,0xff,0x01,0x00,0x34,0x34,0x04,0x00,0x00,0x00,0x00,0x00};

unsigned char outdata[600];
unsigned long temp,chksum;
int i;

clrscr();
intialize_port();
i=15;
chksum=0;
while(i>=4)
{
chksum+=data[i];
i--;
}
chksum=~chksum;
chksum++;
printf("chksum %x\n",chksum);
temp=chksum;
data[16]=(unsigned char) (temp & 0xff);
data[17]=(unsigned char) ((temp >> 8) & 0xff);
data[18]=(unsigned char) ((temp >> 16) & 0xff);
data[19]=(unsigned char) ((temp >> 24) & 0xff);

for(i=0;i<=19;i++)
printf(" %x ",data[i]);
printf("\n");

/*loop for sending command */
printf("data sent :\n");
for(i=0;i<=19;i++)
{
outportb(PORT1,data[i]);
printf("%x ",data[i]);
while(!(inportb(LSR) & 0x20));
}

i=0;
/*loop for receiving response*/
printf("\nrecd data :\n");
while(i<48)
{
while(!(inportb(LSR) & 0x01));
outdata[i] = inportb(PORT1);
printf("%x ",outdata[i]);
i++;
}
printf("\ncomplete\n");
while(1);
}
 
  


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
Serial port problem floydes Programming 1 10-14-2004 12:50 PM
C++ serial port program problem doar Programming 3 10-01-2004 11:46 AM
problem in serial port communication linux4john Linux - Newbie 3 10-30-2003 10:26 PM
Serial port problem ruchika Linux - Networking 0 08-15-2003 05:18 PM
Problem with Serial port mritunjay Linux - Networking 1 04-22-2002 02:59 PM


All times are GMT -5. The time now is 08:35 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration