]> sigrok.org Git - libserialport.git/blob - examples/port_info.c
change type of result variables to ssize_t
[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                 printf("sp_get_port_by_name() failed!\n");
29                 return -1;
30         }
31
32         /* Display some basic information about the port. */
33         printf("Port name: %s\n", sp_get_port_name(port));
34         printf("Description: %s\n", sp_get_port_description(port));
35
36         /* Identify the transport which this port is connected through,
37          * e.g. native port, USB or Bluetooth. */
38         enum sp_transport transport = sp_get_port_transport(port);
39
40         if (transport == SP_TRANSPORT_NATIVE) {
41                 /* This is a "native" port, usually directly connected
42                  * to the system rather than some external interface. */
43                 printf("Type: Native\n");
44         } else if (transport == SP_TRANSPORT_USB) {
45                 /* This is a USB to serial converter of some kind. */
46                 printf("Type: USB\n");
47
48                 /* Display string information from the USB descriptors. */
49                 printf("Manufacturer: %s\n", sp_get_port_usb_manufacturer(port));
50                 printf("Product: %s\n", sp_get_port_usb_product(port));
51                 printf("Serial: %s\n", sp_get_port_usb_serial(port));
52
53                 /* Display USB vendor and product IDs. */
54                 int usb_vid, usb_pid;
55                 sp_get_port_usb_vid_pid(port, &usb_vid, &usb_pid);
56                 printf("VID: %04X PID: %04X\n", usb_vid, usb_pid);
57
58                 /* Display bus and address. */
59                 int usb_bus, usb_address;
60                 sp_get_port_usb_bus_address(port, &usb_bus, &usb_address);
61                 printf("Bus: %d Address: %d\n", usb_bus, usb_address);
62         } else if (transport == SP_TRANSPORT_BLUETOOTH) {
63                 /* This is a Bluetooth serial port. */
64                 printf("Type: Bluetooth\n");
65
66                 /* Display Bluetooth MAC address. */
67                 printf("MAC: %s\n", sp_get_port_bluetooth_address(port));
68         }
69
70         printf("Freeing port.\n");
71
72         /* Free the port structure created by sp_get_port_by_name(). */
73         sp_free_port(port);
74
75         /* Note that this will also free the port name and other
76          * strings retrieved from the port structure. If you want
77          * to keep these, copy them before freeing the port. */
78
79         return 0;
80 }