#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 argc, char **argv){ int fd; struct termios options; char buffer; /* Input buffer */ int nbytes; /* Number of bytes read */ int tries; /* Number of tries so far */ fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY); if (fd == -1)perror("open_port: Unable to open /dev/ttyUSB0 - "); fcntl(fd, F_SETFL, 0); /* set read to block untill read is done */ fcntl(fd, F_SETFL, 0); /* get the current options */ tcgetattr(fd, &options); /* No parity (8N1): */ options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB; options.c_cflag &= ~CSIZE; options.c_cflag |= CS8; /* Disable HW flow control */ // options.c_cflag &= ~CNEW_RTSCTS; options.c_cflag &= ~CRTSCTS; /* Disable SW flow control */ options.c_iflag &= ~(IXON | IXOFF | IXANY); /* Choose RAW Input */ options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /* 9600 Baud both ways */ cfsetispeed(&options, B9600); cfsetospeed(&options, B9600); options.c_cc[VTIME] = 0; /* inter-character timer unused */ options.c_cc[VMIN] = 2; /* blocking read until 5 chars received */ /* set the options */ tcsetattr(fd, TCSANOW, &options); while((nbytes = read(fd,&buffer,1))>0) printf("%x \n",buffer); close(fd); return 0; }