#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 open_port(void) { int fd; // File descriptor for the port fd = open("/dev/ttyS0", O_RDWR | O_NONBLOCK); if (fd == -1) { fprintf(stderr, "open_port: Unable to open /dev/ttyS0 %s\n",strerror(errno)); // exit(EXIT_FAILURE); } return (fd); } int main(void){ int fd = 0; // File descriptor struct termios options; // Terminal options char *a="Sushant ME"; //a = malloc(100); fd = open_port(); // Open tty device for RD and WR printf("fd =%d\n",fd); fcntl(fd, F_SETFL); // Configure port reading tcgetattr(fd, &options); // Get the current options for the port cfsetispeed(&options, B19200); // Set the baud rates to 19200 cfsetospeed(&options, B19200); printf("1:\n"); options.c_cflag |= (CLOCAL | CREAD); // Enable the receiver and set local mode options.c_cflag &= ~PARENB; // No parity bit options.c_cflag &= ~CSTOPB; // 1 stop bit options.c_cflag &= ~CSIZE; // Mask data size options.c_cflag |= CS8; // Select 8 data bits //options.c_cflag &= CRTSCTS; // Disable hardware flow control // options.c_lflag &= ~(ICANON | ECHO | ISIG); tcsetattr(fd, TCSANOW, &options); int rd,wr; while(1) { wr = write(fd,a,10); if(wr <0) { perror("Write:"); } memset(a,'\0',sizeof(a)); rd = read(fd,a,10); if(rd <0) { perror("Read:"); } printf("a= %s\n",a); } close(fd); return 0; }