Hi I have written the followinng serial port program. It compiles without errors. During execution the program does not recieve all the characters that I send from another computer thru minicom. Most of the characters are lost only a few are recieved. The baud rates on both sides are same. Is there something missing in the code ? Please help.
Code:
#include <termios.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <sys/select.h>
#include <unistd.h>
#include <string.h>
#define DEVICE "/dev/ttyS0"
int main(void)
{
int fd;
struct termios oldtio, newtio;
//open the serial communication device.
fd = open(DEVICE, O_RDWR | O_NOCTTY | O_SYNC | O_NONBLOCK);
if(fd < 0) {
printf("Failed to open device.\n");
return -1;
}
memset(&newtio, 0, sizeof(newtio));
/* Store the old configuration. This will be restored when we close the library. */
tcgetattr(fd, &oldtio);
newtio.c_cflag = B115200 /* Baudrate specified by the user */ \
| CS8 /* character size mask */ \
| CLOCAL /* Ignore modem control lines */ \
| CREAD /* Enable reciever */ \
| CSTOPB;
printf("ISIG : %x\n", ISIG);
printf("ICANON : %x\n", ICANON);
newtio.c_iflag = IGNPAR | ICRNL ;
newtio.c_oflag = 0;
newtio.c_lflag = ISIG;
newtio.c_cc[VMIN]=1;
newtio.c_cc[VTIME]=0;
printf("newtio.c_lflag: %x\n", newtio.c_lflag);
/* Set the new configuration */
tcflush(fd, TCIFLUSH);
if(tcsetattr(fd, TCSANOW, &(newtio)) < 0 ) {
printf("Failed to set new configuration. Possibly an invalid configuration.\n");
close(fd);
return -2;
}
{
char a=0;
char b[11];
while(1) {
fd_set fds;
FD_ZERO(&fds);
FD_SET(fd, &fds);
select(fd+1, &fds, NULL, NULL, NULL);
printf("Select returned.\n");
if(read( fd, &(b[0]), sizeof(char) )< 0) {
// perror("reading error.");
continue;
}
// printf("%c",b[0]);
putchar(b[0]);
}
}
return 0;
}