regarding serial ports
From: emb in linux (haranath.t_at_gmail.com)
Date: 11/15/05
- Next message: Grant Edwards: "Re: JPEG Viewer"
- Previous message: style: "Re: JPEG Viewer"
- Next in thread: Rune Christensen: "Re: regarding serial ports"
- Reply: Rune Christensen: "Re: regarding serial ports"
- Reply: FLY135: "Re: regarding serial ports"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 15 Nov 2005 05:24:50 -0800
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);
}
close (fd);
}
return (0);
}
output:a
[JUNK ----------------------->]b
[JUNK------------------------->]c
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.
- Next message: Grant Edwards: "Re: JPEG Viewer"
- Previous message: style: "Re: JPEG Viewer"
- Next in thread: Rune Christensen: "Re: regarding serial ports"
- Reply: Rune Christensen: "Re: regarding serial ports"
- Reply: FLY135: "Re: regarding serial ports"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|