1 #include <libserialport.h>
6 /* Example of how to send and receive data.
8 * This example file is released to the public domain. */
10 /* Helper function for error handling. */
11 int check(enum sp_return result
);
13 int main(int argc
, char **argv
)
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
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. */
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]);
31 int num_ports
= argc
- 1;
32 char **port_names
= argv
+ 1;
34 /* The ports we will use. */
35 struct sp_port
*ports
[2];
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
]));
42 printf("Opening port.\n");
43 check(sp_open(ports
[i
], SP_MODE_READ_WRITE
));
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
));
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
];
60 /* The data we will send. */
61 char *data
= "Hello!";
62 int size
= strlen(data
);
64 /* We'll allow a 1 second timeout for send and receive. */
65 unsigned int timeout
= 1000;
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. */
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
));
77 /* Check whether we sent all of the data. */
79 printf("Sent %d bytes successfully.\n", size
);
81 printf("Timed out, %d/%d bytes sent.\n", result
, size
);
83 /* Allocate a buffer to receive data. */
84 char *buf
= malloc(size
+ 1);
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 result
= check(sp_blocking_read(rx_port
, buf
, size
, timeout
));
91 /* Check whether we received the number of bytes we wanted. */
93 printf("Received %d bytes successfully.\n", size
);
95 printf("Timed out, %d/%d bytes received.\n", result
, size
);
97 /* Check if we received the same data we sent. */
99 printf("Received '%s'.\n", buf
);
101 /* Free receive buffer. */
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
]);
114 /* Helper function for error handling. */
115 int check(enum sp_return result
)
117 /* For this example we'll just exit on any error by calling abort(). */
122 printf("Error: Invalid argument.\n");
125 error_message
= sp_last_error_message();
126 printf("Error: Failed: %s\n", error_message
);
127 sp_free_error_message(error_message
);
130 printf("Error: Not supported.\n");
133 printf("Error: Couldn't allocate memory.\n");