Module: Modbus::Cli::CommandsCommon

Included in:
DumpCommand, ReadCommand, WriteCommand
Defined in:
lib/modbus-cli/commands_common.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

MAX_WRITE_COILS =
800
MAX_WRITE_WORDS =
123
DEFAULT_SLAVE =
1

Instance Method Summary collapse

Instance Method Details

#addr_formatObject



119
120
121
122
123
124
125
126
127
# File 'lib/modbus-cli/commands_common.rb', line 119

def addr_format
  if schneider?
    :schneider
  elsif modicon?
    :modicon
  else
    address[:format]
  end
end

#addr_offsetObject



69
70
71
# File 'lib/modbus-cli/commands_common.rb', line 69

def addr_offset
  address[:offset]
end

#addr_typeObject



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/modbus-cli/commands_common.rb', line 73

def addr_type
  if int?
    :int
  elsif dword?
    :dword
  elsif float?
    :float
  elsif word?
    :word
  else
    address[:datatype]
  end
end

#data_sizeObject



59
60
61
62
63
64
65
66
# File 'lib/modbus-cli/commands_common.rb', line 59

def data_size
  case addr_type
  when :bit, :word, :int
    1
  when :float, :dword
    2
  end
end

#modicon_match(address) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/modbus-cli/commands_common.rb', line 107

def modicon_match(address)
  if address.match /^\d+$/ 
    offset = address.to_i
    case offset
    when 1..99999
      {:offset => offset - 1, :datatype => :bit, :format => :modicon}
    when 400001..499999
      {:offset => offset - 400001, :datatype => :word, :format => :modicon}
    end
  end
end

#schneider_match(address) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/modbus-cli/commands_common.rb', line 88

def schneider_match(address)
  schneider_match =  address.match /%M([FWD])?(\d+)/i
  if schneider_match
    {:offset => schneider_match[2].to_i, :format => :schneider}.tap do |result|
      case schneider_match[1]
      when nil
        result[:datatype] = :bit
      when 'W', 'w'
        result[:datatype] = :word
      when 'F', 'f'
        result[:datatype] = :float
      when 'D', 'd'
        result[:datatype] = :dword
      end
    end
  end
end

#sliced_write_coils(sl, offset, data) ⇒ Object



135
136
137
138
139
# File 'lib/modbus-cli/commands_common.rb', line 135

def sliced_write_coils(sl, offset, data)
  (0..(data.count - 1)).each_slice(MAX_WRITE_COILS) do |slice|
    result = sl.write_multiple_coils(slice.first + offset, data.values_at(*slice))
  end
end

#sliced_write_registers(sl, offset, data) ⇒ Object



129
130
131
132
133
# File 'lib/modbus-cli/commands_common.rb', line 129

def sliced_write_registers(sl, offset, data)
  (0..(data.count - 1)).each_slice(MAX_WRITE_WORDS) do |slice|
    result = sl.write_holding_registers(slice.first + offset, data.values_at(*slice))
  end
end