Class: OpenC3::TerminatedProtocol

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

Overview

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

Direct Known Subclasses

CobsProtocol, SlipProtocol, TemplateProtocol

Instance Attribute Summary

Attributes inherited from Protocol

#allow_empty_data, #extra, #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, #protocol_cmd, #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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/openc3/interfaces/protocols/terminated_protocol.rb', line 43

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)
  raise "strip_read_termination must be true or false" if @strip_read_termination != true and @strip_read_termination != false

  super(discard_leading_bytes, sync_pattern, fill_fields, allow_empty_data)
end

Instance Method Details

#write_data(data, extra = nil) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/openc3/interfaces/protocols/terminated_protocol.rb', line 60

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

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