Class: Modbus::Cli::WriteCommand
Constant Summary
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
#execute ⇒ Object
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/modbus-cli/write_command.rb', line 38
def execute
connect_args =
if connect_timeout
[host, port, {connect_timeout: connect_timeout}]
else
[host, port]
end
ModBus::TCPClient.connect(*connect_args) do |cl|
cl.with_slave(slave) do |sl|
sl.debug = true if debug?
sl.read_retry_timeout = timeout if timeout
case addr_type
when :bit
write_coils sl
when :word, :int
write_words sl
when :float
write_floats sl
when :dword
write_dwords sl
end
end
end
end
|
#int_parameter(vv, min, max) ⇒ Object
85
86
87
88
89
|
# File 'lib/modbus-cli/write_command.rb', line 85
def int_parameter(vv, min, max)
Integer(vv).tap do |v|
raise ArgumentError, "Value should be in the range #{min}..#{max}" unless (min..max).member? v
end
end
|
#pack_and_write(sl, format) ⇒ Object
80
81
82
83
|
# File 'lib/modbus-cli/write_command.rb', line 80
def pack_and_write(sl, format)
sliced_write_registers(sl, addr_offset, values_list.reverse.pack("#{format}*").unpack('n*').reverse)
end
|
#write_coils(sl) ⇒ Object
64
65
66
|
# File 'lib/modbus-cli/write_command.rb', line 64
def write_coils(sl)
sliced_write_coils sl, addr_offset, values_list
end
|
#write_dwords(sl) ⇒ Object
76
77
78
|
# File 'lib/modbus-cli/write_command.rb', line 76
def write_dwords(sl)
pack_and_write sl, 'N'
end
|
#write_floats(sl) ⇒ Object
72
73
74
|
# File 'lib/modbus-cli/write_command.rb', line 72
def write_floats(sl)
pack_and_write sl, 'g'
end
|
#write_words(sl) ⇒ Object
68
69
70
|
# File 'lib/modbus-cli/write_command.rb', line 68
def write_words(sl)
sliced_write_registers sl, addr_offset, values_list.pack('S*').unpack('S*')
end
|