]> sigrok.org Git - libserialport.git/blob - examples/send_receive.c
Fix use of variable length array in send_receive example, for MSVC.
[libserialport.git] / examples / send_receive.c
1 #include <libserialport.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 /* Example of how to send and receive data.
7  *
8  * This example file is released to the public domain. */
9
10 /* Helper function for error handling. */
11 int check(enum sp_return result);
12
13 int main(int argc, char **argv)
14 {
15         /* This example can be used with one or two ports. With one port, it
16          * will send data and try to receive it on the same port. This can be
17          * done by connecting a single wire between the TX and RX pins of the
18          * port.
19          *
20          * Alternatively it can be used with two serial ports connected to each
21          * other, so that data can be sent on one and received on the other.
22          * This can be done with two ports with TX/RX cross-connected, e.g. by
23          * a "null modem" cable, or with a pair of interconnected virtual ports,
24          * such as those created by com0com on Windows or tty0tty on Linux. */
25
26         /* Get the port names from the command line. */
27         if (argc < 2 || argc > 3) {
28                 printf("Usage: %s <port 1> [<port 2>]\n", argv[0]);
29                 return -1;
30         }
31         int num_ports = argc - 1;
32         char **port_names = argv + 1;
33
34         /* The ports we will use. */
35         struct sp_port *ports[2];
36
37         /* Open and configure each port. */
38         for (int i = 0; i < num_ports; i++) {
39                 printf("Looking for port %s.\n", port_names[i]);
40                 check(sp_get_port_by_name(port_names[i], &ports[i]));
41
42                 printf("Opening port.\n");
43                 check(sp_open(ports[i], SP_MODE_READ_WRITE));
44
45                 printf("Setting port to 9600 8N1, no flow control.\n");
46                 check(sp_set_baudrate(ports[i], 9600));
47                 check(sp_set_bits(ports[i], 8));
48                 check(sp_set_parity(ports[i], SP_PARITY_NONE));
49                 check(sp_set_stopbits(ports[i], 1));
50                 check(sp_set_flowcontrol(ports[i], SP_FLOWCONTROL_NONE));
51         }
52
53         /* Now send some data on each port and receive it back. */
54         for (int tx = 0; tx < num_ports; tx++) {
55                 /* Get the ports to send and receive on. */
56                 int rx = num_ports == 1 ? 0 : ((tx == 0) ? 1 : 0);
57                 struct sp_port *tx_port = ports[tx];
58                 struct sp_port *rx_port = ports[rx];
59
60                 /* The data we will send. */
61                 char *data = "Hello!";
62                 int size = strlen(data);
63
64                 /* We'll allow a 1 second timeout for send and receive. */
65                 unsigned int timeout = 1000;
66
67                 /* On success, sp_blocking_write() and sp_blocking_read()
68                  * return the number of bytes sent/received before the
69                  * timeout expired. We'll store that result here. */
70                 int result;
71
72                 /* Send data. */
73                 printf("Sending '%s' (%d bytes) on port %s.\n",
74                                 data, size, sp_get_port_name(tx_port));
75                 result = check(sp_blocking_write(tx_port, data, size, timeout));
76
77                 /* Check whether we sent all of the data. */
78                 if (result == size)
79                         printf("Sent %d bytes successfully.\n", size);
80                 else
81                         printf("Timed out, %d/%d bytes sent.\n", result, size);
82
83                 /* Allocate a buffer to receive data. */
84                 char *buf = malloc(size + 1);
85
86                 /* Try to receive the data on the other port. */
87                 printf("Receiving %d bytes on port %s.\n",
88                                 size, sp_get_port_name(rx_port));
89                 check(sp_blocking_read(rx_port, buf, size, timeout));
90
91                 /* Check whether we received the number of bytes we wanted. */
92                 if (result == size)
93                         printf("Received %d bytes successfully.\n", size);
94                 else
95                         printf("Timed out, %d/%d bytes received.\n", result, size);
96
97                 /* Check if we received the same data we sent. */
98                 buf[result] = '\0';
99                 printf("Received '%s'.\n", buf);
100
101                 /* Free receive buffer. */
102                 free(buf);
103         }
104
105         /* Close ports and free resources. */
106         for (int i = 0; i < num_ports; i++) {
107                 check(sp_close(ports[i]));
108                 sp_free_port(ports[i]);
109         }
110
111         return 0;
112 }
113
114 /* Helper function for error handling. */
115 int check(enum sp_return result)
116 {
117         /* For this example we'll just exit on any error by calling abort(). */
118         char *error_message;
119
120         switch (result) {
121         case SP_ERR_ARG:
122                 printf("Error: Invalid argument.\n");
123                 abort();
124         case SP_ERR_FAIL:
125                 error_message = sp_last_error_message();
126                 printf("Error: Failed: %s\n", error_message);
127                 sp_free_error_message(error_message);
128                 abort();
129         case SP_ERR_SUPP:
130                 printf("Error: Not supported.\n");
131                 abort();
132         case SP_ERR_MEM:
133                 printf("Error: Couldn't allocate memory.\n");
134                 abort();
135         case SP_OK:
136         default:
137                 return result;
138         }
139 }