/
simple_uart_term.c
107 lines (89 loc) · 2.28 KB
/
simple_uart_term.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/select.h>
#include <fcntl.h>
#include <termios.h>
#include "simple_uart.h"
static void print_usage(const char *program)
{
fprintf(stderr, "%s [-p port] [-b baudrate] [-f flags] | -l\n", program);
}
int main(int argc, char *argv[])
{
const char *port = NULL;
int baudrate;
const char *flags = "8N1";
int opt;
while ((opt = getopt(argc, argv, "p:b:f:lh")) != -1) {
switch (opt) {
case 'p': port = optarg; break;
case 'b': baudrate = atoi(optarg); break;
case 'f': flags = optarg; break;
case 'l': {
char **names;
ssize_t nuarts = simple_uart_list(&names);
printf("Found %zd ports\n", nuarts);
for (ssize_t i = 0; i < nuarts; i++) {
printf("Port %zd: %s\n", i, names[i]);
}
for (ssize_t i = 0; i < nuarts; i++) {
if (names[i]) free(names[i]);
}
free(names);
exit(0);
}
default:
case 'h':
print_usage(argv[0]);
exit(1);
}
}
if (!port) {
print_usage(argv[0]);
exit(1);
}
struct simple_uart *uart;
uart = simple_uart_open(port, baudrate, flags);
if (!uart) {
fprintf(stderr, "Unable to open %s:%d:%s\n", port, baudrate, flags);
exit(1);
}
/* Disable buffering on stdin */
struct termios term;
if (tcgetattr(STDIN_FILENO, &term)) {
fprintf(stderr, "tcgetattr\n");
exit(-1);
}
term.c_lflag &= ~(ICANON | ECHO);
term.c_cc[VMIN] = 0;
term.c_cc[VTIME] = 0;
if (tcsetattr(STDIN_FILENO, TCSANOW, &term)) {
fprintf(stderr, "tcsetattr\n");
exit(-1);
}
while (1) {
fd_set fds;
int nfds = STDIN_FILENO;
struct timeval tv = {1, 0};
char buffer[128];
/* Wait for bytes on either serial port or stdin */
FD_ZERO(&fds);
FD_SET(STDIN_FILENO, &fds);
FD_SET(simple_uart_get_fd(uart), &fds);
if (simple_uart_get_fd(uart) > nfds)
nfds = simple_uart_get_fd(uart);
select(nfds + 1, &fds, NULL, NULL, &tv);
/* Move bytes from one to the other */
if (FD_ISSET(STDIN_FILENO, &fds)) {
int len = read(STDIN_FILENO, buffer, sizeof(buffer));
if (len > 0) simple_uart_write(uart, buffer, len);
}
if (FD_ISSET(simple_uart_get_fd(uart), &fds)) {
int len = simple_uart_read(uart, buffer, sizeof(buffer));
if (len > 0) write(STDOUT_FILENO, buffer, len);
}
}
simple_uart_close(uart);
return 0;
}