Class: Cosmos::SerialInterface

Inherits:
StreamInterface show all
Defined in:
lib/cosmos/interfaces/serial_interface.rb

Overview

Provides a base class for interfaces that use serial ports

Constant Summary

Constants included from ApiShared

ApiShared::DEFAULT_TLM_POLLING_RATE

Constants included from Extract

Extract::SCANNING_REGULAR_EXPRESSION

Instance Attribute Summary

Attributes inherited from StreamInterface

#stream

Attributes inherited from Interface

#auto_reconnect, #bytes_read, #bytes_written, #cmd_routers, #connect_on_startup, #disable_disconnect, #interfaces, #name, #num_clients, #options, #override_tlm, #packet_log_writer_pairs, #protocol_info, #raw_logger_pair, #read_count, #read_protocols, #read_queue_size, #read_raw_data, #read_raw_data_time, #reconnect_delay, #routers, #stored_packet_log_writer_pairs, #target_names, #thread, #write_count, #write_protocols, #write_queue_size, #written_raw_data, #written_raw_data_time

Instance Method Summary collapse

Methods inherited from StreamInterface

#connected?, #disconnect, #read_interface, #write_interface

Methods inherited from Interface

#_normalize_tlm, #_override, #_override_tlm, #_override_tlm_raw, #_write, #add_protocol, #connected?, #convert_data_to_packet, #convert_packet_to_data, #copy_to, #disconnect, #read, #read_allowed?, #read_interface, #read_interface_base, #start_raw_logging, #stop_raw_logging, #write, #write_allowed?, #write_interface, #write_interface_base, #write_raw, #write_raw_allowed?

Methods included from Api

#cmd, #cmd_no_checks, #cmd_no_hazardous_check, #cmd_no_range_check, #cmd_raw, #cmd_raw_no_checks, #cmd_raw_no_hazardous_check, #cmd_raw_no_range_check, #cmd_tlm_clear_counters, #cmd_tlm_reload, #connect_interface, #connect_router, #disable_limits, #disable_limits_group, #disconnect_interface, #disconnect_router, #enable_limits, #enable_limits_group, #get_all_cmd_info, #get_all_interface_info, #get_all_packet_logger_info, #get_all_router_info, #get_all_target_info, #get_all_tlm_info, #get_background_tasks, #get_cmd_buffer, #get_cmd_cnt, #get_cmd_hazardous, #get_cmd_list, #get_cmd_log_filename, #get_cmd_param_list, #get_cmd_time, #get_cmd_value, #get_interface_info, #get_interface_names, #get_interface_targets, #get_limits, #get_limits_event, #get_limits_groups, #get_limits_set, #get_limits_sets, #get_out_of_limits, #get_output_logs_filenames, #get_overall_limits_state, #get_packet, #get_packet_data, #get_packet_logger_info, #get_packet_loggers, #get_router_info, #get_router_names, #get_saved_config, #get_screen_definition, #get_screen_list, #get_server_message, #get_server_message_log_filename, #get_server_status, #get_stale, #get_target_ignored_items, #get_target_ignored_parameters, #get_target_info, #get_target_list, #get_tlm_buffer, #get_tlm_cnt, #get_tlm_details, #get_tlm_item_list, #get_tlm_list, #get_tlm_log_filename, #get_tlm_packet, #get_tlm_values, #inject_tlm, #interface_state, #limits_enabled?, #map_target_to_interface, #normalize_tlm, #override_tlm, #override_tlm_raw, #replay_move_end, #replay_move_index, #replay_move_start, #replay_play, #replay_reverse_play, #replay_select_file, #replay_set_playback_delay, #replay_status, #replay_step_back, #replay_step_forward, #replay_stop, #router_state, #send_raw, #set_limits, #set_limits_set, #set_tlm, #set_tlm_raw, #start_background_task, #start_cmd_log, #start_logging, #start_new_server_message_log, #start_raw_logging_interface, #start_raw_logging_router, #start_tlm_log, #stop_background_task, #stop_cmd_log, #stop_logging, #stop_raw_logging_interface, #stop_raw_logging_router, #stop_tlm_log, #subscribe_limits_events, #subscribe_packet_data, #subscribe_server_messages, #tlm, #tlm_formatted, #tlm_raw, #tlm_variable, #tlm_with_units, #unsubscribe_limits_events, #unsubscribe_packet_data, #unsubscribe_server_messages

Constructor Details

#initialize(write_port_name, read_port_name, baud_rate, parity, stop_bits, write_timeout, read_timeout, protocol_type = nil, *protocol_args) ⇒ SerialInterface

Creates a serial interface which uses the specified stream protocol.

Parameters:

  • write_port_name (String)

    The name of the serial port to write

  • read_port_name (String)

    The name of the serial port to read

  • baud_rate (Integer)

    The serial port baud rate

  • parity (Symbol)

    The parity which is normally :NONE. Must be one of :NONE, :EVEN, or :ODD.

  • stop_bits (Integer)

    The number of stop bits which is normally 1.

  • write_timeout (Integer)

    The number of seconds to attempt the write before aborting

  • read_timeout (Integer)

    The number of seconds to attempt to read data from the serial port before aborting

  • protocol_type (String) (defaults to: nil)

    Combined with 'Protocol' to resolve to a COSMOS protocol class

  • protocol_args (Array)

    Arguments to pass to the protocol constructor



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
# File 'lib/cosmos/interfaces/serial_interface.rb', line 32

def initialize(write_port_name,
               read_port_name,
               baud_rate,
               parity,
               stop_bits,
               write_timeout,
               read_timeout,
               protocol_type = nil,
               *protocol_args)
  super(protocol_type, protocol_args)

  @write_port_name = ConfigParser.handle_nil(write_port_name)
  @read_port_name = ConfigParser.handle_nil(read_port_name)
  @baud_rate = baud_rate
  @parity = parity.to_s.intern
  @stop_bits = stop_bits
  @write_timeout = write_timeout
  @read_timeout = read_timeout
  @write_allowed = false unless @write_port_name
  @write_raw_allowed = false unless @write_port_name
  @read_allowed = false unless @read_port_name
  @flow_control = :NONE
  @data_bits = 8
  @struct = []
end

Instance Method Details

#connectObject

Creates a new Cosmos::SerialStream using the parameters passed in the constructor



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cosmos/interfaces/serial_interface.rb', line 59

def connect
  @stream = SerialStream.new(
    @write_port_name,
    @read_port_name,
    @baud_rate,
    @parity,
    @stop_bits,
    @write_timeout,
    @read_timeout,
    @flow_control,
    @data_bits,
    @struct
  )
  super()
end

#set_option(option_name, option_values) ⇒ Object

Supported Options FLOW_CONTROL - Flow control method NONE or RTSCTS. Defaults to NONE DATA_BITS - How many data bits to use STRUCT - Directly set fields in the Win32 DCB or POSIX termios structure



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/cosmos/interfaces/serial_interface.rb', line 79

def set_option(option_name, option_values)
  super(option_name, option_values)
  case option_name.upcase
  when 'FLOW_CONTROL'
    @flow_control = option_values[0]
  when 'DATA_BITS'
    @data_bits = option_values[0].to_i
  when 'STRUCT'
    @struct << option_values
  end
end