class Modbus::ProtocolData
Helper class for dealing with the modbus wire format.
Public Class Methods
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.
# 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
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
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
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
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
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