Re: regarding serial ports
From: emb in linux (haranath.t_at_gmail.com)
Date: 11/16/05
- Previous message: Dirk Craeynest: "C.f.Industrial Pres., Reliable Software Technologies, Ada-Europe 2006"
- In reply to: Rune Christensen: "Re: regarding serial ports"
- Next in thread: Rune Christensen: "Re: regarding serial ports"
- Reply: Rune Christensen: "Re: regarding serial ports"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 15 Nov 2005 20:37:15 -0800
hai
if input is j,k,l
then output: j ΃Žÿw˜öÿ¿±„ À@0²‚Ôõÿ¿˜öÿ¿±„
À@0²˜I@øS@I@Ôõÿ¿@öÿ¿HH@΃
k
ƒŽÿw@Îöÿ¿±„ À@0²˜‚Ôõÿ¿Îöÿ¿±„
À@0²˜I@S@˜I@õÿ¿@öÿ¿HH@Îöÿ¿±„ À@0²ƒ
l
if if check for read==0 also i didn't got any difference.
with regards
emb in linux.
Rune Christensen wrote:
> emb in linux wrote:
> > hai all,
> > Here i want to communicate with two serial ports interactivelly.(i.e)If
> > i send a byte to com1 i have to receive in com2 correpondingly.Here I
> > am getting the subsequent bytes but in between i am getting some junk
> > charecters too.(For that i have connected a null modem cable in
> > between com1 and com2.)
> >
> > For clear Understanding i am giving my transmitting and receving code.
> >
> > // Transmitting code
> >
> > #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 */
> >
> > int
> > main()
> > {
> > int fd,c,i;
> > struct termios options;
> > char c, buff[10];
> > fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY );
> > if (fd == -1)
> > perror("Unable to open /dev/ttyS0\n");
> > else
> > {
> > fprintf(stdout,"enter any byte");
> > fgets(buff,sizeof(buff),stdin);
> > fcntl(fd, F_SETFL, 0);
> > tcgetattr(fd, &options);
> > cfsetispeed(&options,B115200);
> > cfsetospeed(&options,B115200);
> > options.c_cflag |= (CLOCAL | CREAD);
> > options.c_cflag &= ~PARENB;
> > options.c_cflag &= ~CSTOPB;
> > options.c_cflag &= ~CSIZE;
> > options.c_cflag |= CS8;
> > options.c_cflag |=(ICANON | ECHO | ECHOE);
> > options.c_oflag |=OPOST;
> > tcsetattr(fd, TCSANOW, &options);
> > while((c=fgetc(stdin)!=EOF)
> > {
> > buff[i]=c;
> > if(write(fd,buff,1)<0)
> > fprintf(stderr,"Write() failed!\n");
> > i++;
> > fputc(buff,stdout);
> > }
> > close (fd);
> > }
> > return (0);
> > }
> >
> > output:
> > enter any byte:a
> > enter any byte:b
> > enter any byte:c
> > it will close whenever we give ctrl+d
> >
> > //Receiving
> >
> >
> > #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 */
> >
> > int main()
> > {
> > int fd,i=0;
> > char buff[10];
> > struct termios options;
> > fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY );
> > if (fd == -1)
> > perror("Unable to open /dev/ttyS1\n");
> > else
> > {
> > fcntl(fd, F_SETFL, 0);
> > tcgetattr(fd, &options);
> > cfsetispeed(&options,B115200);
> > cfsetospeed(&options,B115200);
> > options.c_cflag |= (CLOCAL | CREAD);
> > options.c_cflag &= ~PARENB;
> > options.c_cflag &= ~CSTOPB;
> > options.c_cflag &= ~CSIZE;
> > options.c_cflag |= CS8;
> > options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
> > options.c_oflag &= ~OPOST;
> > options.c_cc[VMIN]=1;
> > options.c_cc[VTIME]=10;
> > tcsetattr(fd, TCSANOW, &options);
> > fprintf(stdout,"In server\n");
> > while(1)
> > {
> > if (read(fd,buff,10)<0)
> > fprintf(stderr,"Read() failed!\n");
> > fputs(buff,stdout);
>
> Your problem could be that you don't test if read(..) == 0 and nothing
> has been read.
>
> > }
> > close (fd);
> > }
> > return (0);
> > }
> >
> > output:a
> > [JUNK ----------------------->]b
> > [JUNK------------------------->]c
>
> Could you show me the junk?
>
> > here i want to clear the buffer after receiving 1st byte.so that i can
> > put that function in loop.Is there any function to do that?
> > Hope i explaned clearly.If you have any guide other than posix
> > standard,please give me the url.
> > Thanks in advance.
> >
> >
> > With Regards
> >
> > emb in linux.
> >
>
>
> Rune Christensen
> Engineer
- Previous message: Dirk Craeynest: "C.f.Industrial Pres., Reliable Software Technologies, Ada-Europe 2006"
- In reply to: Rune Christensen: "Re: regarding serial ports"
- Next in thread: Rune Christensen: "Re: regarding serial ports"
- Reply: Rune Christensen: "Re: regarding serial ports"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|