HACK: don't even check for termiox
[libserialport/gsi.git] / examples / port_info.c
blob8bf6022046a40c35cd4115ee2074faf2bb22ddcc
1 #include <libserialport.h>
2 #include <stdio.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. */
11 if (argc != 2) {
12 printf("Usage: %s <port name>\n", argv[0]);
13 return -1;
15 char *port_name = argv[1];
17 /* A pointer to a struct sp_port, which will refer to
18 * the port found. */
19 struct sp_port *port;
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);
27 if (result != SP_OK) {
28 printf("sp_get_port_by_name() failed!\n");
29 return -1;
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));
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);
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");
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));
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);
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");
66 /* Display Bluetooth MAC address. */
67 printf("MAC: %s\n", sp_get_port_bluetooth_address(port));
70 printf("Freeing port.\n");
72 /* Free the port structure created by sp_get_port_by_name(). */
73 sp_free_port(port);
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. */
79 return 0;