]> sigrok.org Git - libserialport.git/blobdiff - examples/list_ports.c
Add examples directory with two example programs.
[libserialport.git] / examples / list_ports.c
diff --git a/examples/list_ports.c b/examples/list_ports.c
new file mode 100644 (file)
index 0000000..b8ff863
--- /dev/null
@@ -0,0 +1,50 @@
+#include <libserialport.h>
+#include <stdio.h>
+
+/* Example of how to get a list of serial ports on the system. */
+
+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);
+
+       if (result != SP_OK)
+       {
+               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;
+       for (i = 0; port_list[i] != NULL; i++)
+       {
+               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;
+}