-
Notifications
You must be signed in to change notification settings - Fork 8
/
elvis.c
54 lines (48 loc) · 846 Bytes
/
elvis.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
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
int main()
{
struct termios t;
int count, a;
char buf[12];
int fd = open("/dev/ttyUSB0", O_RDWR);
if (fd < 0) {
perror("open");
return 1;
}
if (tcgetattr(fd, &t)) {
perror("geta");
return 2;
}
t.c_cflag = B4800 | CS8 | CREAD;
t.c_iflag = 0;
t.c_oflag = 0;
t.c_lflag = 0;
t.c_cc[VMIN] = 12;
t.c_cc[VTIME] = 0;
if (tcflush(fd, TCIFLUSH)) {
perror("flush");
return 2;
}
if (tcsetattr(fd, TCSANOW, &t)) {
perror("seta");
return 2;
}
buf[0] = 0;
while (buf[0] != 0x0a)
read(fd, buf, 1);
while ((count = read(fd, buf, sizeof(buf)))) {
if (count < 0) {
perror("read");
return 3;
}
if (count != sizeof(buf))
continue;
printf("%10d, %s", rand(), buf);
}
close(fd);
}