diff --git a/~/C/libserialport/listen.c b/~/C/libserialport/listen.c index 69b9135..34090f5 100644 --- a/~/C/libserialport/listen.c +++ b/~/C/libserialport/listen.c @@ -1,19 +1,29 @@ #include #include #include -#include +#include /* Helper function for error handling. */ int check(enum sp_return result); -int main() { - int result; +int main(int argc, char **argv) { + struct sp_port *serial_port; + char *port_name = "/dev/ttyACM0"; + const int size = 256; char *buffer = malloc(size + 1); - const char *port_name = "/dev/ttyACM0"; - const unsigned int timeout = 1000; - struct sp_port *serial_port; + const unsigned int timeout = 1000; + int result; + + if (argc != 2) { + printf("Usage: %s \n", argv[0]); + return -1; + } else { + port_name = argv[1]; + } + + printf("Connecting to '%s'...\n", port_name); check(sp_get_port_by_name(port_name, &serial_port)); check(sp_open(serial_port, SP_MODE_READ_WRITE)); @@ -22,8 +32,10 @@ int main() { check(sp_set_parity(serial_port, SP_PARITY_NONE)); check(sp_set_stopbits(serial_port, 1)); check(sp_set_flowcontrol(serial_port, SP_FLOWCONTROL_NONE)); + puts("Connected.\n"); - while (1) { + + while (true) { int pos = 0; while (pos < size) { @@ -43,7 +55,7 @@ int main() { pos++; } - printf("%s\n", buffer); + puts(buffer); } }