c - Read return garbage data in serial port programming in linux -


i want communicate pc using rs232 port. can open "/dev/ttys0" , write data using write() function, can't read correct data "dev/ttys0" using read(). read() function read unnecessary data.please tell me how solve problem?

my program code here :

#include <stdio.h> #include <string.h> #include <fcntl.h> #include <termios.h> #include <unistd.h>  int main() {     int n = 0, fd = 0, bytes = 0;      char buffer[10];      struct termios term;      fd = open("/dev/ttys0", o_rdwr | o_noctty | o_ndelay);      if (fd == -1)     {         perror("open");         return;     }     else     {         fcntl(fd, f_setfl, 0);         perror("port");     }      tcgetattr(fd, &term);      cfsetispeed(&term, b115200);     cfsetospeed(&term, b115200);      term.c_cflag |= (clocal | cread);     term.c_cflag &= ~parenb;     term.c_cflag &= ~cstopb;     term.c_cflag &= ~csize;     term.c_cflag |= cs8;     term.c_cflag &= ~crtscts;     term.c_lflag &= ~(icanon | echo | echoe | isig);     term.c_iflag &= ~(ixon | ixoff | ixany);     term.c_oflag &= ~opost;       term.c_cc[vmin] = 0;     term.c_cc[vtime] = 10;       tcsetattr(fd, tcsanow, &term);       printf("enter string...\n");         scanf("%s", buffer);          write(fd, buffer, sizeof(buffer));         perror("write");  //  fcntl(fd, f_setfl, fndelay);          sleep(1);      bytes = read(fd, buffer, sizeof(buffer));     perror("read");      buffer[bytes] = '\0';     printf("bytes : %d\n", bytes);     printf("%s\n", buffer);     memset(buffer, '\0', 10);    } 

as answered here following code works well. did try change serial line? sure shortcutting pin 2 , 3 of serial db9 connector?

int main() {     int n = 0, fd = 0, bytes = 0;     char ch = 0;      char buffer[128], *bufptr;     int nbytes = 0, tries = 0, x = 0;      struct termios term;      fd = open("/dev/ttyusb0", o_rdwr | o_noctty | o_ndelay);      if (fd == -1)     {         perror("open");         return;     }     else     {         fcntl(fd, f_setfl, 0);         perror("port");     }      if (n = tcgetattr(fd, &term) == -1)     {         perror("tcgetattr");         return;     }      if (n = cfsetispeed(&term, b115200) == -1)     {         perror("cfsetispeed");         return;     }      if (n = cfsetospeed(&term, b115200) == -1)     {         perror("cfsetospeed");         return;     }      term.c_cflag |= (clocal | cread);     term.c_cflag &= ~parenb;     term.c_cflag &= ~cstopb;     term.c_cflag &= ~csize;     term.c_cflag |= cs8;     term.c_lflag &= ~(icanon | echo | echoe | isig);     term.c_iflag &= ~(ixon | ixoff | ixany);     term.c_cflag &= ~crtscts;     term.c_oflag &= ~opost;      if (n = tcsetattr(fd, tcsanow, &term) == -1)     {         perror("tcsetattr");         return;     }      char stringtosend[128];      printf("enter string...\n");     scanf("%s", stringtosend);      size_t len = strlen(stringtosend) +1 ;      write(fd,stringtosend, len);     perror("write");      size_t receivedbytes = 0;     bytes = 0;     memset(buffer, 0x00, sizeof(buffer));     while (receivedbytes<len)     {        bytes = read(fd, &buffer[receivedbytes], sizeof(buffer)-1);        perror("read");         if (bytes > 0)            receivedbytes += bytes;     }      printf("bytes : %d , data: %s\n", receivedbytes, buffer); } 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -