rmodbus 0.5.0 → 1.0.0
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 +22 -36
- data/examples/perfomance_rtu.rb +35 -37
- data/examples/perfomance_tcp.rb +36 -38
- data/examples/use_rtu_via_tcp_modbus.rb +8 -5
- data/examples/use_tcp_modbus.rb +10 -6
- data/lib/rmodbus/client.rb +52 -174
- data/lib/rmodbus/common.rb +45 -18
- data/lib/rmodbus/{exceptions.rb → errors.rb} +3 -0
- data/lib/rmodbus/ext.rb +25 -2
- data/lib/rmodbus/proxy.rb +54 -0
- data/lib/rmodbus/{crc16.rb → rtu.rb} +73 -2
- data/lib/rmodbus/rtu_client.rb +20 -116
- data/lib/rmodbus/rtu_server.rb +28 -57
- data/lib/rmodbus/rtu_slave.rb +59 -0
- data/lib/rmodbus/rtu_via_tcp_client.rb +22 -86
- data/lib/rmodbus/rtu_via_tcp_server.rb +31 -95
- data/lib/rmodbus/rtu_via_tcp_slave.rb +58 -0
- data/lib/rmodbus/{parsers.rb → server.rb} +24 -15
- 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 +19 -88
- data/lib/rmodbus/tcp_server.rb +16 -19
- data/lib/rmodbus/tcp_slave.rb +64 -0
- data/lib/rmodbus/version.rb +17 -0
- data/lib/rmodbus.rb +20 -4
- data/spec/client_spec.rb +19 -45
- data/spec/exception_spec.rb +26 -27
- data/spec/ext_spec.rb +24 -1
- data/spec/logging_spec.rb +31 -37
- data/spec/proxy_spec.rb +73 -0
- data/spec/read_rtu_response_spec.rb +2 -4
- data/spec/rtu_client_spec.rb +17 -19
- data/spec/rtu_server_spec.rb +1 -3
- data/spec/rtu_via_tcp_client_spec.rb +69 -63
- data/spec/slave_spec.rb +55 -0
- data/spec/tcp_client_spec.rb +77 -69
- data/spec/tcp_server_spec.rb +34 -49
- metadata +123 -37
- data/AUTHORS +0 -3
- data/ChangeLog +0 -82
- data/LICENSE +0 -675
- data/README +0 -53
- data/examples/add_new_function.rb +0 -19
data/spec/client_spec.rb
CHANGED
@@ -1,57 +1,31 @@
|
|
1
|
-
require 'rmodbus
|
2
|
-
|
1
|
+
require 'rmodbus'
|
3
2
|
include ModBus
|
4
3
|
|
5
4
|
describe Client do
|
6
|
-
|
7
5
|
before do
|
8
|
-
@
|
9
|
-
@cl_mb.stub!(:query).and_return('')
|
10
|
-
end
|
11
|
-
|
12
|
-
it "should support function 'read coils'" do
|
13
|
-
@cl_mb.should_receive(:query).with("\x1\x0\x13\x0\x13").and_return("\xcd\x6b\x5")
|
14
|
-
@cl_mb.read_coils(0x13,0x13).should == [1,0,1,1, 0,0,1,1, 1,1,0,1, 0,1,1,0, 1,0,1]
|
6
|
+
@cl = Client.new
|
15
7
|
end
|
16
8
|
|
17
|
-
it "should
|
18
|
-
@
|
19
|
-
|
9
|
+
it "should give object provider for slave" do
|
10
|
+
slave = @cl.with_slave(1)
|
11
|
+
slave.uid.should eq(1)
|
20
12
|
end
|
21
13
|
|
22
|
-
it "should
|
23
|
-
@
|
24
|
-
|
14
|
+
it "should give object provider for slave in block" do
|
15
|
+
@cl.with_slave(1) do |slave|
|
16
|
+
slave.uid.should eq(1)
|
17
|
+
end
|
25
18
|
end
|
26
|
-
|
27
|
-
it "should
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
it "should support function 'write single coil'" do
|
33
|
-
@cl_mb.should_receive(:query).with("\x5\x0\xac\xff\x0").and_return("\xac\xff\x00")
|
34
|
-
@cl_mb.write_single_coil(0xac,0x1).should == @cl_mb
|
19
|
+
|
20
|
+
it "should connect with TCP server" do
|
21
|
+
Client.connect do |cl|
|
22
|
+
cl.should be_instance_of(Client)
|
23
|
+
end
|
35
24
|
end
|
36
|
-
|
37
|
-
it "
|
38
|
-
|
39
|
-
|
25
|
+
|
26
|
+
it ":new alias :connect" do
|
27
|
+
Client.new do |cl|
|
28
|
+
cl.should be_instance_of(Client)
|
29
|
+
end
|
40
30
|
end
|
41
|
-
|
42
|
-
it "should support function 'write multiple coils'" do
|
43
|
-
@cl_mb.should_receive(:query).with("\xf\x0\x13\x0\xa\x2\xcd\x1").and_return("\x13\x0\xa")
|
44
|
-
@cl_mb.write_multiple_coils(0x13,[1,0,1,1, 0,0,1,1, 1,0]).should == @cl_mb
|
45
|
-
end
|
46
|
-
|
47
|
-
it "should support function 'write multiple registers'" do
|
48
|
-
@cl_mb.should_receive(:query).with("\x10\x0\x1\x0\x3\x6\x0\xa\x1\x2\xf\xf").and_return("\x1\x0\x3")
|
49
|
-
@cl_mb.write_multiple_registers(0x1,[0x000a,0x0102, 0xf0f]).should == @cl_mb
|
50
|
-
end
|
51
|
-
|
52
|
-
it "should support function 'mask write register'" do
|
53
|
-
@cl_mb.should_receive(:query).with("\x16\x0\x4\x0\xf2\x0\2").and_return("\x4\x0\xf2\x0\x2")
|
54
|
-
@cl_mb.mask_write_register(0x4, 0xf2, 0x2).should == @cl_mb
|
55
|
-
end
|
56
|
-
|
57
31
|
end
|
data/spec/exception_spec.rb
CHANGED
@@ -1,10 +1,8 @@
|
|
1
1
|
require 'rmodbus'
|
2
2
|
|
3
|
-
include ModBus
|
4
3
|
include ModBus::Errors
|
5
4
|
|
6
5
|
describe ModBus::TCPClient do
|
7
|
-
|
8
6
|
before(:all) do
|
9
7
|
@srv = ModBus::TCPServer.new(1502, 1)
|
10
8
|
@srv.coils = [0] * 8
|
@@ -13,100 +11,101 @@ describe ModBus::TCPClient do
|
|
13
11
|
@srv.input_registers = [0] * 8
|
14
12
|
@srv.start
|
15
13
|
|
16
|
-
@cl =TCPClient.new('127.0.0.1', 1502
|
14
|
+
@cl = TCPClient.new('127.0.0.1', 1502)
|
15
|
+
@slave = @cl.with_slave(1)
|
17
16
|
end
|
18
17
|
|
19
18
|
# Read coil status
|
20
19
|
it "should read coil status" do
|
21
|
-
@
|
20
|
+
@slave.read_coils(0, 4).should == [0] * 4
|
22
21
|
end
|
23
22
|
|
24
23
|
it "should raise exception if illegal data address" do
|
25
|
-
lambda { @
|
24
|
+
lambda { @slave.read_coils(501, 34) }.should raise_error(IllegalDataAddress)
|
26
25
|
end
|
27
26
|
|
28
27
|
it "should raise exception if too many data" do
|
29
|
-
lambda { @
|
28
|
+
lambda { @slave.read_coils(0, 0x07D1) }.should raise_error(IllegalDataValue)
|
30
29
|
end
|
31
30
|
|
32
31
|
# Read input status
|
33
32
|
it "should read discrete inputs" do
|
34
|
-
@
|
33
|
+
@slave.read_discrete_inputs(0, 4).should == [0] * 4
|
35
34
|
end
|
36
35
|
|
37
36
|
it "should raise exception if illegal data address" do
|
38
|
-
lambda { @
|
37
|
+
lambda { @slave.read_discrete_inputs(50, 23) }.should raise_error(IllegalDataAddress)
|
39
38
|
end
|
40
39
|
|
41
40
|
it "should raise exception if too many data" do
|
42
|
-
lambda { @
|
41
|
+
lambda { @slave.read_discrete_inputs(0, 0x07D1) }.should raise_error(IllegalDataValue)
|
43
42
|
end
|
44
43
|
|
45
44
|
# Read holding registers
|
46
45
|
it "should read discrete inputs" do
|
47
|
-
@
|
46
|
+
@slave.read_holding_registers(0, 4).should == [0, 0, 0, 0]
|
48
47
|
end
|
49
48
|
|
50
49
|
it "should raise exception if illegal data address" do
|
51
|
-
lambda { @
|
50
|
+
lambda { @slave.read_holding_registers(402, 99) }.should raise_error(IllegalDataAddress)
|
52
51
|
end
|
53
52
|
|
54
53
|
|
55
54
|
it "should raise exception if too many data" do
|
56
|
-
lambda { @
|
55
|
+
lambda { @slave.read_holding_registers(0, 0x007E) }.should raise_error(IllegalDataValue)
|
57
56
|
end
|
58
57
|
|
59
58
|
# Read input registers
|
60
59
|
it "should read discrete inputs" do
|
61
|
-
@
|
60
|
+
@slave.read_input_registers(0, 4).should == [0, 0, 0, 0]
|
62
61
|
end
|
63
62
|
|
64
63
|
it "should raise exception if illegal data address" do
|
65
|
-
lambda { @
|
64
|
+
lambda { @slave.read_input_registers(402, 9) }.should raise_error(IllegalDataAddress)
|
66
65
|
end
|
67
66
|
|
68
67
|
it "should raise exception if too many data" do
|
69
|
-
lambda { @
|
68
|
+
lambda { @slave.read_input_registers(0, 0x007E) }.should raise_error(IllegalDataValue)
|
70
69
|
end
|
71
70
|
|
72
71
|
# Force single coil
|
73
72
|
it "should force single coil" do
|
74
|
-
@
|
75
|
-
@
|
73
|
+
@slave.write_single_coil(4, 1).should == @slave
|
74
|
+
@slave.read_coils(4, 4).should == [1, 0, 0, 0]
|
76
75
|
end
|
77
76
|
|
78
77
|
it "should raise exception if illegal data address" do
|
79
|
-
lambda { @
|
78
|
+
lambda { @slave.write_single_coil(501, true) }.should raise_error(IllegalDataAddress)
|
80
79
|
end
|
81
80
|
|
82
81
|
# Preset single register
|
83
82
|
it "should preset single register" do
|
84
|
-
@
|
85
|
-
@
|
83
|
+
@slave.write_single_register(4, 0x0AA0).should == @slave
|
84
|
+
@slave.read_holding_registers(4, 1).should == [0x0AA0]
|
86
85
|
end
|
87
86
|
|
88
87
|
it "should raise exception if illegal data address" do
|
89
|
-
lambda { @
|
88
|
+
lambda { @slave.write_single_register(501, 0x0AA0) }.should raise_error(IllegalDataAddress)
|
90
89
|
end
|
91
90
|
|
92
91
|
# Force multiple coils
|
93
92
|
it "should force multiple coils" do
|
94
|
-
@
|
95
|
-
@
|
93
|
+
@slave.write_multiple_coils(4, [0,1,0,1]).should == @slave
|
94
|
+
@slave.read_coils(3, 5).should == [0,0,1,0,1]
|
96
95
|
end
|
97
96
|
|
98
97
|
it "should raise exception if illegal data address" do
|
99
|
-
lambda { @
|
98
|
+
lambda { @slave.write_multiple_coils(501, [1,0]) }.should raise_error(IllegalDataAddress)
|
100
99
|
end
|
101
100
|
|
102
101
|
# Preset multiple registers
|
103
102
|
it "should preset multiple registers" do
|
104
|
-
@
|
105
|
-
@
|
103
|
+
@slave.write_multiple_registers(4, [1, 2, 3, 0xAACC]).should == @slave
|
104
|
+
@slave.read_holding_registers(3, 5).should == [0, 1, 2, 3, 0xAACC]
|
106
105
|
end
|
107
106
|
|
108
107
|
it "should raise exception if illegal data address" do
|
109
|
-
lambda { @
|
108
|
+
lambda { @slave.write_multiple_registers(501, [1, 2]) }.should raise_error(IllegalDataAddress)
|
110
109
|
end
|
111
110
|
|
112
111
|
after(:all) do
|
data/spec/ext_spec.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'rmodbus'
|
2
2
|
|
3
3
|
describe Array do
|
4
|
-
|
5
4
|
before do
|
6
5
|
@arr = [1,0,1,1, 0,0,1,1, 1,1,0,1, 0,1,1,0, 1,0,1]
|
7
6
|
@test = [0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0]
|
@@ -19,5 +18,29 @@ describe Array do
|
|
19
18
|
"test".unpack_bits == @test
|
20
19
|
end
|
21
20
|
|
21
|
+
it "should turn an array into 32b ints" do
|
22
|
+
[20342, 17344].to_32i.should == [1136676726]
|
23
|
+
[20342, 17344, 20342, 17344].to_32i.size.should == 2
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should turn an array into 32b floats" do
|
27
|
+
[20342, 17344].to_32f[0].should be_within(0.1).of(384.620788574219)
|
28
|
+
[20342, 17344, 20342, 17344].to_32f.size.should == 2
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should turn an array from 32b ints into 16b ints, big endian" do
|
32
|
+
[1136676726].from_32i.should == [20342, 17344]
|
33
|
+
[1136676726, 1136676725].from_32i.should == [20342, 17344, 20341, 17344]
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should turn an array from 32b floats into 16b ints, big endian" do
|
37
|
+
[384.620788].from_32f.should == [20342, 17344]
|
38
|
+
[384.620788, 384.620788].from_32f.should == [20342, 17344, 20342, 17344]
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should raise exception if uneven number of elements" do
|
42
|
+
lambda { [20342, 17344, 123].to_32f }.should raise_error(StandardError)
|
43
|
+
lambda { [20342, 17344, 123].to_32i }.should raise_error(StandardError)
|
44
|
+
end
|
22
45
|
end
|
23
46
|
|
data/spec/logging_spec.rb
CHANGED
@@ -1,71 +1,65 @@
|
|
1
1
|
require 'rmodbus'
|
2
|
-
|
3
2
|
include ModBus
|
4
3
|
|
5
4
|
describe TCPClient do
|
6
|
-
|
7
|
-
UID = 1
|
8
|
-
|
9
5
|
before(:each) do
|
6
|
+
@uid = 1
|
10
7
|
@sock = mock("Socket")
|
11
8
|
@adu = "\000\001\000\000\000\001\001"
|
12
9
|
|
13
10
|
TCPSocket.should_receive(:new).with('127.0.0.1', 1502).and_return(@sock)
|
14
11
|
@sock.stub!(:read).with(0).and_return('')
|
15
12
|
|
16
|
-
@
|
13
|
+
@slave = TCPClient.new('127.0.0.1', 1502).with_slave(@uid)
|
17
14
|
end
|
18
15
|
|
19
16
|
it 'should log rec\send bytes' do
|
20
17
|
request, response = "\x3\x0\x6b\x0\x3", "\x3\x6\x2\x2b\x0\x0\x0\x64"
|
21
18
|
mock_query(request,response)
|
22
|
-
@
|
19
|
+
@slave.debug = true
|
23
20
|
$stdout.should_receive(:puts).with("Tx (12 bytes): [00][01][00][00][00][06][01][03][00][6b][00][03]")
|
24
21
|
$stdout.should_receive(:puts).with("Rx (15 bytes): [00][01][00][00][00][09][01][03][06][02][2b][00][00][00][64]")
|
25
|
-
@
|
22
|
+
@slave.query(request)
|
26
23
|
end
|
27
24
|
|
28
25
|
it "should don't logging if debug disable" do
|
29
26
|
request, response = "\x3\x0\x6b\x0\x3", "\x3\x6\x2\x2b\x0\x0\x0\x64"
|
30
27
|
mock_query(request,response)
|
31
|
-
@
|
28
|
+
@slave.query(request)
|
32
29
|
end
|
33
30
|
|
34
|
-
|
35
31
|
def mock_query(request, response)
|
36
|
-
@adu = @
|
37
|
-
@sock.should_receive(:write).with(@adu[0,4] + "\0\6" +
|
32
|
+
@adu = @slave.transaction.next.to_word + "\x0\x0\x0\x9" + @uid.chr + request
|
33
|
+
@sock.should_receive(:write).with(@adu[0,4] + "\0\6" + @uid.chr + request)
|
38
34
|
@sock.should_receive(:read).with(7).and_return(@adu[0,7])
|
39
35
|
@sock.should_receive(:read).with(8).and_return(response)
|
40
36
|
end
|
41
|
-
|
42
37
|
end
|
43
38
|
|
44
|
-
|
39
|
+
unless RUBY_PLATFORM == "java"
|
40
|
+
describe RTUClient do
|
41
|
+
before do
|
42
|
+
@sp = mock('Serial port')
|
43
|
+
SerialPort.should_receive(:new).with("/dev/port1", 9600, 7, 2, SerialPort::ODD).and_return(@sp)
|
44
|
+
|
45
|
+
@sp.stub!(:read_timeout=)
|
46
|
+
|
47
|
+
@slave = RTUClient.new("/dev/port1", 9600, :data_bits => 7, :stop_bits => 2, :parity => SerialPort::ODD).with_slave(1)
|
48
|
+
@slave.read_retries = 0
|
49
|
+
end
|
45
50
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
51
|
+
it 'should log rec\send bytes' do
|
52
|
+
request = "\x3\x0\x1\x0\x1"
|
53
|
+
@sp.should_receive(:write).with("\1#{request}\xd5\xca")
|
54
|
+
@sp.should_receive(:read).with(2).and_return("\x1\x3")
|
55
|
+
@sp.should_receive(:read).with(1).and_return("\x2")
|
56
|
+
@sp.should_receive(:read).with(4).and_return("\xff\xff\xb9\xf4")
|
57
|
+
|
58
|
+
@slave.debug = true
|
59
|
+
$stdout.should_receive(:puts).with("Tx (8 bytes): [01][03][00][01][00][01][d5][ca]")
|
60
|
+
$stdout.should_receive(:puts).with("Rx (7 bytes): [01][03][02][ff][ff][b9][f4]")
|
61
|
+
|
62
|
+
@slave.query(request).should == "\xff\xff"
|
63
|
+
end
|
54
64
|
end
|
55
|
-
|
56
|
-
it 'should log rec\send bytes' do
|
57
|
-
request = "\x3\x0\x1\x0\x1"
|
58
|
-
@sp.should_receive(:write).with("\1#{request}\xd5\xca")
|
59
|
-
@sp.should_receive(:read).with(2).and_return("\x1\x3")
|
60
|
-
@sp.should_receive(:read).with(1).and_return("\x2")
|
61
|
-
@sp.should_receive(:read).with(4).and_return("\xff\xff\xb9\xf4")
|
62
|
-
|
63
|
-
@mb_client.debug = true
|
64
|
-
$stdout.should_receive(:puts).with("Tx (8 bytes): [01][03][00][01][00][01][d5][ca]")
|
65
|
-
$stdout.should_receive(:puts).with("Rx (7 bytes): [01][03][02][ff][ff][b9][f4]")
|
66
|
-
|
67
|
-
@mb_client.query(request).should == "\xff\xff"
|
68
|
-
end
|
69
|
-
|
70
65
|
end
|
71
|
-
|
data/spec/proxy_spec.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'rmodbus'
|
2
|
+
|
3
|
+
describe Array do
|
4
|
+
before do
|
5
|
+
@slave = mock('ModBus Slave')
|
6
|
+
@coil_proxy = ModBus::ReadWriteProxy.new(@slave, :coil)
|
7
|
+
@discrete_input_proxy = ModBus::ReadOnlyProxy.new(@slave, :discrete_input)
|
8
|
+
@holding_register_proxy = ModBus::ReadWriteProxy.new(@slave, :holding_register)
|
9
|
+
@input_register_proxy = ModBus::ReadOnlyProxy.new(@slave, :input_register)
|
10
|
+
end
|
11
|
+
|
12
|
+
# Handle all of the coil methods
|
13
|
+
it "should call read_coil" do
|
14
|
+
@slave.should_receive(:read_coil).with(0, 1)
|
15
|
+
@coil_proxy[0]
|
16
|
+
end
|
17
|
+
it "should call read_coils" do
|
18
|
+
@slave.should_receive(:read_coils).with(0, 2)
|
19
|
+
@coil_proxy[0..1]
|
20
|
+
end
|
21
|
+
it "should call write_coil" do
|
22
|
+
@slave.should_receive(:write_coil).with(0, 1)
|
23
|
+
@coil_proxy[0] = 1
|
24
|
+
end
|
25
|
+
it "should call write_coils" do
|
26
|
+
@slave.should_receive(:write_coils).with(0, [0, 0])
|
27
|
+
@coil_proxy[0..1] = [0, 0]
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
# Discrete input tests
|
32
|
+
it "should call read_discrete_input" do
|
33
|
+
@slave.should_receive(:read_discrete_input).with(0, 1)
|
34
|
+
@discrete_input_proxy[0]
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should call read_discrete_inputs" do
|
38
|
+
@slave.should_receive(:read_discrete_inputs).with(0, 2)
|
39
|
+
@discrete_input_proxy[0..1]
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
# Holding Register Tess
|
44
|
+
it "should call read_holding_register" do
|
45
|
+
@slave.should_receive(:read_holding_register).with(0, 1)
|
46
|
+
@holding_register_proxy[0]
|
47
|
+
end
|
48
|
+
it "should call read_holding_registers" do
|
49
|
+
@slave.should_receive(:read_holding_registers).with(0, 2)
|
50
|
+
@holding_register_proxy[0..1]
|
51
|
+
end
|
52
|
+
it "should call write_holding_register" do
|
53
|
+
@slave.should_receive(:write_holding_register).with(0, 1)
|
54
|
+
@holding_register_proxy[0] = 1
|
55
|
+
end
|
56
|
+
it "should call write_holding_registers" do
|
57
|
+
@slave.should_receive(:write_holding_registers).with(0, [0, 0])
|
58
|
+
@holding_register_proxy[0..1] = [0, 0]
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
# Input Register Tests
|
63
|
+
it "should call read_discrete_input" do
|
64
|
+
@slave.should_receive(:read_input_register).with(0, 1)
|
65
|
+
@input_register_proxy[0]
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should call read_discrete_inputs" do
|
69
|
+
@slave.should_receive(:read_input_registers).with(0, 2)
|
70
|
+
@input_register_proxy[0..1]
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
@@ -1,10 +1,9 @@
|
|
1
|
-
require 'rmodbus
|
2
|
-
|
1
|
+
require 'rmodbus'
|
3
2
|
include ModBus
|
4
3
|
|
5
4
|
#Use public wrap method
|
6
|
-
|
7
5
|
class Client
|
6
|
+
include RTU
|
8
7
|
def test_read_method(msg)
|
9
8
|
io = TestIO.new(msg)
|
10
9
|
read_rtu_response(io)
|
@@ -85,7 +84,6 @@ describe "#read_rtu_response" do
|
|
85
84
|
}
|
86
85
|
end
|
87
86
|
|
88
|
-
|
89
87
|
def make_resp(msg)
|
90
88
|
"\x1" + msg + "\x2\x2" # slave + msg + mock_crc
|
91
89
|
end
|
data/spec/rtu_client_spec.rb
CHANGED
@@ -1,22 +1,16 @@
|
|
1
|
-
begin
|
2
|
-
require 'rubygems'
|
3
|
-
rescue
|
4
|
-
end
|
5
1
|
require 'rmodbus'
|
6
|
-
|
7
2
|
include ModBus
|
8
3
|
|
9
4
|
describe RTUClient do
|
10
|
-
|
11
5
|
before do
|
12
6
|
@sp = mock('Serial port')
|
13
7
|
SerialPort.should_receive(:new).with("/dev/port1", 9600, 8, 1, 0).and_return(@sp)
|
14
8
|
@sp.stub!(:read_timeout=)
|
15
9
|
@sp.stub!(:read)
|
16
10
|
|
17
|
-
@
|
18
|
-
|
19
|
-
@
|
11
|
+
@cl = RTUClient.new("/dev/port1", 9600, :data_bits => 8, :stop_bits => 1, :parity => SerialPort::NONE)
|
12
|
+
@slave = @cl.with_slave(1)
|
13
|
+
@slave.read_retries = 0
|
20
14
|
end
|
21
15
|
|
22
16
|
it "should ignore frame with other UID" do
|
@@ -24,7 +18,7 @@ describe RTUClient do
|
|
24
18
|
@sp.should_receive(:write).with("\1#{request}\xA6\x31")
|
25
19
|
@sp.should_receive(:read).with(2).and_return("\x2\x10")
|
26
20
|
@sp.should_receive(:read).with(6).and_return("\x0\x1\x0\x1\x1C\x08")
|
27
|
-
lambda {@
|
21
|
+
lambda {@slave.query(request)}.should raise_error(ModBus::Errors::ModBusTimeout)
|
28
22
|
end
|
29
23
|
|
30
24
|
it "should ignored frame with incorrect CRC" do
|
@@ -32,7 +26,7 @@ describe RTUClient do
|
|
32
26
|
@sp.should_receive(:write).with("\1#{request}\xA6\x31")
|
33
27
|
@sp.should_receive(:read).with(2).and_return("\x2\x10")
|
34
28
|
@sp.should_receive(:read).with(6).and_return("\x0\x1\x0\x1\x1C\x08")
|
35
|
-
lambda {@
|
29
|
+
lambda {@slave.query(request)}.should raise_error(ModBus::Errors::ModBusTimeout)
|
36
30
|
end
|
37
31
|
|
38
32
|
it "should return value of registers"do
|
@@ -41,18 +35,17 @@ describe RTUClient do
|
|
41
35
|
@sp.should_receive(:read).with(2).and_return("\x1\x3")
|
42
36
|
@sp.should_receive(:read).with(1).and_return("\x2")
|
43
37
|
@sp.should_receive(:read).with(4).and_return("\xff\xff\xb9\xf4")
|
44
|
-
@
|
38
|
+
@slave.query(request).should == "\xff\xff"
|
45
39
|
end
|
46
40
|
|
47
|
-
|
48
|
-
port, baud
|
41
|
+
it 'should sugar connect method' do
|
42
|
+
port, baud = "/dev/port1", 4800
|
49
43
|
SerialPort.should_receive(:new).with(port, baud, 8, 1, SerialPort::NONE).and_return(@sp)
|
50
44
|
@sp.should_receive(:closed?).and_return(false)
|
51
45
|
@sp.should_receive(:close)
|
52
|
-
RTUClient.connect(port, baud
|
46
|
+
RTUClient.connect(port, baud) do |cl|
|
53
47
|
cl.port.should == port
|
54
48
|
cl.baud.should == baud
|
55
|
-
cl.slave.should == slave
|
56
49
|
cl.data_bits.should == 8
|
57
50
|
cl.stop_bits.should == 1
|
58
51
|
cl.parity.should == SerialPort::NONE
|
@@ -61,16 +54,21 @@ describe RTUClient do
|
|
61
54
|
|
62
55
|
it 'should have closed? method' do
|
63
56
|
@sp.should_receive(:closed?).and_return(false)
|
64
|
-
@
|
57
|
+
@cl.closed?.should == false
|
65
58
|
|
66
59
|
@sp.should_receive(:closed?).and_return(false)
|
67
60
|
@sp.should_receive(:close)
|
68
61
|
|
69
|
-
@
|
62
|
+
@cl.close
|
70
63
|
|
71
64
|
@sp.should_receive(:closed?).and_return(true)
|
72
|
-
@
|
65
|
+
@cl.closed?.should == true
|
73
66
|
end
|
74
67
|
|
68
|
+
it 'should give slave object in block' do
|
69
|
+
@cl.with_slave(1) do |slave|
|
70
|
+
slave.uid = 1
|
71
|
+
end
|
72
|
+
end
|
75
73
|
end
|
76
74
|
|