Class: SerialPort

Inherits:
Object
  • Object
show all
Defined in:
lib/serialport.rb

Constant Summary collapse

NONE =
0
EVEN =
1
ODD =
2
IoPorts =
System::IO::Ports
IoSerialPort =
IoPorts::SerialPort

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, baud, data, stop, parity) ⇒ SerialPort

Returns a new instance of SerialPort.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/serialport.rb', line 11

def initialize(name, baud, data, stop, parity)
  @wait_after_send = nil

  io_parity = case parity
  when NONE then IoPorts::Parity.None
  when EVEN then IoPorts::Parity.Even
  when ODD  then IoPorts::Parity.Odd
  end

  io_stop = case stop
  when 0   then IoPorts::StopBits.None
  when 1   then IoPorts::StopBits.One
  when 1.5 then IoPorts::StopBits.OnePointFive
  when 2   then IoPorts::StopBits.Two
  end

  @port = IoSerialPort.new name, baud, parity, data, stop
  @port.open
end

Instance Attribute Details

#wait_after_sendObject

Returns the value of attribute wait_after_send.



9
10
11
# File 'lib/serialport.rb', line 9

def wait_after_send
  @wait_after_send
end

Instance Method Details

#closeObject



31
32
33
# File 'lib/serialport.rb', line 31

def close
  @port.close
end

#getcObject



40
41
42
# File 'lib/serialport.rb', line 40

def getc
  @port.read_byte.chr
end

#putc(c) ⇒ Object



35
36
37
38
# File 'lib/serialport.rb', line 35

def putc(c)
  @port.write(c)
  sleep(@wait_after_send / 1000.0) if @wait_after_send
end

#readObject



49
50
51
# File 'lib/serialport.rb', line 49

def read
  @port.read_existing
end

#read_timeout=(ms) ⇒ Object



53
54
55
# File 'lib/serialport.rb', line 53

def read_timeout=(ms)
  @port.read_timeout = ms
end

#write(s) ⇒ Object



44
45
46
47
# File 'lib/serialport.rb', line 44

def write(s)
  @port.write(s)
  sleep(@wait_after_send / 1000.0) if @wait_after_send
end