Class: Cosmos::SerialStream

Inherits:
Stream show all
Defined in:
lib/cosmos/streams/serial_stream.rb

Overview

Stream that reads and writes to serial ports by using SerialDriver.

Instance Attribute Summary

Attributes inherited from Stream

#raw_logger_pair

Instance Method Summary collapse

Constructor Details

#initialize(write_port_name, read_port_name, baud_rate, parity, stop_bits, write_timeout, read_timeout) ⇒ SerialStream

Returns a new instance of SerialStream.

Parameters:

  • write_port_name (String)

    The name of the serial port to write. Pass nil if the stream is to be read only. On Windows the port name is typically 'COMX' where X can be any port number. On UNIX the port name is typically a device such as '/dev/ttyS0'.

  • read_port_name (String)

    The name of the serial port to read. Pass nil if the stream is to be read only. On Windows the port name is typically 'COMX' where X can be any port number. On UNIX the port name is typically a device such as '/dev/ttyS0'.

  • baud_rate (Integer)

    The serial port baud rate

  • parity (Symbol)

    Must be :NONE, :EVEN, or :ODD

  • stop_bits (Integer)

    Number of stop bits. Must be 1 or 2.

  • write_timeout (Integer)

    Number of seconds to wait for the write to complete. Pass nil to create no timeout. The Cosmos::SerialDriver will continously try to send the data until it has been sent or an error occurs.

  • read_timeout (Integer)

    Number of seconds to wait for the read to complete. Pass nil to create no timeout. The Cosmos::SerialDriver will continously try to read data until it has received data or an error occurs.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/cosmos/streams/serial_stream.rb', line 41

def initialize(write_port_name,
               read_port_name,
               baud_rate,
               parity,
               stop_bits,
               write_timeout,
               read_timeout)
  super()

  # The SerialDriver class will validate the parameters
  @write_port_name = ConfigParser.handle_nil(write_port_name)
  @read_port_name  = ConfigParser.handle_nil(read_port_name)
  @baud_rate       = Integer(baud_rate)
  @parity          = parity
  @stop_bits       = stop_bits.to_i
  @write_timeout   = ConfigParser.handle_nil(write_timeout)
  @write_timeout   = @write_timeout.to_f if @write_timeout
  @read_timeout    = ConfigParser.handle_nil(read_timeout)
  @read_timeout    = @read_timeout.to_f if @read_timeout

  if @write_port_name
    @write_serial_port = SerialDriver.new(@write_port_name,
                                          @baud_rate,
                                          @parity,
                                          @stop_bits,
                                          @write_timeout,
                                          @read_timeout)
  else
    @write_serial_port = nil
  end
  if @read_port_name
    if @read_port_name == @write_port_name
      @read_serial_port = @write_serial_port
    else
      @read_serial_port = SerialDriver.new(@read_port_name,
                                           @baud_rate,
                                           @parity,
                                           @stop_bits,
                                           @write_timeout,
                                           @read_timeout)
    end
  else
    @read_serial_port = nil
  end
  if @read_serial_port.nil? && @write_serial_port.nil?
    raise "Either a write port or read port must be given"
  end

  # We 'connect' when we create the stream
  @connected = true

  # Mutex on write is needed to protect from commands coming in from more
  # than one tool
  @write_mutex = Mutex.new
end

Instance Method Details

#connectObject

Connect the stream



127
128
129
# File 'lib/cosmos/streams/serial_stream.rb', line 127

def connect
  # N/A - Serial streams 'connect' on creation
end

#connected?Boolean

Returns Whether the serial stream is connected to the serial port.

Returns:

  • (Boolean)

    Whether the serial stream is connected to the serial port



133
134
135
# File 'lib/cosmos/streams/serial_stream.rb', line 133

def connected?
  @connected
end

#disconnectObject

Disconnect by closing the serial ports



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/cosmos/streams/serial_stream.rb', line 138

def disconnect
  if @connected
    begin
      @write_serial_port.close if @write_serial_port && !@write_serial_port.closed?
    rescue IOError
      # Ignore
    end

    begin
      @read_serial_port.close if @read_serial_port && !@read_serial_port.closed?
    rescue IOError
      # Ignore
    end
    @connected = false
  end
end

#readString

Returns a binary string of data from the serial port

Returns:

  • (String)

    Returns a binary string of data from the serial port



98
99
100
101
102
103
104
105
# File 'lib/cosmos/streams/serial_stream.rb', line 98

def read
  raise "Attempt to read from write only stream" unless @read_serial_port
  # No read mutex is needed because there is only one stream procesor
  # reading
  data = @read_serial_port.read
  @raw_logger_pair.read_logger.write(data) if @raw_logger_pair
  data
end

#read_nonblockString

Returns a binary string of data from the serial port without blocking

Returns:

  • (String)

    Returns a binary string of data from the serial port without blocking



108
109
110
111
112
113
114
115
# File 'lib/cosmos/streams/serial_stream.rb', line 108

def read_nonblock
  raise "Attempt to read from write only stream" unless @read_serial_port
  # No read mutex is needed because there is only one stream procesor
  # reading
  data = @read_serial_port.read_nonblock
  @raw_logger_pair.read_logger.write(data) if @raw_logger_pair
  data
end

#write(data) ⇒ Object

Parameters:

  • data (String)

    A binary string of data to write to the serial port



118
119
120
121
122
123
124
# File 'lib/cosmos/streams/serial_stream.rb', line 118

def write(data)
  raise "Attempt to write to read only stream" unless @write_serial_port
  @write_mutex.synchronize do
    @write_serial_port.write(data)
    @raw_logger_pair.write_logger.write(data) if @raw_logger_pair
  end
end