Class: Cosmos::TerminatedProtocol

Inherits:
BurstProtocol show all
Defined in:
lib/cosmos/interfaces/protocols/terminated_protocol.rb

Overview

Protocol which delineates packets using termination characters at the end of the data.

Direct Known Subclasses

TemplateProtocol

Instance Attribute Summary

Attributes inherited from Protocol

#allow_empty_data, #interface

Instance Method Summary collapse

Methods inherited from BurstProtocol

#handle_sync_pattern, #log_discard, #read_data, #reset, #write_packet

Methods inherited from Protocol

#connect_reset, #disconnect_reset, #post_write_interface, #read_data, #read_packet, #reset, #write_packet

Constructor Details

#initialize(write_termination_characters, read_termination_characters, strip_read_termination = true, discard_leading_bytes = 0, sync_pattern = nil, fill_fields = false, allow_empty_data = nil) ⇒ TerminatedProtocol

Returns a new instance of TerminatedProtocol.

Parameters:

  • write_termination_characters (String)

    The characters to write after writing the Packet buffer. Must be given as a hexadecimal string such as '0xABCD'.

  • read_termination_characters (String)

    The characters at the end of the data which delineate the end of a Packet. Must be given as a hexadecimal string such as '0xABCD'.

  • strip_read_termination (Boolean) (defaults to: true)

    Whether to remove the read_termination_characters before turning the data into a Packet.

  • allow_empty_data (true/false/nil) (defaults to: nil)

    See Protocol#initialize

  • discard_leading_bytes (Integer) (defaults to: 0)

    The number of bytes to discard from the binary data after reading. Note that this is often used to remove a sync pattern from the final packet data.

  • sync_pattern (String) (defaults to: nil)

    String representing a hex number ("0x1234") that will be searched for in the raw data. Bytes encountered before this pattern is found are discarded.

  • fill_fields (Boolean) (defaults to: false)

    Fill any required fields when writing packets



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cosmos/interfaces/protocols/terminated_protocol.rb', line 40

def initialize(
  write_termination_characters,
  read_termination_characters,
  strip_read_termination = true,
  discard_leading_bytes = 0,
  sync_pattern = nil,
  fill_fields = false,
  allow_empty_data = nil
)
  @write_termination_characters = write_termination_characters.hex_to_byte_string
  @read_termination_characters = read_termination_characters.hex_to_byte_string
  @strip_read_termination = ConfigParser.handle_true_false(strip_read_termination)

  super(discard_leading_bytes, sync_pattern, fill_fields, allow_empty_data)
end

Instance Method Details

#write_data(data) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/cosmos/interfaces/protocols/terminated_protocol.rb', line 56

def write_data(data)
  raise "Packet contains termination characters!" if data.index(@write_termination_characters)

  data = super(data)
  @write_termination_characters.each_byte do |byte|
    data << byte
  end
  return data
end