1 #include <libserialport.h>
4 /* Example of how to get information about a serial port.
6 * This example file is released to the public domain. */
8 int main(int argc, char **argv)
10 /* Get the port name from the command line. */
12 printf("Usage: %s <port name>\n", argv[0]);
15 char *port_name = argv[1];
17 /* A pointer to a struct sp_port, which will refer to
21 printf("Looking for port %s.\n", port_name);
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);
29 printf("sp_get_port_by_name() failed!\n");
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));
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);
41 if (transport == SP_TRANSPORT_NATIVE)
43 /* This is a "native" port, usually directly connected
44 * to the system rather than some external interface. */
45 printf("Type: Native\n");
47 else if (transport == SP_TRANSPORT_USB)
49 /* This is a USB to serial converter of some kind. */
50 printf("Type: USB\n");
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));
57 /* Display USB vendor and product IDs. */
59 sp_get_port_usb_vid_pid(port, &usb_vid, &usb_pid);
60 printf("VID: %04X PID: %04X\n", usb_vid, usb_pid);
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);
67 else if (transport == SP_TRANSPORT_BLUETOOTH)
69 /* This is a Bluetooth serial port. */
70 printf("Type: Bluetooth\n");
72 /* Display Bluetooth MAC address. */
73 printf("MAC: %s\n", sp_get_port_bluetooth_address(port));
76 printf("Freeing port.\n");
78 /* Free the port structure created by sp_get_port_by_name(). */
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. */