Class: Serial

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyserial/posix.rb,
lib/rubyserial/windows.rb

Constant Summary collapse

Defaults =
{
  baude_rate: 9600, 
  data_bits: 8,
  vmin: 0,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address, baude_rate = 9600, data_bits = 8) ⇒ Serial

config value order is, in that order, from low to high:

config hash  -over-> baud_rate, data_bits -over->  Defaults


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
# File 'lib/rubyserial/posix.rb', line 17

def initialize(address, baude_rate = nil, data_bits = nil, config = {})
  # XXX this is kind of ugly, but neccessary to preserve old orginal code
  # behaviour with default values
  config[:baude_rate] ||= (baude_rate || Defaults[:baude_rate])
  config[:data_bits] ||= (data_bits || Defaults[:data_bits])
  @config = Defaults.merge(config)

  file_opts = RubySerial::Posix::O_RDWR | RubySerial::Posix::O_NOCTTY | RubySerial::Posix::O_NONBLOCK
  @fd = RubySerial::Posix.open(address, file_opts)

  if @fd == -1
    raise RubySerial::Exception, RubySerial::Posix::ERROR_CODES[FFI.errno]
  else
    @open = true
  end

  fl = RubySerial::Posix.fcntl(@fd, RubySerial::Posix::F_GETFL, :int, 0)
  if fl == -1
    raise RubySerial::Exception, RubySerial::Posix::ERROR_CODES[FFI.errno]
  end

  err = RubySerial::Posix.fcntl(@fd, RubySerial::Posix::F_SETFL, :int, ~RubySerial::Posix::O_NONBLOCK & fl)
  if err == -1
    raise RubySerial::Exception, RubySerial::Posix::ERROR_CODES[FFI.errno]
  end

  termio = build_config(@config)
  err = RubySerial::Posix.tcsetattr(@fd, RubySerial::Posix::TCSANOW, termio)
  if err == -1
    raise RubySerial::Exception, RubySerial::Posix::ERROR_CODES[FFI.errno]
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/rubyserial/posix.rb', line 11

def config
  @config
end

#fdObject (readonly)

use the file descriptor, or IO object instance around it, for blocking read with IO#select or for waiting on multiple i/o lines & events



57
58
59
# File 'lib/rubyserial/posix.rb', line 57

def fd
  @fd
end

Instance Method Details

#closeObject



60
61
62
63
64
65
66
67
# File 'lib/rubyserial/posix.rb', line 60

def close
  err = RubySerial::Posix.close(@fd)
  if err == -1
    raise RubySerial::Exception, RubySerial::Posix::ERROR_CODES[FFI.errno]
  else
    @open = false
  end
end

#closed?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/rubyserial/posix.rb', line 50

def closed?
  !@open
end

#getbyteObject



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rubyserial/posix.rb', line 95

def getbyte
  buff = FFI::MemoryPointer.new :char, 1
  i = RubySerial::Posix.read(@fd, buff, 1)
  if i == -1
    raise RubySerial::Exception, RubySerial::Posix::ERROR_CODES[FFI.errno]
  end

  if i == 0
    nil
  else
    buff.get_bytes(0,1).bytes.first
  end
end

#gets(sep = $/, limit = nil) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rubyserial/posix.rb', line 109

def gets(sep=$/, limit=nil)
  sep = "\n\n" if sep == ''
  # This allows the method signature to be (sep) or (limit)
  (limit = sep; sep="\n") if sep.is_a? Integer
  bytes = []
  loop do
    current_byte = getbyte
    bytes << current_byte unless current_byte.nil?
    break if (bytes.last(sep.bytes.to_a.size) == sep.bytes.to_a) || ((bytes.size == limit) if limit)
  end

  bytes.map { |e| e.chr }.join
end

#ioObject



58
# File 'lib/rubyserial/posix.rb', line 58

def io; @io ||= IO.new(@fd); end

#read(size) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/rubyserial/posix.rb', line 86

def read(size)
  buff = FFI::MemoryPointer.new :char, size
  i = RubySerial::Posix.read(@fd, buff, size)
  if i == -1
    raise RubySerial::Exception, RubySerial::Posix::ERROR_CODES[FFI.errno]
  end
  buff.get_bytes(0, i)
end

#write(data) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rubyserial/posix.rb', line 69

def write(data)
  data = data.to_s
  n =  0
  while data.size > n do
    buff = FFI::MemoryPointer.from_string(data[n..-1].to_s)
    i = RubySerial::Posix.write(@fd, buff, buff.size-1)
    if i == -1
      raise RubySerial::Exception, RubySerial::Posix::ERROR_CODES[FFI.errno]
    else
      n = n+i
    end
  end

  # return number of bytes written
  n
end