1 #include <libserialport.h>
4 /* Example of how to get a list of serial ports on the system.
6 * This example file is released to the public domain. */
8 int main(int argc, char **argv)
10 /* A pointer to a null-terminated array of pointers to
11 * struct sp_port, which will contain the ports found.*/
12 struct sp_port **port_list;
14 printf("Getting port list.\n");
16 /* Call sp_list_ports() to get the ports. The port_list
17 * pointer will be updated to refer to the array created. */
18 enum sp_return result = sp_list_ports(&port_list);
20 if (result != SP_OK) {
21 printf("sp_list_ports() failed!\n");
25 /* Iterate through the ports. When port_list[i] is NULL
26 * this indicates the end of the list. */
28 for (i = 0; port_list[i] != NULL; i++) {
29 struct sp_port *port = port_list[i];
31 /* Get the name of the port. */
32 char *port_name = sp_get_port_name(port);
34 printf("Found port: %s\n", port_name);
37 printf("Found %d ports.\n", i);
39 printf("Freeing port list.\n");
41 /* Free the array created by sp_list_ports(). */
42 sp_free_port_list(port_list);
44 /* Note that this will also free all the sp_port structures
45 * it points to. If you want to keep one of them (e.g. to
46 * use that port in the rest of your program), take a copy
47 * of it first using sp_copy_port(). */