Class: OpenC3::CobsProtocol
- Inherits:
-
TerminatedProtocol
- Object
- Protocol
- BurstProtocol
- TerminatedProtocol
- OpenC3::CobsProtocol
- Defined in:
- lib/openc3/interfaces/protocols/cobs_protocol.rb
Overview
Usage in plugin.txt:
INTERFACE …
PROTOCOL READ_WRITE CobsProtocol
Instance Attribute Summary
Attributes inherited from Protocol
#allow_empty_data, #extra, #interface
Instance Method Summary collapse
-
#initialize(allow_empty_data = nil) ⇒ CobsProtocol
constructor
A new instance of CobsProtocol.
- #read_data(data, extra = nil) ⇒ Object
- #write_data(data, extra = nil) ⇒ Object
Methods inherited from BurstProtocol
#handle_sync_pattern, #log_discard, #reduce_to_single_packet, #reset, #write_packet
Methods inherited from Protocol
#connect_reset, #disconnect_reset, #post_write_interface, #protocol_cmd, #read_packet, #reset, #write_packet
Constructor Details
#initialize(allow_empty_data = nil) ⇒ CobsProtocol
Returns a new instance of CobsProtocol.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/openc3/interfaces/protocols/cobs_protocol.rb', line 38 def initialize(allow_empty_data = nil) strip_read_termination = true discard_leading_bytes = 0 sync_pattern = nil fill_fields = false # Handled in write_data below super( "", # Write termination handled in write_data below "00", strip_read_termination, discard_leading_bytes, sync_pattern, fill_fields, allow_empty_data ) end |
Instance Method Details
#read_data(data, extra = nil) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/openc3/interfaces/protocols/cobs_protocol.rb', line 56 def read_data(data, extra = nil) data, extra = super(data, extra) return data, extra if data.length <= 0 or Symbol === data result_data = '' while data.length > 1 # Read the offset to the next zero byte # Note: This may be off the end of the data. If so, the packet is over zero_offset = data[0].unpack('C')[0] if zero_offset == 0xFF # No zeros in this segment result_data << data[1..254] data = data[255..-1] elsif zero_offset <= 1 # End of data or 1 zero result_data << "\x00" data = data[1..-1] else # Mid range zero or end of packet result_data << data[1..(zero_offset - 1)] data = data[zero_offset..-1] result_data << "\x00" if data.length >= 1 end end return result_data, extra end |
#write_data(data, extra = nil) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/openc3/interfaces/protocols/cobs_protocol.rb', line 81 def write_data(data, extra = nil) # Intentionally not calling super() need_insert = false result_data = '' while data.length > 0 index = data.index("\x00") if (index and index > 253) or (index.nil? and data.length >= 254) result_data << "\xFF" result_data << data[0..253] data = data[254..-1] need_insert = false else # index <= 253 or (index.nil? and data.length < 254) if index result_data << [index + 1].pack('C') if index >= 1 result_data << data[0..(index - 1)] end data = data[(index + 1)..-1] need_insert = true else result_data << [data.length + 1].pack('C') result_data << data data = '' need_insert = false end end end # Handle a zero at the end of the packet result_data << "\x01" if need_insert # Terminate message with 0x00 result_data << "\x00" return result_data, extra end |