root / util / read.c @ 8c95bbef
History | View | Annotate | Download (1.11 KB)
1 | 420df25b | Tom Mullins | #include <sys/types.h> |
---|---|---|---|
2 | #include <sys/stat.h> |
||
3 | #include <fcntl.h> |
||
4 | #include <termios.h> |
||
5 | #include <unistd.h> |
||
6 | #include <stdio.h> |
||
7 | #include <string.h> |
||
8 | |||
9 | #define RFID_OK 1 |
||
10 | static char read_cmd[] = {'!', 'R', 'W', 1, 32}; |
||
11 | static int serno; |
||
12 | |||
13 | void init_tty(int fd) { |
||
14 | struct termios ios;
|
||
15 | bzero(&ios, sizeof(ios));
|
||
16 | ios.c_cflag = B9600 | CS8 | CREAD; |
||
17 | 8c95bbef | Tom Mullins | tcsetattr(fd, TCSAFLUSH, &ios); |
18 | 420df25b | Tom Mullins | } |
19 | |||
20 | int bswap(int x) { |
||
21 | return ((x&0xff) << 24) | ((x&0xff00) << 8) |
||
22 | | ((x>>8) & 0xff00) | ((x>>24) & 0xff); |
||
23 | } |
||
24 | |||
25 | 8c95bbef | Tom Mullins | void read_all(int fd, void *buf, int n) { |
26 | int nread;
|
||
27 | while (n > 0) { |
||
28 | nread = read(fd, buf, n); |
||
29 | buf += nread; |
||
30 | n -= nread; |
||
31 | usleep(10000);
|
||
32 | } |
||
33 | } |
||
34 | |||
35 | 420df25b | Tom Mullins | int main(int argc, char **argv) { |
36 | int fd;
|
||
37 | char status;
|
||
38 | |||
39 | fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY);
|
||
40 | init_tty(fd); |
||
41 | |||
42 | while (1) { |
||
43 | 8c95bbef | Tom Mullins | tcflush(fd, TCIFLUSH); |
44 | 420df25b | Tom Mullins | write(fd, read_cmd, sizeof(read_cmd));
|
45 | 8c95bbef | Tom Mullins | read_all(fd, &status, 1);
|
46 | 420df25b | Tom Mullins | if (status == RFID_OK) {
|
47 | 8c95bbef | Tom Mullins | read_all(fd, &serno, 4);
|
48 | 420df25b | Tom Mullins | serno = bswap(serno); |
49 | printf("%08x\n", serno);
|
||
50 | } else {
|
||
51 | printf("error %d\n", (int)(unsigned char)status); |
||
52 | } |
||
53 | 8c95bbef | Tom Mullins | usleep(50000);
|
54 | 420df25b | Tom Mullins | } |
55 | |||
56 | close(fd); |
||
57 | return 0; |
||
58 | } |