Revision 420df25b

View differences:

util/read
1
#!/bin/bash
2

  
3
stty 9600 raw < /dev/ttyUSB0
4
#-echo -echok -icrnl -ixon -icanon -opost -onlcr 
5

  
6
xxd -r readhex > /dev/ttyUSB0
7

  
8
sleep 0.1
9

  
10
response=`head -c 5 /dev/ttyUSB0`
11
ok=`printf "\001"`
12

  
13
if [ ${response:0:1} = $ok ]; then
14
  echo -n ${response:1} | xxd
15
else
16
  # if it failed, it hangs waiting for more characters anyway, so this never
17
  # happens unless the serial port is missing :(
18
  echo Failed.
19
fi
util/read.c
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, 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
}
util/read.sh
1
#!/bin/bash
2

  
3
stty 9600 raw < /dev/ttyUSB0
4
#-echo -echok -icrnl -ixon -icanon -opost -onlcr 
5

  
6
xxd -r readhex > /dev/ttyUSB0
7

  
8
sleep 0.1
9

  
10
response=`head -c 5 /dev/ttyUSB0`
11
ok=`printf "\001"`
12

  
13
if [ ${response:0:1} = $ok ]; then
14
  echo -n ${response:1} | xxd
15
else
16
  # if it failed, it hangs waiting for more characters anyway, so this never
17
  # happens unless the serial port is missing :(
18
  echo Failed.
19
fi

Also available in: Unified diff