-
Notifications
You must be signed in to change notification settings - Fork 0
/
octopus.rb
39 lines (34 loc) · 1011 Bytes
/
octopus.rb
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
#!/usr/bin/env ruby
# Ruby class to read Octopus RFID tag ids with a 14333C compatible USB reader.
# Octopus tags begin with a ~ and have a length of 14 ASCII characters.
require 'rubygems'
require 'serialport'
class Octopus
attr_accessor :port, :baud, :tag_length
def initialize(port = "/dev/ttyUSB0", baud = 9600, tag_length = 14)
@port, @baud, @tag_length = port, baud, tag_length
end
def reset
SerialPort.open @port, @baud do |sp|
sp.write "55AA01020455AA\n"
end
end
def read
# Loops until an RFID tag is detected, and returns the tag id.
tag_id = ""
SerialPort.open @port, @baud do |sp|
while true
sp.read_timeout = 1
v = sp.read
if v.start_with?("~") # If read string starts with "~", reset tag_id
tag_id = v[1, v.size]
elsif !v.empty?
tag_id += v.strip
end
tag_id.gsub!("~", "")
tag_id.strip!
return tag_id if tag_id.size == @tag_length
end
end
end
end