class Modbus::ProtocolData

Helper class for dealing with the modbus wire format.

Public Class Methods

new(buffer = nil) click to toggle source

Initializes a new ProtocolData instance. Unpacks a buffer string if given.

@param buffer [String, Array] The buffer data. If it’s a String, it’s automatically unpacked.

Calls superclass method
# File lib/modbus/connection/protocol_data.rb, line 17
def initialize(buffer = nil)
  case buffer
  when String
    super buffer.unpack('C*')
  when Array
    super buffer
  end
end

Public Instance Methods

push_byte(byte) click to toggle source

Interprets a value as a byte (network byte order) and pushes the byte to the end of the array.

@param byte [Integer] The value to push.

# File lib/modbus/connection/protocol_data.rb, line 61
def push_byte(byte)
  self.concat [byte].pack('C').unpack('C')
end
push_word(word) click to toggle source

Interprets a value as a word (network byte order) and pushes two bytes to the end of the array.

@param word [Integer] The value to push.

# File lib/modbus/connection/protocol_data.rb, line 42
def push_word(word)
  self.concat [word].pack('n').unpack('C2')
end
shift_byte() click to toggle source

Shifts one bytes off the from front of the array.

@return [Integer] The shifted byte.

# File lib/modbus/connection/protocol_data.rb, line 51
def shift_byte
  # self.shift
  self.slice!(0,1).first
end
shift_word() click to toggle source

Shifts two bytes off the from front of the array and interprets them as a word (network byte order).

@return [Integer, NilClass] The shifted word or nil if there are not enough bytes.

# File lib/modbus/connection/protocol_data.rb, line 31
def shift_word
  return nil if size < 2
  # self.shift(2).pack('C2').unpack('n').first
  self.slice!(0,2).pack('C2').unpack('n').first
end
to_buffer() click to toggle source

Converts the array data into a string.

@return [String] The data string (frozen).

# File lib/modbus/connection/protocol_data.rb, line 70
def to_buffer
  self.pack('C*').freeze
end