rmodbus 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,84 @@
1
+ =begin
2
+ It's very simple example of implementation XPCA gateway (see http://www.xpca.org)
3
+ for communication with TCP ModBus devices
4
+ It receives REST requests (e.g http://127.0.0.1:4567/mb/127.0.0.1/8502/1/coils/6/17 )
5
+ and returns data in JSON format addr : data:
6
+ {"coils": {
7
+ "6":{
8
+ "value":0,
9
+ "timestamp":"2011-07-12 18:11:03 +0000",
10
+ "quality":"good"
11
+ },
12
+ "7":{
13
+ "value":0,
14
+ "timestamp":"2011-07-12 18:11:03 +0000",
15
+ "quality":"good"
16
+ }
17
+ ...
18
+ }
19
+
20
+ This code requies gems: rmodbus, sinatra and json
21
+ 2011 (c) Aleksey Timin
22
+ =end
23
+
24
+ require 'rubygems'
25
+ require 'rmodbus'
26
+ require 'sinatra'
27
+ require 'json'
28
+
29
+ # Launche TCP ModBus server for test
30
+ IP = '127.0.0.1'
31
+ PORT = 8502
32
+
33
+ @srv = ModBus::TCPServer.new(PORT,1)
34
+
35
+ @srv.holding_registers = Array.new(100) { |i| i = i + 1 }
36
+ @srv.input_registers = Array.new(100) { |i| i = i + 1 }
37
+ @srv.coils = Array.new(100) { |i| i = 0 }
38
+ @srv.discrete_inputs = Array.new(100) { |i| i = 0 }
39
+
40
+ @srv.start
41
+
42
+
43
+ # Calc a GET request
44
+ # @example
45
+ # http://127.0.0.1:4567/mb/127.0.0.1/8502/1/coils/6/17
46
+ #
47
+ # HTTP route: GET http://localhost/mb/:ip/:port/:slave/:dataplace/:firstaddr/:lastaddr
48
+ #
49
+ # :ip - ip addr of ModBus TCP Server
50
+ # :port - port of ModBUs TCP Server
51
+ # :slave - id of slave device
52
+ # :dataplace - valid values: coils, discrete_inputs, input_registers, holding_registers
53
+ # :firstaddr - first addr of registers(contacts)
54
+ # :lastaddr - last addr of registers(contacts)
55
+ get '/mb/:ip/:port/:slave/:dataplace/:firstaddr/:lastaddr' do
56
+ resp = {}
57
+ begin
58
+ data = []
59
+ ModBus::TCPClient.new(params[:ip].to_s, params[:port].to_i) do |cl|
60
+ cl.with_slave(params[:slave].to_i) do |slave|
61
+ slave.debug = true
62
+ dataplace = slave.send params[:dataplace]
63
+ data = dataplace[params[:firstaddr].to_i .. params[:lastaddr].to_i]
64
+ end
65
+ end
66
+
67
+ resp = { params[:dataplace] => {}}
68
+ data.each_with_index do |v,i|
69
+ resp[params[:dataplace]][params[:firstaddr].to_i + i] = {
70
+ :value => v,
71
+ :timestamp => Time.now.utc.strftime("%Y-%m-%d %H:%M:%S %z"),
72
+ :quality => "good"
73
+ }
74
+ end
75
+ rescue Exception => e
76
+ resp = { :error => {
77
+ :type => e.class,
78
+ :message => e.message }
79
+ }
80
+ end
81
+
82
+ content_type "application/json"
83
+ resp.to_json
84
+ end
@@ -33,10 +33,15 @@ module ModBus
33
33
  # Init server
34
34
  # @param [Integer] port listen port
35
35
  # @param [Integer] uid slave device
36
- def initialize(port = 10002, uid = 1)
37
- @uid = uid
38
- super(port)
39
- end
36
+ # @param [Hash] opts options of server
37
+ # @option opts [String] :host host of server default '127.0.0.1'
38
+ # @option opts [Float, Integer] :max_connection max of TCP connection with server default 4
39
+ def initialize(port = 10002, uid = 1, opts = {})
40
+ @uid = uid
41
+ opts[:host] = DEFAULT_HOST unless opts[:host]
42
+ opts[:max_connection] = 4 unless opts[:max_connection]
43
+ super(port, host = opts[:host], maxConnection = opts[:max_connection])
44
+ end
40
45
 
41
46
  protected
42
47
  # Serve requests
@@ -30,9 +30,14 @@ module ModBus
30
30
  # Init server
31
31
  # @param [Integer] port listen port
32
32
  # @param [Integer] uid slave device
33
- def initialize(port = 502, uid = 1)
33
+ # @param [Hash] opts options of server
34
+ # @option opts [String] :host host of server default '127.0.0.1'
35
+ # @option opts [Float, Integer] :max_connection max of TCP connection with server default 4
36
+ def initialize(port = 502, uid = 1, opts = {})
34
37
  @uid = uid
35
- super(port)
38
+ opts[:host] = DEFAULT_HOST unless opts[:host]
39
+ opts[:max_connection] = 4 unless opts[:max_connection]
40
+ super(port, host = opts[:host], maxConnection = opts[:max_connection])
36
41
  end
37
42
 
38
43
  # Serve requests
@@ -13,5 +13,5 @@
13
13
  # GNU General Public License for more details.
14
14
  module ModBus
15
15
  # Package version
16
- VERSION = '1.0.2'
16
+ VERSION = '1.0.3'
17
17
  end
metadata CHANGED
@@ -1,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rmodbus
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
5
4
  prerelease:
6
- segments:
7
- - 1
8
- - 0
9
- - 2
10
- version: 1.0.2
5
+ version: 1.0.3
11
6
  platform: ruby
12
7
  authors:
13
8
  - A.Timin, J. Sanders, K. Reynolds
@@ -15,7 +10,7 @@ autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
12
 
18
- date: 2011-06-30 00:00:00 Z
13
+ date: 2011-07-16 00:00:00 Z
19
14
  dependencies:
20
15
  - !ruby/object:Gem::Dependency
21
16
  name: serialport
@@ -25,9 +20,6 @@ dependencies:
25
20
  requirements:
26
21
  - - ">="
27
22
  - !ruby/object:Gem::Version
28
- hash: 3
29
- segments:
30
- - 0
31
23
  version: "0"
32
24
  type: :runtime
33
25
  version_requirements: *id001
@@ -39,9 +31,6 @@ dependencies:
39
31
  requirements:
40
32
  - - ">="
41
33
  - !ruby/object:Gem::Version
42
- hash: 3
43
- segments:
44
- - 0
45
34
  version: "0"
46
35
  type: :development
47
36
  version_requirements: *id002
@@ -53,9 +42,6 @@ dependencies:
53
42
  requirements:
54
43
  - - ">="
55
44
  - !ruby/object:Gem::Version
56
- hash: 3
57
- segments:
58
- - 0
59
45
  version: "0"
60
46
  type: :development
61
47
  version_requirements: *id003
@@ -67,9 +53,6 @@ dependencies:
67
53
  requirements:
68
54
  - - ">="
69
55
  - !ruby/object:Gem::Version
70
- hash: 3
71
- segments:
72
- - 0
73
56
  version: "0"
74
57
  type: :development
75
58
  version_requirements: *id004
@@ -81,9 +64,6 @@ dependencies:
81
64
  requirements:
82
65
  - - ">="
83
66
  - !ruby/object:Gem::Version
84
- hash: 3
85
- segments:
86
- - 0
87
67
  version: "0"
88
68
  type: :development
89
69
  version_requirements: *id005
@@ -95,9 +75,6 @@ dependencies:
95
75
  requirements:
96
76
  - - ">="
97
77
  - !ruby/object:Gem::Version
98
- hash: 3
99
- segments:
100
- - 0
101
78
  version: "0"
102
79
  type: :development
103
80
  version_requirements: *id006
@@ -109,9 +86,6 @@ dependencies:
109
86
  requirements:
110
87
  - - ">="
111
88
  - !ruby/object:Gem::Version
112
- hash: 3
113
- segments:
114
- - 0
115
89
  version: "0"
116
90
  type: :development
117
91
  version_requirements: *id007
@@ -146,6 +120,7 @@ files:
146
120
  - lib/rmodbus/server.rb
147
121
  - lib/rmodbus/rtu_via_tcp_slave.rb
148
122
  - lib/rmodbus/tcp_slave.rb
123
+ - examples/simple-xpca-gateway.rb
149
124
  - examples/perfomance_tcp.rb
150
125
  - examples/use_tcp_modbus.rb
151
126
  - examples/use_rtu_via_tcp_modbus.rb
@@ -182,26 +157,19 @@ required_ruby_version: !ruby/object:Gem::Requirement
182
157
  requirements:
183
158
  - - ">="
184
159
  - !ruby/object:Gem::Version
185
- hash: 3
186
- segments:
187
- - 0
188
160
  version: "0"
189
161
  required_rubygems_version: !ruby/object:Gem::Requirement
190
162
  none: false
191
163
  requirements:
192
164
  - - ">="
193
165
  - !ruby/object:Gem::Version
194
- hash: 3
195
- segments:
196
- - 0
197
166
  version: "0"
198
167
  requirements: []
199
168
 
200
169
  rubyforge_project:
201
- rubygems_version: 1.7.2
170
+ rubygems_version: 1.8.5
202
171
  signing_key:
203
172
  specification_version: 3
204
173
  summary: RModBus - free implementation of protocol ModBus
205
174
  test_files: []
206
175
 
207
- has_rdoc: