]> sigrok.org Git - libserialport.git/blob - examples/port_info.c
Release examples as public domain.
[libserialport.git] / examples / port_info.c
1 #include <libserialport.h>
2 #include <stdio.h>
3
4 /* Example of how to get information about a serial port.
5  *
6  * This example file is released to the public domain. */
7
8 int main(int argc, char **argv)
9 {
10         /* Get the port name from the command line. */
11         if (argc != 2) {
12                 printf("Usage: %s <port name>\n", argv[0]);
13                 return -1;
14         }
15         char *port_name = argv[1];
16
17         /* A pointer to a struct sp_port, which will refer to
18          * the port found. */
19         struct sp_port *port;
20
21         printf("Looking for port %s.\n", port_name);
22
23         /* Call sp_get_port_by_name() to find the port. The port
24          * pointer will be updated to refer to the port found. */
25         enum sp_return result = sp_get_port_by_name(port_name, &port);
26
27         if (result != SP_OK)
28         {
29                 printf("sp_get_port_by_name() failed!\n");
30                 return -1;
31         }
32
33         /* Display some basic information about the port. */
34         printf("Port name: %s\n", sp_get_port_name(port));
35         printf("Description: %s\n", sp_get_port_description(port));
36
37         /* Identify the transport which this port is connected through,
38          * e.g. native port, USB or Bluetooth. */
39         enum sp_transport transport = sp_get_port_transport(port);
40
41         if (transport == SP_TRANSPORT_NATIVE)
42         {
43                 /* This is a "native" port, usually directly connected
44                  * to the system rather than some external interface. */
45                 printf("Type: Native\n");
46         }
47         else if (transport == SP_TRANSPORT_USB)
48         {
49                 /* This is a USB to serial converter of some kind. */
50                 printf("Type: USB\n");
51
52                 /* Display string information from the USB descriptors. */
53                 printf("Manufacturer: %s\n", sp_get_port_usb_manufacturer(port));
54                 printf("Product: %s\n", sp_get_port_usb_product(port));
55                 printf("Serial: %s\n", sp_get_port_usb_serial(port));
56
57                 /* Display USB vendor and product IDs. */
58                 int usb_vid, usb_pid;
59                 sp_get_port_usb_vid_pid(port, &usb_vid, &usb_pid);
60                 printf("VID: %04X PID: %04X\n", usb_vid, usb_pid);
61
62                 /* Display bus and address. */
63                 int usb_bus, usb_address;
64                 sp_get_port_usb_bus_address(port, &usb_bus, &usb_address);
65                 printf("Bus: %d Address: %d\n", usb_bus, usb_address);
66         }
67         else if (transport == SP_TRANSPORT_BLUETOOTH)
68         {
69                 /* This is a Bluetooth serial port. */
70                 printf("Type: Bluetooth\n");
71
72                 /* Display Bluetooth MAC address. */
73                 printf("MAC: %s\n", sp_get_port_bluetooth_address(port));
74         }
75
76         printf("Freeing port.\n");
77
78         /* Free the port structure created by sp_get_port_by_name(). */
79         sp_free_port(port);
80
81         /* Note that this will also free the port name and other
82          * strings retrieved from the port structure. If you want
83          * to keep these, copy them before freeing the port. */
84
85         return 0;
86 }