LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


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

Rep: Reputation: 0
problem in serial port communication


hi

sorry i have posted this message in software forum by mistake and found that this wud be the right place

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, 02:45 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,569

Rep: Reputation: 171Reputation: 171
Please paste the code you're using. Someone here will browse it and tell you what needs to be changed. I understand it's more a programming question, right?
 
Old 10-30-2003, 03:25 PM   #3
dorian33
Member
 
Registered: Jan 2003
Location: Poland, Warsaw
Distribution: LFS, Gentoo
Posts: 591

Rep: Reputation: 32
If you wish I can give you my own set of useful function for RS port setup but please give your mail address.
 
Old 10-30-2003, 09:26 PM   #4
linux4john
LQ Newbie
 
Registered: Oct 2003
Location: london
Distribution: RH8.0
Posts: 6

Original Poster
Rep: Reputation: 0
hi mora,
thanks for the reply.
i have pasted the code -
what i'm trying to do in linux followd by what i did in dos .

hi dorian
thanks for ur reply. my email id is linux4john@yahoo.com.

-------------------------------------------------------------------------------

/*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 communication prems Linux - Newbie 1 04-17-2005 02:31 AM
serial port communication vidyaraj Linux - Software 2 03-14-2004 11:32 PM
communication with serial port vidyaraj Linux - Hardware 0 03-09-2004 08:32 PM
serial port communication vidyaraj Linux - Software 6 03-02-2004 06:46 PM
communication via serial port perdesiz Linux - Software 0 11-13-2003 06:23 AM


All times are GMT -5. The time now is 06:58 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration