Class: SerialPort

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

Class Method Summary collapse

Class Method Details

.new(port, *params) ⇒ Object

Creates a serial port object.

port may be a port number or the file name of a defice. The number is portable; so 0 is mapped to “COM1” on Windows, “/dev/ttyS0” on Linux, “/dev/cuaa0” on Mac OS X, etc.

params can be used to configure the serial port. See SerialPort#set_modem_params for details



15
16
17
18
19
20
21
22
23
24
# File 'lib/extensions/serialport/serialport.rb', line 15

def SerialPort::new(port, *params)
   sp = create(port)
   begin
      sp.set_modem_params(*params)
   rescue
      sp.close
      raise
   end
   return sp
end

.open(port, *params) ⇒ Object

This behaves like SerialPort#new, except that you can pass a block to which the new serial port object will be passed. In this case the connection is automaticaly closed when the block has finished.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/extensions/serialport/serialport.rb', line 29

def SerialPort::open(port, *params)
   sp = create(port)
   begin
      sp.set_modem_params(*params)
   rescue
      sp.close
      raise
   end
   if (block_given?)
     begin
        yield sp
     ensure             
        sp.close
     end
     return nil
   end
   return sp
end