need a sample code

From: emb in linux (haranath.t_at_gmail.com)
Date: 11/08/05


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.



Relevant Pages

  • Re: Help with 7 Circuit Project?
    ... false alarms, to the extent you mentioned, as the programming you will want ... holes in your security system. ... the great thing about HAI (and their sister brands Aegis and OnQ ... As you dive into the Omni, you will find out how easy and advanced it is. ...
    (comp.home.automation)
  • Re: One problem or two?
    ... this is my actual first post on this ng. ... i'm writing a rl myself (more on it on a later stage of developement). ... in differen programming lamguages in the last 15. ... basic operation on almost every language and you'll use a lot of it ...
    (rec.games.roguelike.development)
  • Re: Basic HAI Omni system information
    ... You will find a ton of information on HAI ... How do you define an alarm zone as exterior or interior, NC/NO or both, ... In the programming area, how would you program: If any exterior zone ...
    (comp.home.automation)
  • Re: Basic HAI Omni system information
    ... You will find a ton of information on HAI ... How do you define an alarm zone as exterior or interior, NC/NO or both, ... In the programming area, how would you program: If any exterior zone ...
    (comp.home.automation)
  • Re: How to generate a md5 hash?
    ... this is my first post here and I'm very very new in 'C' ... > programming. ... > And I began to 'translate' my PHP program, that beyond a lot of things ... > But I still dont know how to generate a md5 hash from my string ...
    (comp.lang.c)