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_areaObject



87
88
89
# File 'lib/modbus-cli/commands_common.rb', line 87

def addr_area
  address[:area]
end

#addr_formatObject



137
138
139
140
141
142
143
144
145
# File 'lib/modbus-cli/commands_common.rb', line 137

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



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/modbus-cli/commands_common.rb', line 114

def modicon_match(address)
  if address.match /^\d+$/ 
    offset = address.to_i
    case offset
    when 1..99999
      { :offset => offset - 1,
        :datatype => :bit,
        :format => :modicon }
    # 100001..199999 inputs are not supported
    when 300001..399999
      { :offset => offset - 300001,
        :datatype => :word,
        :format => :modicon,
        :area => :input_registers }
    when 400001..499999
      { :offset => offset - 400001,
        :datatype => :word,
        :format => :modicon,
        :area => :holding_registers }
    end
  end
end

#schneider_match(address) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/modbus-cli/commands_common.rb', line 92

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
        result[:area] = :holding_registers
      when 'F', 'f'
        result[:datatype] = :float
        result[:area] = :holding_registers
      when 'D', 'd'
        result[:datatype] = :dword
        result[:area] = :holding_registers
      end
    end
  end
end

#sliced_write_coils(sl, offset, data) ⇒ Object



153
154
155
156
157
# File 'lib/modbus-cli/commands_common.rb', line 153

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



147
148
149
150
151
# File 'lib/modbus-cli/commands_common.rb', line 147

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