Statistics
| Branch: | Revision:

root / util / read.c @ 8c95bbef

History | View | Annotate | Download (1.11 KB)

1
#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, TCSAFLUSH, &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
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
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
    tcflush(fd, TCIFLUSH);
44
    write(fd, read_cmd, sizeof(read_cmd));
45
    read_all(fd, &status, 1);
46
    if (status == RFID_OK) {
47
      read_all(fd, &serno, 4);
48
      serno = bswap(serno);
49
      printf("%08x\n", serno);
50
    } else {
51
      printf("error %d\n", (int)(unsigned char)status);
52
    }
53
    usleep(50000);
54
  }
55

    
56
  close(fd);
57
  return 0;
58
}