]> sigrok.org Git - libserialport.git/blob - examples/list_ports.c
Add some additional formatting hints to Doxygen comments.
[libserialport.git] / examples / list_ports.c
1 #include <libserialport.h>
2 #include <stdio.h>
3
4 /* Example of how to get a list of serial ports on the system. */
5
6 int main(int argc, char **argv)
7 {
8         /* A pointer to a null-terminated array of pointers to
9          * struct sp_port, which will contain the ports found.*/
10         struct sp_port **port_list;
11
12         printf("Getting port list.\n");
13
14         /* Call sp_list_ports() to get the ports. The port_list
15          * pointer will be updated to refer to the array created. */
16         enum sp_return result = sp_list_ports(&port_list);
17
18         if (result != SP_OK)
19         {
20                 printf("sp_list_ports() failed!\n");
21                 return -1;
22         }
23
24         /* Iterate through the ports. When port_list[i] is NULL
25          * this indicates the end of the list. */
26         int i;
27         for (i = 0; port_list[i] != NULL; i++)
28         {
29                 struct sp_port *port = port_list[i];
30
31                 /* Get the name of the port. */
32                 char *port_name = sp_get_port_name(port);
33
34                 printf("Found port: %s\n", port_name);
35         }
36
37         printf("Found %d ports.\n", i);
38
39         printf("Freeing port list.\n");
40
41         /* Free the array created by sp_list_ports(). */
42         sp_free_port_list(port_list);
43
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(). */
48
49         return 0;
50 }