port from argv

This commit is contained in:
Alexander Popov 2023-07-29 23:15:23 +03:00
parent 053986cf00
commit 55528a6363
Signed by: iiiypuk
GPG Key ID: E47FE0AB36CD5ED6
1 changed files with 20 additions and 8 deletions

View File

@ -1,19 +1,29 @@
#include <libserialport.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
/* 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 <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_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);
}
}