port from argv
This commit is contained in:
@ -1,19 +1,29 @@
|
|||||||
#include <libserialport.h>
|
#include <libserialport.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
/* Helper function for error handling. */
|
/* Helper function for error handling. */
|
||||||
int check(enum sp_return result);
|
int check(enum sp_return result);
|
||||||
|
|
||||||
int main() {
|
int main(int argc, char **argv) {
|
||||||
int result;
|
struct sp_port *serial_port;
|
||||||
|
char *port_name = "/dev/ttyACM0";
|
||||||
|
|
||||||
const int size = 256;
|
const int size = 256;
|
||||||
char *buffer = malloc(size + 1);
|
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 <port>\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_get_port_by_name(port_name, &serial_port));
|
||||||
check(sp_open(serial_port, SP_MODE_READ_WRITE));
|
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_parity(serial_port, SP_PARITY_NONE));
|
||||||
check(sp_set_stopbits(serial_port, 1));
|
check(sp_set_stopbits(serial_port, 1));
|
||||||
check(sp_set_flowcontrol(serial_port, SP_FLOWCONTROL_NONE));
|
check(sp_set_flowcontrol(serial_port, SP_FLOWCONTROL_NONE));
|
||||||
|
puts("Connected.\n");
|
||||||
|
|
||||||
while (1) {
|
|
||||||
|
while (true) {
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
|
|
||||||
while (pos < size) {
|
while (pos < size) {
|
||||||
@ -43,7 +55,7 @@ int main() {
|
|||||||
pos++;
|
pos++;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("%s\n", buffer);
|
puts(buffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user