2020-01-05 05:04:06 +03:00
|
|
|
#include <libserialport.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2020-01-05 17:53:09 +03:00
|
|
|
/* Example of how to get a list of serial ports on the system.
|
|
|
|
*
|
|
|
|
* This example file is released to the public domain. */
|
2020-01-05 05:04:06 +03:00
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
/* A pointer to a null-terminated array of pointers to
|
|
|
|
* struct sp_port, which will contain the ports found.*/
|
|
|
|
struct sp_port **port_list;
|
|
|
|
|
|
|
|
printf("Getting port list.\n");
|
|
|
|
|
|
|
|
/* Call sp_list_ports() to get the ports. The port_list
|
|
|
|
* pointer will be updated to refer to the array created. */
|
|
|
|
enum sp_return result = sp_list_ports(&port_list);
|
|
|
|
|
2020-01-26 23:18:49 +03:00
|
|
|
if (result != SP_OK) {
|
2020-01-05 05:04:06 +03:00
|
|
|
printf("sp_list_ports() failed!\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Iterate through the ports. When port_list[i] is NULL
|
|
|
|
* this indicates the end of the list. */
|
|
|
|
int i;
|
2020-01-26 23:18:49 +03:00
|
|
|
for (i = 0; port_list[i] != NULL; i++) {
|
2020-01-05 05:04:06 +03:00
|
|
|
struct sp_port *port = port_list[i];
|
|
|
|
|
|
|
|
/* Get the name of the port. */
|
|
|
|
char *port_name = sp_get_port_name(port);
|
|
|
|
|
|
|
|
printf("Found port: %s\n", port_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Found %d ports.\n", i);
|
|
|
|
|
|
|
|
printf("Freeing port list.\n");
|
|
|
|
|
|
|
|
/* Free the array created by sp_list_ports(). */
|
|
|
|
sp_free_port_list(port_list);
|
|
|
|
|
|
|
|
/* Note that this will also free all the sp_port structures
|
|
|
|
* it points to. If you want to keep one of them (e.g. to
|
|
|
|
* use that port in the rest of your program), take a copy
|
|
|
|
* of it first using sp_copy_port(). */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|