root / util / read.c @ 420df25b
History | View | Annotate | Download (939 Bytes)
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 | tcsetattr(fd, TCSANOW, &ios); |
||
18 | } |
||
19 | |||
20 | int bswap(int x) { |
||
21 | return ((x&0xff) << 24) | ((x&0xff00) << 8) |
||
22 | | ((x>>8) & 0xff00) | ((x>>24) & 0xff); |
||
23 | } |
||
24 | |||
25 | int main(int argc, char **argv) { |
||
26 | int fd;
|
||
27 | char status;
|
||
28 | |||
29 | fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY);
|
||
30 | init_tty(fd); |
||
31 | |||
32 | while (1) { |
||
33 | write(fd, read_cmd, sizeof(read_cmd));
|
||
34 | read(fd, &status, 1);
|
||
35 | if (status == RFID_OK) {
|
||
36 | read(fd, &serno, 4);
|
||
37 | serno = bswap(serno); |
||
38 | printf("%08x\n", serno);
|
||
39 | } else {
|
||
40 | printf("error %d\n", (int)(unsigned char)status); |
||
41 | } |
||
42 | usleep(100000);
|
||
43 | } |
||
44 | |||
45 | close(fd); |
||
46 | return 0; |
||
47 | } |