Class: VoltronicRS232

Inherits:
Object
  • Object
show all
Defined in:
lib/voltronic_rs232.rb

Overview

Simple immutable object representing the Voltronic RS232 protocol

@author: Johan van der Vyver

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ VoltronicRS232

:nodoc:



19
20
21
22
23
24
25
26
27
# File 'lib/voltronic_rs232.rb', line 19

def initialize(data) #:nodoc:
  @data = data.to_s.chomp.dup.freeze
  if (@data != @data.encode(::Encoding.find('ASCII'), {invalid: :replace, undef: :replace, replace: ''}))
    raise ArgumentError.new("Input data can only be ASCII")
  end
  @crc = calculate_crc(data.bytes.to_a).map { |b| b.chr }.join.freeze
  @bytes = "#{@data}#{@crc}\r".freeze
  self.freeze
end

Instance Attribute Details

#bytesObject (readonly)

The encoded data that will be transmitted over the wire Format: <DATA><CRC><CR>



17
18
19
# File 'lib/voltronic_rs232.rb', line 17

def bytes
  @bytes
end

#crcObject (readonly)

The calculated CRC for the data



12
13
14
# File 'lib/voltronic_rs232.rb', line 12

def crc
  @crc
end

#dataObject (readonly)

The human readable command to be sent to the device



8
9
10
# File 'lib/voltronic_rs232.rb', line 8

def data
  @data
end

Instance Method Details

#==(other) ⇒ Object

:nodoc:



37
38
39
40
41
42
# File 'lib/voltronic_rs232.rb', line 37

def ==(other) #:nodoc:
  return true if self.equal?(other)
  return false if other.nil?
  return false unless other.respond_to?(:bytes)
  (self.bytes == other.bytes)
end

#inspectObject

:nodoc:



33
34
35
# File 'lib/voltronic_rs232.rb', line 33

def inspect #:nodoc:
  to_s
end

#to_sObject

:nodoc:



29
30
31
# File 'lib/voltronic_rs232.rb', line 29

def to_s #:nodoc:
  "#{self.class.name.to_s.split('::').last}(data: '#{data}')"
end