-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathserial_client.rb
77 lines (60 loc) · 1.2 KB
/
serial_client.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
require 'serialport'
class SerialClient
def initialize device, baud
@serial = SerialPort.new device, baud
@serial.read_timeout = 2000
end
def time_required *values
write_serial "#{values.collect { |value| "%3.1f" % value}.join ' '}t"
read_serial.to_f
end
def time
write_serial 't'
read_serial.to_i * 0.001
end
def time_remaining
write_serial 'T'
read_serial.to_f
end
def target *values
write_serial "#{values.collect { |value| "%3.1f" % value}.join ' '}c"
end
def configuration str
write_serial str
read_serial.split(' ').collect &:to_f
end
def pos
configuration 'c'
end
def lower
configuration 'l'
end
def upper
configuration 'u'
end
def ready?
write_serial 'o'
read_serial.to_i != 0
end
def teach_point_name index
(index + 'a'.ord).chr
end
def save_teach_point index
write_serial "m#{teach_point_name(index)}"
end
def load_teach_point index
c = teach_point_name index
configuration "'#{c}d#{c}"
end
def stop
write_serial 'x'
end
def write_serial str
@serial.write str
@serial.flush
end
def read_serial
result = @serial.readline
result
end
end