]> sigrok.org Git - libserialport.git/blame - examples/list_ports.c
change type of result variables to ssize_t
[libserialport.git] / examples / list_ports.c
CommitLineData
8c1a14e6
ML
1#include <libserialport.h>
2#include <stdio.h>
3
ee12a01e
ML
4/* Example of how to get a list of serial ports on the system.
5 *
6 * This example file is released to the public domain. */
8c1a14e6
ML
7
8int main(int argc, char **argv)
9{
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;
13
14 printf("Getting port list.\n");
15
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);
19
78c3db9b 20 if (result != SP_OK) {
8c1a14e6
ML
21 printf("sp_list_ports() failed!\n");
22 return -1;
23 }
24
25 /* Iterate through the ports. When port_list[i] is NULL
26 * this indicates the end of the list. */
27 int i;
78c3db9b 28 for (i = 0; port_list[i] != NULL; i++) {
8c1a14e6
ML
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}