/* Silly program used to verify a tty is in fact connected to a solidoodle. * Change B115200 to different baud rate define if your firmware is set * to operate at a different rate. * * typical usage: send-this /dev/ttyACM0 M115 * * That will send an M115 gcode command which should respond by printing the * info about the firmware and wot-not. If it doesn't maybe the device you * just plugged in isn't really a solidoodle... */ #include #include #include #include #include #include #include #include #include #include #include int main(int argc, char ** argv) { struct termios settings; int fd; fd_set rset; struct timeval tmo; int selstat; char buf[1024]; int i; /* Make sure there is at least a device name and one word to send */ if (argc < 2) { fprintf(stderr, "usage: send-this tty string...\n"); exit(2); } /* Make sure we can open the device in nonblocking mode */ fd = open(argv[1], O_RDWR|O_NONBLOCK); if (fd == -1) { i = errno; fprintf(stderr, "Unable to open %s : %s (%d)\n", argv[1], strerror(i), i); exit(2); } /* Set it to "raw" mode */ memset((void *)&settings, 0, sizeof(settings)); cfmakeraw(&settings); cfsetospeed(&settings, B115200); cfsetispeed(&settings, B115200); if (tcsetattr(fd, TCSANOW, &settings) == -1) { i = errno; fprintf(stderr, "Unable to set %s to raw mode : %s (%d)\n", argv[1], strerror(i), i); exit(2); } /* If there is any gibberish buffered up, discard it */ for ( ; ; ) { FD_ZERO(&rset); FD_SET(fd, &rset); tmo.tv_sec = 0; tmo.tv_usec = 0; selstat = select(fd+1, &rset, NULL, NULL, &tmo); if (selstat <= 0) { break; } read(fd, buf, sizeof(buf)); } /* Now send space separated words from argument list followed by newline */ for (i = 2; i < argc; ++i) { write(fd, " ", 1); write(fd, argv[i], strlen(argv[i])); } write(fd, "\r", 1); /* Echo everything returned till we get no data for 1/10 of a second */ for ( ; ; ) { FD_ZERO(&rset); FD_SET(fd, &rset); tmo.tv_sec = 0; tmo.tv_usec = 100000; selstat = select(fd+1, &rset, NULL, NULL, &tmo); if (selstat <= 0) { break; } i = read(fd, buf, sizeof(buf)); write(fileno(stdout), buf, i); } return 0; }