regarding serial ports

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 /* Standard input/output definitions */ #include /* String function definitions */ #include /* UNIX standard function definitions */ #include /* File control definitions */ #include /* Error number definitions */ #include /* 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)]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.

Reply to
emb in linux
Loading thread data ...

Your problem could be that you don't test if read(..) == 0 and nothing has been read.

Could you show me the junk?

Rune Christensen Engineer

Reply to
Rune Christensen

The OP is also outputting a string, but doesn't appear to be null terminating it first.

Note to OP: Don't forget to actually reserve one byte in the string for a null. In other words, only read a maximum of (buffer_size - 1) bytes at a time.

Simon.

--
Simon Clubley, clubley@remove_me.eisner.decus.org-Earth.UFP       
Scientific Theory: A testable hypothesis that is supported by a body of evidence
Reply to
Simon Clubley

hai if input is j,k,l then output: j =CE=83=8E=FFw=98=F6=FF=BF=B1=84 =C0@0=B2=82=D4=F5=FF=BF= =98=F6=FF=BF=B1=84 =C0@0=B2=98I@=F8S@I@=D4=F5=FF=BF@=F6=FF=BFHH@=CE=83 k =83=8E=FFw@=CE=F6=FF=BF=B1=84 =C0@0=B2=98=82=D4=F5=FF=BF=CE=F6=FF=BF=B1= =84 =C0@0=B2=98I@S@=98I@=F5=FF=BF@=F6=FF=BFHH@=CE=F6=FF=BF=B1=84 =C0@0= =B2=83 l if if check for read=3D=3D0 also i didn't got any difference.

with regards

emb > > hai all,

Reply to
emb in linux

I have looked at your code one more time.

When reading and writing you need to use two compatible functions.

if (fgets(buff, 10, fd) == NULL) { fprintf(stderr,"Read() failed!\n"); abort(); } fputs(buff, stdout);

another version is:

int len;

if ((len = read(fd, buff, 10)) < 0) { fprintf(stderr,"Read() failed!\n"); abort(); } write(stdout, buff, len);

Try one of them and tell me the result.

Rune Christensen Engineer

Reply to
Rune Christensen

Here i have understood the idea,but the two versions which you has are giving warnings and not working too.

With Regards

emb in linux.

Reply to
emb in linux

Could you be more specific with the warnings. Copy paste them and send them in a reply to this message.

What kind of output do you get now?

The transmitting part of your code you use both fgets(buff, sizeof(buff), stdin) and later while((c = fgetc(stdin)) != EOF) why that?

Rune Christensen Engineer

Reply to
Rune Christensen

"i" is uninitialized and buff can only hold 10 characters. Plus in your fputc you are sending it a pointer as the 1st parameter instead of a int. You are lucky if this code even compiles or runs without crashing. If you can call that luck.

Reply to
FLY135

Hai all, its long time since i have mailed to this group(due to viral feaver).i got my serial port programming some what interactivelly.Here i am giving my send and receive codes to help others who may want to start work on serial ports.

/*sending program:*/

#include #include #include #include #include #include int main() { int fd,i,a,n=0; struct termios options; char buff[1]; fd=open("/dev/ttyS0",O_RDWR | O_NOCTTY); if(fd==-1) perror("Unable to open /dev/ttyS0\n"); else { while(1) { 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; options.c_cc[VMIN]=1; options.c_cc[VTIME]=1; tcflush(fd,TCIFLUSH); tcsetattr(fd,TCSANOW,&options); i=fgetc(stdin); buff[0]=i; if(write(fd,buff,1)

Reply to
emb in linux

Reply to
emb in linux

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.