Writing and reading from a serial port
Hello, I am working on a project that will basically write some data to 8250 serial port (/dev/ttyS0) and then read back the same data. My understanding is if I write something, it will stored in a buffer and then if I read back, I should be able to read what I just wrote. However, I am having issues where I can write successfully to a serial port but when I check for # of bytes available in the input buffer, it returns 0. So basically, my write does not flush. Can you tell me where the problem..? Here is my code snippet. #include <termios.h> #include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <sys/signal.h> #include <sys/stat.h> #include <sys/types.h> #include <sys/time.h> #include <stdlib.h> #include <sys/ioctl.h> int main() { int fd, res, sen; int i; struct termios oldtio, newtio; char buf[] = "hello"; int bytes, total; fd = open("/dev/ttyS0",O_RDWR | O_NONBLOCK | O_SYNC); if (fd < 0) { perror("error"); return EXIT_FAILURE; } fcntl( fd, F_SETFL, O_NONBLOCK ); newtio.c_cflag = CLOCAL | CREAD; newtio.c_iflag = IGNPAR | ICRNL; newtio.c_oflag = 0; newtio.c_lflag = ICANON; newtio.c_cc[VMIN] = 0; newtio.c_cc[VTIME] = 10; tcflush(fd, TCIFLUSH); tcsetattr( fd, TCSANOW, &newtio ); sen = write( fd, buf, sizeof(buf) ); printf("No. of bytes written = %d\n", sen); tcsetattr(fd, TCSAFLUSH, &newtio); total =ioctl(fd, FIONREAD, &bytes); printf("No. of bytes in the input buffer = %d\n", total); return 0; } Any help will be highly appreciated. Thanks, Nick |
No comments:
Post a Comment