]> sigrok.org Git - libserialport.git/blob - examples/await_events.c
Relax timing test because it's too tight for Windows.
[libserialport.git] / examples / await_events.c
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 = malloc(num_ports * sizeof(struct sp_port *));
24         if (!ports)
25                 abort();
26
27         /* The set of events we will wait for. */
28         struct sp_event_set *event_set;
29
30         /* Allocate the event set. */
31         check(sp_new_event_set(&event_set));
32
33         /* Open and configure each port, and then add its RX event
34          * to the event set. */
35         for (int i = 0; i < num_ports; i++) {
36
37                 printf("Looking for port %s.\n", port_names[i]);
38                 check(sp_get_port_by_name(port_names[i], &ports[i]));
39
40                 printf("Opening port.\n");
41                 check(sp_open(ports[i], SP_MODE_READ));
42
43                 printf("Setting port to 9600 8N1, no flow control.\n");
44                 check(sp_set_baudrate(ports[i], 9600));
45                 check(sp_set_bits(ports[i], 8));
46                 check(sp_set_parity(ports[i], SP_PARITY_NONE));
47                 check(sp_set_stopbits(ports[i], 1));
48                 check(sp_set_flowcontrol(ports[i], SP_FLOWCONTROL_NONE));
49
50                 printf("Adding port RX event to event set.\n");
51                 check(sp_add_port_events(event_set, ports[i], SP_EVENT_RX_READY));
52         }
53
54         /* Now we can call sp_wait() to await any event in the set.
55          * It will return when an event occurs, or the timeout elapses. */
56         printf("Waiting up to 5 seconds for RX on any port...\n");
57         check(sp_wait(event_set, 5000));
58
59         /* Iterate over ports to see which have data waiting. */
60         for (int i = 0; i < num_ports; i++) {
61                 /* Get number of bytes waiting. */
62                 int bytes_waiting = check(sp_input_waiting(ports[i]));
63                 printf("Port %s: %d bytes received.\n",
64                                 sp_get_port_name(ports[i]), bytes_waiting);
65         }
66
67         /* Close ports and free resources. */
68         sp_free_event_set(event_set);
69         for (int i = 0; i < num_ports; i++) {
70                 check(sp_close(ports[i]));
71                 sp_free_port(ports[i]);
72         }
73
74         return 0;
75 }
76
77 /* Helper function for error handling. */
78 int check(enum sp_return result)
79 {
80         /* For this example we'll just exit on any error by calling abort(). */
81         char *error_message;
82         switch (result) {
83                 case SP_ERR_ARG:
84                         printf("Error: Invalid argument.\n");
85                         abort();
86                 case SP_ERR_FAIL:
87                         error_message = sp_last_error_message();
88                         printf("Error: Failed: %s\n", error_message);
89                         sp_free_error_message(error_message);
90                         abort();
91                 case SP_ERR_SUPP:
92                         printf("Error: Not supported.\n");
93                         abort();
94                 case SP_ERR_MEM:
95                         printf("Error: Couldn't allocate memory.\n");
96                         abort();
97                 case SP_OK:
98                 default:
99                         return result;
100         }
101 }