File: serialport.rb

package info (click to toggle)
ruby-serialport 1.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 532 kB
  • sloc: ansic: 1,425; ruby: 74; sh: 16; makefile: 4
file content (61 lines) | stat: -rw-r--r-- 1,848 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
require 'serialport.so'


# This class is used for communication over a serial port.
# In addition to the methods here, you can use Ruby IO methods, e.g. read, write, getc, readlines, etc.
#
# @see http://rubydoc.info/stdlib/core/IO Ruby IO class
# @see http://www.cmrr.umn.edu/~strupp/serial.html "Serial Programming Guide for POSIX Operating Systems"
class SerialPort < IO
   autoload :VERSION, 'serialport/version'

   private_class_method(:create)

   # Creates a serial port object.
   # Accepts the port identifier and a variable list for configuration as paramaters or hash.
   # Please see SerialPort#set_modem_params
   #
   # @overload new(port, *params)
   #  @param port [Integer] the serial port number,
   #    where 0 is mapped to "COM1" on Windows, "/dev/ttyS0" on Linux, "/dev/cuaa0" on Mac OS X, etc.
   # @overload new(port, *params)
   #  @param port [String] the serial port file e.g. "/dev/ttyS0"
   # @return [SerialPort]
   # @see SerialPort#set_modem_params
   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.
   #
   # @yield [serial_port] the serial port number or filename
   # @see SerialPort#new
   # @see SerialPort#set_modem_params
   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
end