File: serialport.rb

package info (click to toggle)
libserialport-ruby 0.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 140 kB
  • ctags: 113
  • sloc: ansic: 1,331; ruby: 59; makefile: 7
file content (44 lines) | stat: -rwxr-xr-x 1,140 bytes parent folder | download
1
2
3
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
require "serialport.so"

class SerialPort
   private_class_method(:create)

   # Creates a serial port object.
   #
   # <tt>port</tt> 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.
   #
   # <tt>params</tt> can be used to configure the serial port.
   # See SerialPort#set_modem_params for details
   def SerialPort::new(port, *params)
      sp = create(port)
      begin
         sp.set_modem_params(*params)
      rescue
         sp.close
         raise
      end
      return sp
   end

   # 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.
   def SerialPort::open(port, *params)
      sp = create(port)
      begin
         sp.set_modem_params(*params)
         if (block_given?)
            yield sp
            sp.close
            return nil
         end
      rescue
         sp.close
         raise
      end
      return sp
   end
end