rmodbus 1.0.0-java
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/NEWS.md +52 -0
- data/README.md +87 -0
- data/Rakefile +31 -0
- data/examples/perfomance_rtu.rb +56 -0
- data/examples/perfomance_tcp.rb +55 -0
- data/examples/use_rtu_via_tcp_modbus.rb +22 -0
- data/examples/use_tcp_modbus.rb +23 -0
- data/lib/rmodbus/client.rb +91 -0
- data/lib/rmodbus/common.rb +50 -0
- data/lib/rmodbus/errors.rb +50 -0
- data/lib/rmodbus/ext.rb +88 -0
- data/lib/rmodbus/proxy.rb +54 -0
- data/lib/rmodbus/rtu.rb +140 -0
- data/lib/rmodbus/rtu_client.rb +41 -0
- data/lib/rmodbus/rtu_server.rb +61 -0
- data/lib/rmodbus/rtu_slave.rb +59 -0
- data/lib/rmodbus/rtu_via_tcp_client.rb +41 -0
- data/lib/rmodbus/rtu_via_tcp_server.rb +50 -0
- data/lib/rmodbus/rtu_via_tcp_slave.rb +58 -0
- data/lib/rmodbus/server.rb +142 -0
- data/lib/rmodbus/slave.rb +268 -0
- data/lib/rmodbus/sp.rb +45 -0
- data/lib/rmodbus/tcp.rb +49 -0
- data/lib/rmodbus/tcp_client.rb +39 -0
- data/lib/rmodbus/tcp_server.rb +61 -0
- data/lib/rmodbus/tcp_slave.rb +64 -0
- data/lib/rmodbus/version.rb +17 -0
- data/lib/rmodbus.rb +35 -0
- data/spec/client_spec.rb +31 -0
- data/spec/exception_spec.rb +116 -0
- data/spec/ext_spec.rb +46 -0
- data/spec/logging_spec.rb +65 -0
- data/spec/proxy_spec.rb +73 -0
- data/spec/read_rtu_response_spec.rb +91 -0
- data/spec/rtu_client_spec.rb +74 -0
- data/spec/rtu_server_spec.rb +29 -0
- data/spec/rtu_via_tcp_client_spec.rb +78 -0
- data/spec/slave_spec.rb +55 -0
- data/spec/tcp_client_spec.rb +81 -0
- data/spec/tcp_server_spec.rb +99 -0
- metadata +165 -0
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'rmodbus'
|
2
|
+
|
3
|
+
describe TCPClient do
|
4
|
+
describe "method 'query'" do
|
5
|
+
before(:each) do
|
6
|
+
@uid = 1
|
7
|
+
@sock = mock("Socket")
|
8
|
+
@adu = "\000\001\000\000\000\001\001"
|
9
|
+
|
10
|
+
TCPSocket.should_receive(:new).with('127.0.0.1', 1502).and_return(@sock)
|
11
|
+
@sock.stub!(:read).with(0).and_return('')
|
12
|
+
@cl = ModBus::TCPClient.new('127.0.0.1', 1502)
|
13
|
+
@slave = @cl.with_slave(@uid)
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should send valid MBAP Header' do
|
17
|
+
@adu[0,2] = @slave.transaction.next.to_word
|
18
|
+
@sock.should_receive(:write).with(@adu)
|
19
|
+
@sock.should_receive(:read).with(7).and_return(@adu)
|
20
|
+
@slave.query('').should == nil
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should throw exception if get other transaction' do
|
24
|
+
@adu[0,2] = @slave.transaction.next.to_word
|
25
|
+
@sock.should_receive(:write).with(@adu)
|
26
|
+
@sock.should_receive(:read).with(7).and_return("\000\002\000\000\000\001" + @uid.chr)
|
27
|
+
begin
|
28
|
+
@slave.query('').should == nil
|
29
|
+
rescue Exception => ex
|
30
|
+
ex.class.should == Errors::ModBusException
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'should return only data from PDU' do
|
35
|
+
request = "\x3\x0\x6b\x0\x3"
|
36
|
+
response = "\x3\x6\x2\x2b\x0\x0\x0\x64"
|
37
|
+
@adu = @slave.transaction.next.to_word + "\x0\x0\x0\x9" + @uid.chr + request
|
38
|
+
@sock.should_receive(:write).with(@adu[0,4] + "\0\6" + @uid.chr + request)
|
39
|
+
@sock.should_receive(:read).with(7).and_return(@adu[0,7])
|
40
|
+
@sock.should_receive(:read).with(8).and_return(response)
|
41
|
+
|
42
|
+
@slave.query(request).should == response[2..-1]
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should sugar connect method' do
|
46
|
+
ipaddr, port = '127.0.0.1', 502
|
47
|
+
TCPSocket.should_receive(:new).with(ipaddr, port).and_return(@sock)
|
48
|
+
@sock.should_receive(:closed?).and_return(false)
|
49
|
+
@sock.should_receive(:close)
|
50
|
+
TCPClient.connect(ipaddr, port) do |cl|
|
51
|
+
cl.ipaddr.should == ipaddr
|
52
|
+
cl.port.should == port
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'should have closed? method' do
|
57
|
+
@sock.should_receive(:closed?).and_return(false)
|
58
|
+
@cl.closed?.should == false
|
59
|
+
|
60
|
+
@sock.should_receive(:closed?).and_return(false)
|
61
|
+
@sock.should_receive(:close)
|
62
|
+
|
63
|
+
@cl.close
|
64
|
+
|
65
|
+
@sock.should_receive(:closed?).and_return(true)
|
66
|
+
@cl.closed?.should == true
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'should give slave object in block' do
|
70
|
+
@cl.with_slave(1) do |slave|
|
71
|
+
slave.uid = 1
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should tune connection timeout" do
|
77
|
+
timeout(0.5) do
|
78
|
+
lambda { ModBus::TCPClient.new('81.123.231.11', 1999, :connect_timeout => 0.1) }.should raise_error(ModBusTimeout)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'rmodbus'
|
2
|
+
|
3
|
+
describe TCPServer do
|
4
|
+
before do
|
5
|
+
@server = ModBus::TCPServer.new(8502,1)
|
6
|
+
@server.coils = [1,0,1,1]
|
7
|
+
@server.discrete_inputs = [1,1,0,0]
|
8
|
+
@server.holding_registers = [1,2,3,4]
|
9
|
+
@server.input_registers = [1,2,3,4]
|
10
|
+
@server.start
|
11
|
+
@cl = ModBus::TCPClient.new('127.0.0.1', 8502)
|
12
|
+
@slave = @cl.with_slave(1)
|
13
|
+
@slave.read_retries = 0
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should silent if UID has mismatched" do
|
17
|
+
@cl.close
|
18
|
+
ModBus::TCPClient.connect('127.0.0.1', 8502) do |cl|
|
19
|
+
lambda { cl.with_slave(2).read_coils(1,3) }.should raise_exception(
|
20
|
+
ModBus::Errors::ModBusException,
|
21
|
+
"Server did not respond"
|
22
|
+
)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should send exception if function not supported" do
|
27
|
+
lambda { @slave.query('0x43') }.should raise_exception(
|
28
|
+
ModBus::Errors::IllegalFunction,
|
29
|
+
"The function code received in the query is not an allowable action for the server"
|
30
|
+
)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should send exception if quanity of out more 0x7d" do
|
34
|
+
lambda { @slave.read_coils(0, 0x7e) }.should raise_exception(
|
35
|
+
ModBus::Errors::IllegalDataValue,
|
36
|
+
"A value contained in the query data field is not an allowable value for server"
|
37
|
+
)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should send exception if addr not valid" do
|
41
|
+
lambda { @slave.read_coils(2, 8) }.should raise_exception(
|
42
|
+
ModBus::Errors::IllegalDataAddress,
|
43
|
+
"The data address received in the query is not an allowable address for the server"
|
44
|
+
)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should calc a many requests" do
|
48
|
+
@slave.read_coils(1,2)
|
49
|
+
@slave.write_multiple_registers(0,[9,9,9,])
|
50
|
+
@slave.read_holding_registers(0,3).should == [9,9,9]
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should supported function 'read coils'" do
|
54
|
+
@slave.read_coils(0,3).should == @server.coils[0,3]
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should supported function 'read discrete inputs'" do
|
58
|
+
@slave.read_discrete_inputs(1,3).should == @server.discrete_inputs[1,3]
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should supported function 'read holding registers'" do
|
62
|
+
@slave.read_holding_registers(0,3).should == @server.holding_registers[0,3]
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should supported function 'read input registers'" do
|
66
|
+
@slave.read_input_registers(2,2).should == @server.input_registers[2,2]
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should supported function 'write single coil'" do
|
70
|
+
@server.coils[3] = 0
|
71
|
+
@slave.write_single_coil(3,1)
|
72
|
+
@server.coils[3].should == 1
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should supported function 'write single register'" do
|
76
|
+
@server.holding_registers[3] = 25
|
77
|
+
@slave.write_single_register(3,35)
|
78
|
+
@server.holding_registers[3].should == 35
|
79
|
+
end
|
80
|
+
|
81
|
+
it "should supported function 'write multiple coils'" do
|
82
|
+
@server.coils = [1,1,1,0, 0,0,0,0, 0,0,0,0, 0,1,1,1]
|
83
|
+
@slave.write_multiple_coils(3, [1, 0,1,0,1, 0,1,0,1])
|
84
|
+
@server.coils.should == [1,1,1,1, 0,1,0,1, 0,1,0,1, 0,1,1,1]
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should supported function 'write multiple registers'" do
|
88
|
+
@server.holding_registers = [1,2,3,4,5,6,7,8,9]
|
89
|
+
@slave.write_multiple_registers(3,[1,2,3,4,5])
|
90
|
+
@server.holding_registers.should == [1,2,3,1,2,3,4,5,9]
|
91
|
+
end
|
92
|
+
|
93
|
+
after do
|
94
|
+
@cl.close
|
95
|
+
@server.stop unless @server.stopped?
|
96
|
+
while GServer.in_service?(8502)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
metadata
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rmodbus
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 1.0.0
|
6
|
+
platform: java
|
7
|
+
authors:
|
8
|
+
- A.Timin, J. Sanders, K. Reynolds
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-06-27 00:00:00 +06:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: rake
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: "0"
|
25
|
+
type: :development
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
prerelease: false
|
30
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: "0"
|
36
|
+
type: :development
|
37
|
+
version_requirements: *id002
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: rspec
|
40
|
+
prerelease: false
|
41
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
type: :development
|
48
|
+
version_requirements: *id003
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: rcov
|
51
|
+
prerelease: false
|
52
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
type: :development
|
59
|
+
version_requirements: *id004
|
60
|
+
- !ruby/object:Gem::Dependency
|
61
|
+
name: yard
|
62
|
+
prerelease: false
|
63
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: "0"
|
69
|
+
type: :development
|
70
|
+
version_requirements: *id005
|
71
|
+
- !ruby/object:Gem::Dependency
|
72
|
+
name: rdiscount
|
73
|
+
prerelease: false
|
74
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: "0"
|
80
|
+
type: :development
|
81
|
+
version_requirements: *id006
|
82
|
+
description:
|
83
|
+
email: atimin@gmail.com
|
84
|
+
executables: []
|
85
|
+
|
86
|
+
extensions: []
|
87
|
+
|
88
|
+
extra_rdoc_files:
|
89
|
+
- README.md
|
90
|
+
- NEWS.md
|
91
|
+
files:
|
92
|
+
- lib/rmodbus.rb
|
93
|
+
- lib/rmodbus/version.rb
|
94
|
+
- lib/rmodbus/errors.rb
|
95
|
+
- lib/rmodbus/ext.rb
|
96
|
+
- lib/rmodbus/rtu_via_tcp_client.rb
|
97
|
+
- lib/rmodbus/sp.rb
|
98
|
+
- lib/rmodbus/tcp.rb
|
99
|
+
- lib/rmodbus/common.rb
|
100
|
+
- lib/rmodbus/client.rb
|
101
|
+
- lib/rmodbus/slave.rb
|
102
|
+
- lib/rmodbus/tcp_server.rb
|
103
|
+
- lib/rmodbus/tcp_client.rb
|
104
|
+
- lib/rmodbus/proxy.rb
|
105
|
+
- lib/rmodbus/rtu_slave.rb
|
106
|
+
- lib/rmodbus/rtu_server.rb
|
107
|
+
- lib/rmodbus/rtu.rb
|
108
|
+
- lib/rmodbus/rtu_client.rb
|
109
|
+
- lib/rmodbus/rtu_via_tcp_server.rb
|
110
|
+
- lib/rmodbus/server.rb
|
111
|
+
- lib/rmodbus/rtu_via_tcp_slave.rb
|
112
|
+
- lib/rmodbus/tcp_slave.rb
|
113
|
+
- examples/perfomance_tcp.rb
|
114
|
+
- examples/use_tcp_modbus.rb
|
115
|
+
- examples/use_rtu_via_tcp_modbus.rb
|
116
|
+
- examples/perfomance_rtu.rb
|
117
|
+
- spec/tcp_server_spec.rb
|
118
|
+
- spec/logging_spec.rb
|
119
|
+
- spec/rtu_via_tcp_client_spec.rb
|
120
|
+
- spec/rtu_server_spec.rb
|
121
|
+
- spec/tcp_client_spec.rb
|
122
|
+
- spec/exception_spec.rb
|
123
|
+
- spec/rtu_client_spec.rb
|
124
|
+
- spec/slave_spec.rb
|
125
|
+
- spec/client_spec.rb
|
126
|
+
- spec/read_rtu_response_spec.rb
|
127
|
+
- spec/proxy_spec.rb
|
128
|
+
- spec/ext_spec.rb
|
129
|
+
- Rakefile
|
130
|
+
- README.md
|
131
|
+
- NEWS.md
|
132
|
+
has_rdoc: true
|
133
|
+
homepage: http://rmodbus.heroku.com
|
134
|
+
licenses: []
|
135
|
+
|
136
|
+
post_install_message:
|
137
|
+
rdoc_options:
|
138
|
+
- --title
|
139
|
+
- RModBus
|
140
|
+
- --inline-source
|
141
|
+
- --main
|
142
|
+
- README.md
|
143
|
+
require_paths:
|
144
|
+
- lib
|
145
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
146
|
+
none: false
|
147
|
+
requirements:
|
148
|
+
- - ">="
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
version: "0"
|
151
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
152
|
+
none: false
|
153
|
+
requirements:
|
154
|
+
- - ">="
|
155
|
+
- !ruby/object:Gem::Version
|
156
|
+
version: "0"
|
157
|
+
requirements: []
|
158
|
+
|
159
|
+
rubyforge_project:
|
160
|
+
rubygems_version: 1.5.1
|
161
|
+
signing_key:
|
162
|
+
specification_version: 3
|
163
|
+
summary: RModBus - free implementation of protocol ModBus
|
164
|
+
test_files: []
|
165
|
+
|