LQ Newbie
Registered: Oct 2003
Location: london
Distribution: RH8.0
Posts: 6
Original Poster
Rep:
|
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);
}
|