Class: Smalrubot::TxRx::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/smalrubot/tx_rx/base.rb

Direct Known Subclasses

Serial

Constant Summary collapse

RETURN_CODE =
"\n".ord

Instance Method Summary collapse

Instance Method Details

#flush_readObject



37
38
39
# File 'lib/smalrubot/tx_rx/base.rb', line 37

def flush_read
  gets until gets == nil
end

#gets(timeout = 0.005) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/smalrubot/tx_rx/base.rb', line 43

def gets(timeout=0.005)
  Timeout.timeout(timeout) do
    s = io.gets
    Smalrubot.debug_log("gets: %s", s)
    return s
  end
rescue Exception
  nil
end

#handshakeObject

Raises:



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/smalrubot/tx_rx/base.rb', line 25

def handshake
  5.times do
    write("!9000000.")
    line = gets(1)
    if line && line.match(/ACK:/)
      flush_read
      return line.chomp.split(/:/)[1].to_i
    end
  end
  raise BoardNotFound
end

#read(timeout = 0.005) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/smalrubot/tx_rx/base.rb', line 7

def read(timeout = 0.005)
  line = gets(timeout)
  if line && line.match(/\A\d+:/)
    pin, message = line.chomp.split(/:/)
    if pin && message
      return pin, message
    end
  end
end

#write(message) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/smalrubot/tx_rx/base.rb', line 17

def write(message)
  n = io.write(message)
  Smalrubot.debug_log('write: %s(A:%d, E:%d)', message, n, message.length)
  if n != message.length
    raise "FATAL: n(#{n}) != message.length(#{message.length})"
  end
end