Class: Rpremote::Serial

Inherits:
Object
  • Object
show all
Defined in:
lib/rpremote/serial.rb,
sig/rpremote.rbs

Defined Under Namespace

Classes: ConfigurationError

Constant Summary collapse

BAUD_RATE =

Returns:

  • (Integer)
115_200

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, baud: BAUD_RATE) ⇒ Serial

Returns a new instance of Serial.

Parameters:

  • path (String)
  • baud: (Integer) (defaults to: BAUD_RATE)


24
25
26
27
28
29
30
31
32
33
# File 'lib/rpremote/serial.rb', line 24

def initialize(path, baud: BAUD_RATE)
  @path = path
  @baud = Integer(baud)
  configure!
  @io = File.open(path, File::RDWR)
  @io.binmode
  @io.sync = true
rescue SystemCallError => e
  raise ConfigurationError, "cannot open serial port #{path}: #{e.message}"
end

Instance Attribute Details

#baudInteger (readonly)

Returns the value of attribute baud.

Returns:

  • (Integer)


22
23
24
# File 'lib/rpremote/serial.rb', line 22

def baud
  @baud
end

#ioFile (readonly)

Returns the value of attribute io.

Returns:

  • (File)


22
23
24
# File 'lib/rpremote/serial.rb', line 22

def io
  @io
end

#pathString (readonly)

Returns the value of attribute path.

Returns:

  • (String)


22
23
24
# File 'lib/rpremote/serial.rb', line 22

def path
  @path
end

Class Method Details

.openvoid .open(path, baud:) ⇒ Serial

Overloads:

  • .openvoid

    This method returns an undefined value.

  • .open(path, baud:) ⇒ Serial

    Parameters:

    • path (String)
    • baud: (Integer)

    Returns:



11
12
13
14
15
16
17
18
19
20
# File 'lib/rpremote/serial.rb', line 11

def self.open(path, baud: BAUD_RATE)
  port = new(path, baud: baud)
  return port unless block_given?

  begin
    yield port
  ensure
    port.close
  end
end

Instance Method Details

#closeObject

Returns:

  • (Object)


47
48
49
# File 'lib/rpremote/serial.rb', line 47

def close
  io.close unless io.closed?
end

#closed?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/rpremote/serial.rb', line 51

def closed?
  io.closed?
end

#flushObject

Returns:

  • (Object)


43
44
45
# File 'lib/rpremote/serial.rb', line 43

def flush
  io.flush
end

#read_nonblock(length) ⇒ String

Parameters:

  • length (Integer)

Returns:

  • (String)


35
36
37
# File 'lib/rpremote/serial.rb', line 35

def read_nonblock(length)
  io.read_nonblock(length)
end

#to_ioFile

Returns:

  • (File)


55
56
57
# File 'lib/rpremote/serial.rb', line 55

def to_io
  io
end

#write(data) ⇒ Integer

Parameters:

  • data (String)

Returns:

  • (Integer)


39
40
41
# File 'lib/rpremote/serial.rb', line 39

def write(data)
  io.write(data)
end