Class: Modbus::Cli::ReadCommand
Constant Summary
collapse
- MAX_READ_COIL_COUNT =
1000
- MAX_READ_WORD_COUNT =
125
CommandsCommon::DEFAULT_SLAVE, CommandsCommon::MAX_WRITE_COILS, CommandsCommon::MAX_WRITE_WORDS
Instance Method Summary
collapse
address_parameter, connect_timeout_option, datatype_options, debug_option, format_options, host_option, host_parameter, output_option, slave_option, timeout_option
#addr_area, #addr_format, #addr_offset, #addr_type, #data_size, #modicon_match, #schneider_match, #sliced_write_coils, #sliced_write_registers
Instance Method Details
#address_to_s(addr, format = addr_format) ⇒ Object
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
# File 'lib/modbus-cli/read_command.rb', line 156
def address_to_s(addr, format = addr_format)
case format
when :schneider
case addr_type
when :bit
'%M' + addr.to_s
when :word, :int
'%MW' + addr.to_s
when :dword
'%MD' + addr.to_s
when :float
'%MF' + addr.to_s
end
when :modicon
case addr_type
when :bit
(addr + 1).to_s
when :word, :int, :dword, :float
case addr_area
when :input_registers
(addr + 300001).to_s
else
(addr + 400001).to_s
end
end
end
end
|
#execute ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/modbus-cli/read_command.rb', line 76
def execute
connect_lambda =
if connect_timeout
Proc.new { |&block| ModBus::TCPClient.connect(host, port, connect_timeout: connect_timeout, &block) }
else
Proc.new { |&block| ModBus::TCPClient.connect(host, port, &block) }
end
connect_lambda.call do |cl|
cl.with_slave(slave) do |sl|
sl.debug = true if debug?
sl.read_retry_timeout = timeout if timeout
if output then
case addr_type
when :bit
read_coils_to_file(sl)
else
read_words_to_file(sl)
end
else
case addr_type
when :bit
read_coils(sl)
when :int
read_registers(sl, :int => true)
when :word
read_registers(sl)
when :float
read_floats(sl)
when :dword
read_dwords(sl)
end
end
end
end
end
|
#nice_float(str) ⇒ Object
147
148
149
150
151
152
153
154
|
# File 'lib/modbus-cli/read_command.rb', line 147
def nice_float(str)
m = str.match /^(.*[.][0-9])0*$/
if m
m[1]
else
str
end
end
|
#read_and_unpack(sl, format) ⇒ Object
115
116
117
118
|
# File 'lib/modbus-cli/read_command.rb', line 115
def read_and_unpack(sl, format)
read_data_words(sl).reverse.pack('n*').unpack("#{format}*").reverse
end
|
#read_coils(sl) ⇒ Object
69
70
71
72
73
74
|
# File 'lib/modbus-cli/read_command.rb', line 69
def read_coils(sl)
data = read_data_coils(sl)
read_range.zip(data) do |pair|
puts "#{ '%-10s' % address_to_s(pair.first)} #{'%d' % pair.last}"
end
end
|
#read_coils_to_file(sl) ⇒ Object
59
60
61
|
# File 'lib/modbus-cli/read_command.rb', line 59
def read_coils_to_file(sl)
write_data_to_file(read_data_coils(sl))
end
|
#read_data_coils(sl) ⇒ Object
134
135
136
137
138
139
140
|
# File 'lib/modbus-cli/read_command.rb', line 134
def read_data_coils(sl)
result = []
read_range.each_slice(MAX_READ_COIL_COUNT) do |slice|
result += sl.read_coils(slice.first, slice.count)
end
result
end
|
#read_data_words(sl) ⇒ Object
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/modbus-cli/read_command.rb', line 120
def read_data_words(sl)
result = []
read_range.each_slice(MAX_READ_WORD_COUNT) do |slice|
case addr_area
when :input_registers
result += sl.read_input_registers(slice.first, slice.count)
else
result += sl.read_holding_registers(slice.first, slice.count)
end
end
result
end
|
#read_dwords(sl) ⇒ Object
38
39
40
41
42
43
|
# File 'lib/modbus-cli/read_command.rb', line 38
def read_dwords(sl)
dwords = read_and_unpack(sl, 'N')
(0...count).each do |n|
puts "#{ '%-10s' % address_to_s(addr_offset + n * data_size)} #{'%10d' % dwords[n]}"
end
end
|
#read_floats(sl) ⇒ Object
31
32
33
34
35
36
|
# File 'lib/modbus-cli/read_command.rb', line 31
def read_floats(sl)
floats = read_and_unpack(sl, 'g')
(0...count).each do |n|
puts "#{ '%-10s' % address_to_s(addr_offset + n * data_size)} #{nice_float('% 16.8f' % floats[n])}"
end
end
|
#read_range ⇒ Object
142
143
144
|
# File 'lib/modbus-cli/read_command.rb', line 142
def read_range
(addr_offset..(addr_offset + count * data_size - 1))
end
|
#read_registers(sl, options = {}) ⇒ Object
45
46
47
48
49
50
51
52
53
|
# File 'lib/modbus-cli/read_command.rb', line 45
def read_registers(sl, options = {})
data = read_data_words(sl)
if options[:int]
data = data.pack('S').unpack('s')
end
read_range.zip(data).each do |pair|
puts "#{ '%-10s' % address_to_s(pair.first)} #{'%6d' % pair.last}"
end
end
|
#read_words_to_file(sl) ⇒ Object
55
56
57
|
# File 'lib/modbus-cli/read_command.rb', line 55
def read_words_to_file(sl)
write_data_to_file(read_data_words(sl))
end
|
#write_data_to_file(data) ⇒ Object
63
64
65
66
67
|
# File 'lib/modbus-cli/read_command.rb', line 63
def write_data_to_file(data)
File.open(output, 'w') do |file|
file.puts({ :host => host, :port => port, :slave => slave, :offset => address_to_s(addr_offset, :modicon), :data => data }.to_yaml)
end
end
|