// code by SASANO Takayoshi, CC BY-SA #include #include #include #include #include #include #include static void tx_test(int fd) { uint8_t buf; while (1) { buf = rand(); write(fd, &buf, sizeof(buf)); } /* NOTREACHED */ } static int setup_serial(int fd) { struct termios t; memset(&t, 0, sizeof(t)); cfsetospeed(&t, 9600); cfsetispeed(&t, 9600); t.c_cflag = CREAD | CLOCAL | CS8; t.c_iflag = INPCK; t.c_oflag = 0; t.c_lflag = 0; t.c_cc[VTIME] = 0; t.c_cc[VMIN] = 1; tcflush(fd, TCIFLUSH); if (tcsetattr(fd, TCSANOW, &t) < 0) goto fail; return 0; fail: return -1; } int main(int argc, char *argv[]) { int fd; if (argc < 2) { printf("%s [device]\n", argv[0]); goto fin0; } fd = open(argv[1], O_RDWR | O_NOCTTY | O_NDELAY); if (fd < 0) { printf("device open error\n"); goto fin0; } if (setup_serial(fd) < 0) { printf("serial parameter error\n"); goto fin1; } tx_test(fd); fin1: close(fd); fin0: return 0; }