rmodbus 1.0.2-java → 1.0.3-java

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
@@ -2,7 +2,7 @@
2
2
  name: rmodbus
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.0.2
5
+ version: 1.0.3
6
6
  platform: java
7
7
  authors:
8
8
  - A.Timin, J. Sanders, K. Reynolds
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-07-01 00:00:00 +06:00
13
+ date: 2011-07-16 00:00:00 +06:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -110,6 +110,7 @@ files:
110
110
  - lib/rmodbus/server.rb
111
111
  - lib/rmodbus/rtu_via_tcp_slave.rb
112
112
  - lib/rmodbus/tcp_slave.rb
113
+ - examples/simple-xpca-gateway.rb
113
114
  - examples/perfomance_tcp.rb
114
115
  - examples/use_tcp_modbus.rb
115
116
  - examples/use_rtu_via_tcp_modbus.rb