]>
Commit | Line | Data |
---|---|---|
a20ed296 ML |
1 | #include <libserialport.h> |
2 | #include <stdio.h> | |
3 | #include <stdlib.h> | |
4 | ||
5 | /* Example of how to wait for events on multiple ports. | |
6 | * | |
7 | * This example file is released to the public domain. */ | |
8 | ||
9 | /* Helper function for error handling. */ | |
10 | int check(enum sp_return result); | |
11 | ||
12 | int main(int argc, char **argv) | |
13 | { | |
14 | /* Get the port names from the command line. */ | |
15 | if (argc < 2) { | |
16 | printf("Usage: %s <port name>...\n", argv[0]); | |
17 | return -1; | |
18 | } | |
19 | int num_ports = argc - 1; | |
20 | char **port_names = argv + 1; | |
21 | ||
22 | /* The ports we will use. */ | |
23 | struct sp_port *ports[num_ports]; | |
24 | ||
25 | /* The set of events we will wait for. */ | |
26 | struct sp_event_set *event_set; | |
27 | ||
28 | /* Allocate the event set. */ | |
29 | check(sp_new_event_set(&event_set)); | |
30 | ||
31 | /* Open and configure each port, and then add its RX event | |
32 | * to the event set. */ | |
33 | for (int i = 0; i < num_ports; i++) { | |
34 | ||
35 | printf("Looking for port %s.\n", port_names[i]); | |
36 | check(sp_get_port_by_name(port_names[i], &ports[i])); | |
37 | ||
38 | printf("Opening port.\n"); | |
39 | check(sp_open(ports[i], SP_MODE_READ)); | |
40 | ||
41 | printf("Setting port to 9600 8N1, no flow control.\n"); | |
42 | check(sp_set_baudrate(ports[i], 9600)); | |
43 | check(sp_set_bits(ports[i], 8)); | |
44 | check(sp_set_parity(ports[i], SP_PARITY_NONE)); | |
45 | check(sp_set_stopbits(ports[i], 1)); | |
46 | check(sp_set_flowcontrol(ports[i], SP_FLOWCONTROL_NONE)); | |
47 | ||
48 | printf("Adding port RX event to event set.\n"); | |
49 | check(sp_add_port_events(event_set, ports[i], SP_EVENT_RX_READY)); | |
50 | } | |
51 | ||
52 | /* Now we can call sp_wait() to await any event in the set. | |
53 | * It will return when an event occurs, or the timeout elapses. */ | |
54 | printf("Waiting up to 5 seconds for RX on any port...\n"); | |
55 | check(sp_wait(event_set, 5000)); | |
56 | ||
57 | /* Iterate over ports to see which have data waiting. */ | |
58 | for (int i = 0; i < num_ports; i++) { | |
59 | /* Get number of bytes waiting. */ | |
60 | int bytes_waiting = check(sp_input_waiting(ports[i])); | |
61 | printf("Port %s: %d bytes received.\n", | |
62 | sp_get_port_name(ports[i]), bytes_waiting); | |
63 | } | |
64 | ||
65 | /* Close ports and free resources. */ | |
66 | sp_free_event_set(event_set); | |
67 | for (int i = 0; i < num_ports; i++) { | |
68 | check(sp_close(ports[i])); | |
69 | sp_free_port(ports[i]); | |
70 | } | |
71 | ||
72 | return 0; | |
73 | } | |
74 | ||
75 | /* Helper function for error handling. */ | |
76 | int check(enum sp_return result) | |
77 | { | |
78 | /* For this example we'll just exit on any error by calling abort(). */ | |
79 | char *error_message; | |
80 | switch (result) { | |
81 | case SP_ERR_ARG: | |
82 | printf("Error: Invalid argument.\n"); | |
83 | abort(); | |
84 | case SP_ERR_FAIL: | |
85 | error_message = sp_last_error_message(); | |
86 | printf("Error: Failed: %s\n", error_message); | |
87 | sp_free_error_message(error_message); | |
88 | abort(); | |
89 | case SP_ERR_SUPP: | |
90 | printf("Error: Not supported.\n"); | |
91 | abort(); | |
92 | case SP_ERR_MEM: | |
93 | printf("Error: Couldn't allocate memory.\n"); | |
94 | abort(); | |
95 | case SP_OK: | |
96 | default: | |
97 | return result; | |
98 | } | |
99 | } |