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, 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
# File 'lib/modbus-cli/read_command.rb', line 147
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
case addr_area
when :input_registers
(addr + 300001).to_s
else (addr + 400001).to_s
end
end
end
end
|
#execute ⇒ Object
75
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
|
# File 'lib/modbus-cli/read_command.rb', line 75
def execute
ModBus::TCPClient.connect(host, port) 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
138
139
140
141
142
143
144
145
|
# File 'lib/modbus-cli/read_command.rb', line 138
def nice_float(str)
m = str.match /^(.*[.][0-9])0*$/
if m
m[1]
else
str
end
end
|
#read_and_unpack(sl, format) ⇒ Object
106
107
108
109
|
# File 'lib/modbus-cli/read_command.rb', line 106
def read_and_unpack(sl, format)
read_data_words(sl).reverse.pack('n*').unpack("#{format}*").reverse
end
|
#read_coils(sl) ⇒ Object
68
69
70
71
72
73
|
# File 'lib/modbus-cli/read_command.rb', line 68
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
58
59
60
|
# File 'lib/modbus-cli/read_command.rb', line 58
def read_coils_to_file(sl)
write_data_to_file(read_data_coils(sl))
end
|
#read_data_coils(sl) ⇒ Object
125
126
127
128
129
130
131
|
# File 'lib/modbus-cli/read_command.rb', line 125
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
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/modbus-cli/read_command.rb', line 111
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
37
38
39
40
41
42
|
# File 'lib/modbus-cli/read_command.rb', line 37
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
30
31
32
33
34
35
|
# File 'lib/modbus-cli/read_command.rb', line 30
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
133
134
135
|
# File 'lib/modbus-cli/read_command.rb', line 133
def read_range
(addr_offset..(addr_offset + count * data_size - 1))
end
|
#read_registers(sl, options = {}) ⇒ Object
44
45
46
47
48
49
50
51
52
|
# File 'lib/modbus-cli/read_command.rb', line 44
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
54
55
56
|
# File 'lib/modbus-cli/read_command.rb', line 54
def read_words_to_file(sl)
write_data_to_file(read_data_words(sl))
end
|
#write_data_to_file(data) ⇒ Object
62
63
64
65
66
|
# File 'lib/modbus-cli/read_command.rb', line 62
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
|