Class: Serial

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

Overview

Copyright © 2014-2016 The Hybrid Group

Instance Method Summary collapse

Constructor Details

#initialize(address, baude_rate = 9600, data_bits = 8, parity = :none, stop_bits = 1) ⇒ Serial

Returns a new instance of Serial.



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

def initialize(address, baude_rate=9600, data_bits=8, parity=:none, stop_bits=1)
  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::Error, 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::Error, 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::Error, RubySerial::Posix::ERROR_CODES[FFI.errno]
  end

  @config = build_config(baude_rate, data_bits, parity, stop_bits)

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

Instance Method Details

#closeObject



36
37
38
39
40
41
42
43
# File 'lib/rubyserial/posix.rb', line 36

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

#closed?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/rubyserial/posix.rb', line 32

def closed?
  !@open
end

#getbyteObject



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

def getbyte
  buff = FFI::MemoryPointer.new :char, 1
  i = RubySerial::Posix.read(@fd, buff, 1)
  if i == -1
    raise RubySerial::Error, 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



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

def gets(sep=$/, limit=nil)
  if block_given?
    loop do
      yield(get_until_sep(sep, limit))
    end
  else
    get_until_sep(sep, limit)
  end
end

#read(size) ⇒ Object



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

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

#write(data) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rubyserial/posix.rb', line 45

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::Error, RubySerial::Posix::ERROR_CODES[FFI.errno]
    else
      n = n+i
    end
  end

  # return number of bytes written
  n
end