#include #include #include #include #include #include #include #include static int dev_init(int fd) { struct termios t; t.c_iflag = IGNBRK | IGNPAR | INPCK; t.c_oflag = 0; t.c_cflag = B57600 | CLOCAL | CREAD | CS8 | PARENB; t.c_lflag = 0; memset(t.c_cc, _POSIX_VDISABLE, NCCS); t.c_cc[VMIN] = 0; t.c_cc[VTIME] = 0; if (tcsetattr(fd, TCSANOW, &t) == -1) { perror("tcsetattr"); exit(2); } return 0; } static int dev_open(const char *name) { int fd = open(name, O_RDWR | O_NONBLOCK); if (fd == -1) { perror("open"); exit(2); } return fd; } static ssize_t dev_read(int fd, void *buf, size_t count) { ssize_t ret = read(fd, buf, count); if (ret == -1) { perror("read"); exit(2); } printf("%s: %d\n", __FUNCTION__, ret); return ret; } int main(void) { int fd = dev_open("/dev/ttyS0"); dev_init(fd); fd_set rfds; FD_ZERO(&rfds); FD_SET(fd, &rfds); if (select(fd + 1, &rfds, NULL, NULL, NULL) == -1) { perror("select"); exit(2); } char buf; dev_read(fd, &buf, 1); dev_read(fd, &buf, 1); return 0; }