Show available ports

This commit is contained in:
Alexander Popov 2023-07-29 23:45:51 +03:00
parent c2e628b33e
commit 1916075fee
Signed by: iiiypuk
GPG Key ID: E47FE0AB36CD5ED6

View File

@ -8,7 +8,7 @@ int check(enum sp_return result);
int main(int argc, char **argv) {
struct sp_port *serial_port;
char *port_name = "/dev/ttyACM0";
char *port_name;
const int size = 256;
char *buffer = malloc(size + 1);
@ -16,8 +16,33 @@ int main(int argc, char **argv) {
const unsigned int timeout = 1000;
int result;
/* Get the port name from the command line. */
if (argc != 2) {
printf("Usage: %s <port>\n", argv[0]);
printf("Usage: %s <port>\n\n", argv[0]);
struct sp_port **port_list;
enum sp_return result = sp_list_ports(&port_list);
/* Getting the available ports. */
if (result != SP_OK) {
puts("Getting available ports failed!");
} else {
puts("Available ports:");
int i;
for (i = 0; port_list[i] != NULL; i++) {
/* Get the name of the port. */
struct sp_port *port = port_list[i];
char *port_name = sp_get_port_name(port);
printf(" * %s\n", port_name);
}
printf("\nAvailable %d ports.\n", i);
sp_free_port_list(port_list);
}
return -1;
} else {
port_name = argv[1];
@ -34,18 +59,24 @@ int main(int argc, char **argv) {
check(sp_set_flowcontrol(serial_port, SP_FLOWCONTROL_NONE));
puts("Connected.");
while (true) {
/* Reading lines from serial port. */
bool reading = true;
while (reading) {
int pos = 0;
/* Character-by-character reading. */
while (pos < size) {
result = check(sp_blocking_read(serial_port, buffer + pos, 1, timeout));
if (result == -1) {
puts("Error reading from serial port");
reading = false;
break;
} else if (result == 0) {
puts("No more data");
reading = false;
break;
} else {
if (buffer[pos] == '\n') {