need a sample code
From: emb in linux (haranath.t_at_gmail.com)
Date: 11/08/05
- Next message: Alain Mosnier: "Re: need a sample code"
- Previous message: pweichel: "How can i detect presence of USB keyboard from user space program ?"
- Next in thread: Alain Mosnier: "Re: need a sample code"
- Reply: Alain Mosnier: "Re: need a sample code"
- Reply: Simon Clubley: "Re: need a sample code"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 8 Nov 2005 03:33:02 -0800
hai all
This is my first post to this group.I am very much new to embedded
programming .Now i want to write data to com1 and want to read from
com2 .Here i have connected a NULL-MODEM cable in between the two
serial ports.
The following is the code,which i am running on my Linux Box.
I am getting the return value from read as -1.
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
main()
{
int fd[2],n;
char buff[4];
fd[0]=open("/dev/ttyS0",O_RDWR | O_NOCTTY | O_NDELAY);
fd[1]=open("/dev/ttyS1",O_RDWR | O_NOCTTY | O_NDELAY);
if (fd[0] || fd[1] <= 0)
{
perror("open_port:Unable to open /dev/ttyS0 or /dev/ttyS1");
abrot();
}
/* Before writing data i am reading the buffer */
fcntl(fd[0],F_SETFL,FNDELAY);
n=read(fd[0],buff,4);
printf("%d",n); /*here i am getting the value of n as -1*/
fputs(buff,stdout);/*here i am getting some garbage values */
fcntl(fd[1],F_SETFL,FNDELAY);/*here i am setting the flags*/
n=read(fd[1],buff,4);
printf("%d",n); /*here i am getting the value of n as -1*/
fputs(buff,stdout);/*here i am getting garbage values */
/* Writing data to com1*/
fcntl(fd,F_SETFL,0);
n=write(fd,"hai\r",4);
printf("write n value is %d\n",n); /* here i am getting the value of
n as 4*/
if (n<=0)
{
fputs ("write() of 4 bytes to com1 is failed!\n",stderr);
abort();
}
/* Reading from the com2*/
fcntl(fd[1],F_SETFL,FNDELAY);
n=read(fd[1],buff,4);
printf("read n value is %s\n",buff);
if (n<=0)
fputs ("read() of 4 bytes from com2 is failed!\n",stderr);
fputs(buff,stdout);
/*Closing the com ports*/
close(fd[0]);
close(fd[1]);
}
the output of the code is :garbage values(for 1st fputs)
n=4
n=-1(printf in read)
read of 4 bytes from com2 is
failed
garbage values(for last fputs)
what might
Hope i explaned clearly.
Thanks in Advance.
With Regards
emb in linux.
- Next message: Alain Mosnier: "Re: need a sample code"
- Previous message: pweichel: "How can i detect presence of USB keyboard from user space program ?"
- Next in thread: Alain Mosnier: "Re: need a sample code"
- Reply: Alain Mosnier: "Re: need a sample code"
- Reply: Simon Clubley: "Re: need a sample code"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|