ffi-snap7 0.1.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.
@@ -0,0 +1,24 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ .idea
24
+ .ruby-*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ffi-snap7.gemspec
4
+ gemspec
@@ -0,0 +1,167 @@
1
+ Copyright (c) 2015 Andy Rohr
2
+
3
+ GNU LESSER GENERAL PUBLIC LICENSE
4
+ Version 3, 29 June 2007
5
+
6
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
7
+ Everyone is permitted to copy and distribute verbatim copies
8
+ of this license document, but changing it is not allowed.
9
+
10
+
11
+ This version of the GNU Lesser General Public License incorporates
12
+ the terms and conditions of version 3 of the GNU General Public
13
+ License, supplemented by the additional permissions listed below.
14
+
15
+ 0. Additional Definitions.
16
+
17
+ As used herein, "this License" refers to version 3 of the GNU Lesser
18
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
19
+ General Public License.
20
+
21
+ "The Library" refers to a covered work governed by this License,
22
+ other than an Application or a Combined Work as defined below.
23
+
24
+ An "Application" is any work that makes use of an interface provided
25
+ by the Library, but which is not otherwise based on the Library.
26
+ Defining a subclass of a class defined by the Library is deemed a mode
27
+ of using an interface provided by the Library.
28
+
29
+ A "Combined Work" is a work produced by combining or linking an
30
+ Application with the Library. The particular version of the Library
31
+ with which the Combined Work was made is also called the "Linked
32
+ Version".
33
+
34
+ The "Minimal Corresponding Source" for a Combined Work means the
35
+ Corresponding Source for the Combined Work, excluding any source code
36
+ for portions of the Combined Work that, considered in isolation, are
37
+ based on the Application, and not on the Linked Version.
38
+
39
+ The "Corresponding Application Code" for a Combined Work means the
40
+ object code and/or source code for the Application, including any data
41
+ and utility programs needed for reproducing the Combined Work from the
42
+ Application, but excluding the System Libraries of the Combined Work.
43
+
44
+ 1. Exception to Section 3 of the GNU GPL.
45
+
46
+ You may convey a covered work under sections 3 and 4 of this License
47
+ without being bound by section 3 of the GNU GPL.
48
+
49
+ 2. Conveying Modified Versions.
50
+
51
+ If you modify a copy of the Library, and, in your modifications, a
52
+ facility refers to a function or data to be supplied by an Application
53
+ that uses the facility (other than as an argument passed when the
54
+ facility is invoked), then you may convey a copy of the modified
55
+ version:
56
+
57
+ a) under this License, provided that you make a good faith effort to
58
+ ensure that, in the event an Application does not supply the
59
+ function or data, the facility still operates, and performs
60
+ whatever part of its purpose remains meaningful, or
61
+
62
+ b) under the GNU GPL, with none of the additional permissions of
63
+ this License applicable to that copy.
64
+
65
+ 3. Object Code Incorporating Material from Library Header Files.
66
+
67
+ The object code form of an Application may incorporate material from
68
+ a header file that is part of the Library. You may convey such object
69
+ code under terms of your choice, provided that, if the incorporated
70
+ material is not limited to numerical parameters, data structure
71
+ layouts and accessors, or small macros, inline functions and templates
72
+ (ten or fewer lines in length), you do both of the following:
73
+
74
+ a) Give prominent notice with each copy of the object code that the
75
+ Library is used in it and that the Library and its use are
76
+ covered by this License.
77
+
78
+ b) Accompany the object code with a copy of the GNU GPL and this license
79
+ document.
80
+
81
+ 4. Combined Works.
82
+
83
+ You may convey a Combined Work under terms of your choice that,
84
+ taken together, effectively do not restrict modification of the
85
+ portions of the Library contained in the Combined Work and reverse
86
+ engineering for debugging such modifications, if you also do each of
87
+ the following:
88
+
89
+ a) Give prominent notice with each copy of the Combined Work that
90
+ the Library is used in it and that the Library and its use are
91
+ covered by this License.
92
+
93
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
94
+ document.
95
+
96
+ c) For a Combined Work that displays copyright notices during
97
+ execution, include the copyright notice for the Library among
98
+ these notices, as well as a reference directing the user to the
99
+ copies of the GNU GPL and this license document.
100
+
101
+ d) Do one of the following:
102
+
103
+ 0) Convey the Minimal Corresponding Source under the terms of this
104
+ License, and the Corresponding Application Code in a form
105
+ suitable for, and under terms that permit, the user to
106
+ recombine or relink the Application with a modified version of
107
+ the Linked Version to produce a modified Combined Work, in the
108
+ manner specified by section 6 of the GNU GPL for conveying
109
+ Corresponding Source.
110
+
111
+ 1) Use a suitable shared library mechanism for linking with the
112
+ Library. A suitable mechanism is one that (a) uses at run time
113
+ a copy of the Library already present on the user's computer
114
+ system, and (b) will operate properly with a modified version
115
+ of the Library that is interface-compatible with the Linked
116
+ Version.
117
+
118
+ e) Provide Installation Information, but only if you would otherwise
119
+ be required to provide such information under section 6 of the
120
+ GNU GPL, and only to the extent that such information is
121
+ necessary to install and execute a modified version of the
122
+ Combined Work produced by recombining or relinking the
123
+ Application with a modified version of the Linked Version. (If
124
+ you use option 4d0, the Installation Information must accompany
125
+ the Minimal Corresponding Source and Corresponding Application
126
+ Code. If you use option 4d1, you must provide the Installation
127
+ Information in the manner specified by section 6 of the GNU GPL
128
+ for conveying Corresponding Source.)
129
+
130
+ 5. Combined Libraries.
131
+
132
+ You may place library facilities that are a work based on the
133
+ Library side by side in a single library together with other library
134
+ facilities that are not Applications and are not covered by this
135
+ License, and convey such a combined library under terms of your
136
+ choice, if you do both of the following:
137
+
138
+ a) Accompany the combined library with a copy of the same work based
139
+ on the Library, uncombined with any other library facilities,
140
+ conveyed under the terms of this License.
141
+
142
+ b) Give prominent notice with the combined library that part of it
143
+ is a work based on the Library, and explaining where to find the
144
+ accompanying uncombined form of the same work.
145
+
146
+ 6. Revised Versions of the GNU Lesser General Public License.
147
+
148
+ The Free Software Foundation may publish revised and/or new versions
149
+ of the GNU Lesser General Public License from time to time. Such new
150
+ versions will be similar in spirit to the present version, but may
151
+ differ in detail to address new problems or concerns.
152
+
153
+ Each version is given a distinguishing version number. If the
154
+ Library as you received it specifies that a certain numbered version
155
+ of the GNU Lesser General Public License "or any later version"
156
+ applies to it, you have the option of following the terms and
157
+ conditions either of that published version or of any later version
158
+ published by the Free Software Foundation. If the Library as you
159
+ received it does not specify a version number of the GNU Lesser
160
+ General Public License, you may choose any version of the GNU Lesser
161
+ General Public License ever published by the Free Software Foundation.
162
+
163
+ If the Library as you received it specifies that a proxy can decide
164
+ whether future versions of the GNU Lesser General Public License shall
165
+ apply, that proxy's public statement of acceptance of any version is
166
+ permanent authorization for you to choose that version for the
167
+ Library.
@@ -0,0 +1,77 @@
1
+ # FFI Snap7
2
+
3
+ FFI Ruby wrapper for Snap7 (http://snap7.sourceforge.net/)
4
+
5
+ ## Installation
6
+
7
+ Under Ubuntu 14.04 (the environment I tested, but may apply to others as well), perform the following steps:
8
+
9
+ 1. Download and compile Snap7 (see according documentation)
10
+ 2. Copy libsnap7.so to /usr/local/lib
11
+
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'ffi-snap7'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install ffi-snap7
24
+
25
+
26
+ ## Usage
27
+
28
+ See examples/
29
+
30
+ Server:
31
+
32
+ server = Snap7::Server.new
33
+ server.register_db 1, 16
34
+ server.register_db 2, 16
35
+ server.start
36
+ puts 'started'
37
+
38
+ run = true
39
+ trap('INT') do
40
+ puts 'stopping'
41
+ server.stop
42
+ run = false
43
+ end
44
+
45
+ while run
46
+ sleep 0.1
47
+ end
48
+ puts 'done'
49
+
50
+ Startup:
51
+
52
+ authbind --deep ./examples/server.rb
53
+
54
+
55
+
56
+ Client:
57
+
58
+ client = Snap7::Client.new
59
+ client.connect '127.0.0.1', 0, 2
60
+ p client.db_read 1, 0, 4
61
+ client.db_write 1, 0, 4, [7, 15, 31, 63]
62
+ p client.db_read 1, 0, 4
63
+ client.disconnect
64
+
65
+ Startup:
66
+
67
+ ./examples/client.rb
68
+
69
+
70
+
71
+ ## Contributing
72
+
73
+ 1. Fork it ( https://github.com/arohr/ffi-snap7/fork )
74
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
75
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
76
+ 4. Push to the branch (`git push origin my-new-feature`)
77
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'lib/ffi-snap7'
4
+
5
+ client = Snap7::Client.new
6
+ client.connect '127.0.0.1', 0, 2
7
+ p client.db_read 1, 0, 4
8
+ client.db_write 1, 0, 4, [7, 15, 31, 63]
9
+ p client.db_read 1, 0, 4
10
+ client.disconnect
11
+
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'lib/ffi-snap7'
4
+
5
+ server = Snap7::Server.new
6
+ server.register_db 1, 16
7
+ server.register_db 2, 16
8
+ server.start
9
+ puts 'started'
10
+
11
+ run = true
12
+ trap('INT') do
13
+ puts 'stopping'
14
+ server.stop
15
+ run = false
16
+ end
17
+
18
+ while run
19
+ sleep 0.1
20
+ end
21
+ puts 'done'
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ffi-snap7/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'ffi-snap7'
8
+ spec.version = Snap7::VERSION
9
+ spec.authors = ['Andy Rohr']
10
+ spec.email = ['andy.rohr@mindclue.ch']
11
+ spec.summary = %q{FFI Ruby wrapper for Snap7 (http://snap7.sourceforge.net/)}
12
+ spec.description = %q{Access to Siemens PLCs, Simulation of Siemens PLCs}
13
+ spec.homepage = 'https://github.com/arohr/ffi-snap7'
14
+ spec.license = 'LGPLv3'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_runtime_dependency 'ffi'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.6'
24
+ spec.add_development_dependency 'rake'
25
+ end
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'ffi'
3
+
4
+ module Snap7
5
+ extend FFI::Library
6
+ ffi_lib 'libsnap7.so'
7
+
8
+ # S7Object
9
+ typedef :pointer, :s7obj
10
+ end
11
+
12
+ require 'lib/ffi-snap7/version'
13
+ require 'lib/ffi-snap7/client/client'
14
+ require 'lib/ffi-snap7/server/server'
@@ -0,0 +1,57 @@
1
+ require 'lib/ffi-snap7/client/wrapper'
2
+
3
+ module Snap7
4
+
5
+ class Client
6
+
7
+ def initialize
8
+ @cli = Snap7.cli_create
9
+ end
10
+
11
+
12
+ def connect(address, rack, slot)
13
+ check_rc Snap7.cli_connect_to(@cli, address, rack, slot)
14
+ end
15
+
16
+
17
+ def disconnect
18
+ Snap7.cli_disconnect @cli
19
+ end
20
+
21
+
22
+ def db_read(db_number, start, size)
23
+ p_usr_data = FFI::MemoryPointer.new :uchar, size
24
+ check_rc Snap7.cli_db_read(@cli, db_number, start, size, p_usr_data)
25
+ p_usr_data.get_array_of_uint8 0, size
26
+ end
27
+
28
+
29
+ def db_write(db_number, start, size, data)
30
+ p_usr_data = FFI::MemoryPointer.new :uchar, size
31
+ p_usr_data.put_array_of_uint8 0, data
32
+ check_rc Snap7.cli_db_write(@cli, db_number, start, size, p_usr_data)
33
+ end
34
+
35
+
36
+ private
37
+
38
+
39
+ def check_rc(rc)
40
+ fail error_text(rc) unless rc == 0
41
+ end
42
+
43
+
44
+ def error_text(error)
45
+ text_len = 1024
46
+ text_ptr = FFI::MemoryPointer.new :pointer, text_len
47
+ if Snap7.cli_error_text(error, text_ptr, text_len) == 0
48
+ text_ptr.read_string
49
+ else
50
+ fail "Error getting error text for error no #{error}"
51
+ end
52
+ end
53
+
54
+
55
+ end
56
+
57
+ end
@@ -0,0 +1,26 @@
1
+ module Snap7
2
+
3
+ # EXPORTSPEC S7Object S7API Cli_Create();
4
+ attach_function :cli_create, :Cli_Create, [], :s7obj
5
+
6
+ # EXPORTSPEC void S7API Cli_Destroy(S7Object &Client);
7
+ # FIXME crashes MRI
8
+ # attach_function :cli_destroy, :Cli_Destroy, [:s7obj], :void
9
+
10
+ # EXPORTSPEC int S7API Cli_ConnectTo(S7Object Client, const char *Address, int Rack, int Slot);
11
+ attach_function :cli_connect_to, :Cli_ConnectTo, [:s7obj, :string, :int, :int], :int
12
+
13
+ # EXPORTSPEC int S7API Cli_Disconnect(S7Object Client);
14
+ attach_function :cli_disconnect, :Cli_Disconnect, [:s7obj], :int
15
+
16
+ # EXPORTSPEC int S7API Cli_DBRead(S7Object Client, int DBNumber, int Start, int Size, void *pUsrData);
17
+ attach_function :cli_db_read, :Cli_DBRead, [:s7obj, :int, :int, :int, :pointer], :int
18
+
19
+ # EXPORTSPEC int S7API Cli_DBWrite(S7Object Client, int DBNumber, int Start, int Size, void *pUsrData);
20
+ attach_function :cli_db_write, :Cli_DBWrite, [:s7obj, :int, :int, :int, :pointer], :int
21
+
22
+ # EXPORTSPEC int S7API Cli_ErrorText(int Error, char *Text, int TextLen);
23
+ attach_function :cli_error_text, :Cli_ErrorText, [:int, :pointer, :int], :int
24
+
25
+
26
+ end
@@ -0,0 +1,62 @@
1
+ require 'lib/ffi-snap7/server/wrapper'
2
+
3
+ module Snap7
4
+
5
+ class Server
6
+
7
+ S7AreaPE = 0
8
+ S7AreaPA = 1
9
+ S7AreaMK = 2
10
+ S7AreaCT = 3
11
+ S7AreaTM = 4
12
+ S7AreaDB = 5
13
+
14
+
15
+ def initialize
16
+ @srv = Snap7.srv_create
17
+ end
18
+
19
+
20
+ def start
21
+ check_rc Snap7.srv_start(@srv)
22
+ end
23
+
24
+
25
+ def stop
26
+ check_rc Snap7.srv_stop(@srv)
27
+ end
28
+
29
+
30
+ def register_area(area_code, index, size)
31
+ p_usr_data = FFI::MemoryPointer.new :uchar, size
32
+ check_rc Snap7.srv_register_area @srv, area_code, index, p_usr_data, size
33
+ end
34
+
35
+
36
+ def register_db(index, size)
37
+ register_area S7AreaDB, index, size
38
+ end
39
+
40
+
41
+ private
42
+
43
+
44
+ def check_rc(rc)
45
+ fail error_text(rc) unless rc == 0
46
+ end
47
+
48
+
49
+ def error_text(error)
50
+ text_len = 1024
51
+ text_ptr = FFI::MemoryPointer.new :pointer, text_len
52
+ if Snap7.srv_error_text(error, text_ptr, text_len) == 0
53
+ text_ptr.read_string
54
+ else
55
+ fail "Error getting error text for error no #{error}"
56
+ end
57
+ end
58
+
59
+ end
60
+
61
+ end
62
+
@@ -0,0 +1,37 @@
1
+ module Snap7
2
+
3
+ # EXPORTSPEC int S7API Srv_ErrorText(int Error, char *Text, int TextLen);
4
+ attach_function :srv_error_text, :Srv_ErrorText, [:int, :pointer, :int], :int
5
+
6
+ # EXPORTSPEC S7Object S7API Srv_Create();
7
+ attach_function :srv_create, :Srv_Create, [], :s7obj
8
+
9
+ # EXPORTSPEC void S7API Srv_Destroy(S7Object &Server);
10
+ # FIXME crashes MRI
11
+ # attach_function :srv_destroy, :Srv_Destroy, [:s7obj], :void
12
+
13
+ # EXPORTSPEC int S7API Srv_GetParam(S7Object Server, int ParamNumber, void *pValue);
14
+
15
+ # EXPORTSPEC int S7API Srv_SetParam(S7Object Server, int ParamNumber, void *pValue);
16
+
17
+ # EXPORTSPEC int S7API Srv_Start(S7Object Server);
18
+ attach_function :srv_start, :Srv_Start, [:s7obj], :int
19
+
20
+ # EXPORTSPEC int S7API Srv_StartTo(S7Object Server, const char *Address);
21
+ # attach_function :srv_start_to, :Srv_StartTo, [:s7obj, :string], :int
22
+
23
+ # EXPORTSPEC int S7API Srv_Stop(S7Object Server);
24
+ attach_function :srv_stop, :Srv_Stop, [:s7obj], :int
25
+
26
+ # EXPORTSPEC int S7API Srv_RegisterArea(S7Object Server, int AreaCode, word Index, void *pUsrData, int Size);
27
+ attach_function :srv_register_area, :Srv_RegisterArea, [:s7obj, :int, :int16, :pointer, :int], :int
28
+
29
+ # EXPORTSPEC int S7API Srv_UnregisterArea(S7Object Server, int AreaCode, word Index);
30
+ # attach_function :srv_unregister_area, :Srv_UnregisterArea, [:s7obj, :int, :int16], :int
31
+
32
+ # EXPORTSPEC int S7API Srv_LockArea(S7Object Server, int AreaCode, word Index);
33
+
34
+ # EXPORTSPEC int S7API Srv_UnlockArea(S7Object Server, int AreaCode, word Index);
35
+
36
+
37
+ end
@@ -0,0 +1,3 @@
1
+ module Snap7
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ffi-snap7
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Andy Rohr
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2015-03-20 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ type: :runtime
22
+ version_requirements: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ hash: 3
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ requirement: *id001
32
+ prerelease: false
33
+ name: ffi
34
+ - !ruby/object:Gem::Dependency
35
+ type: :development
36
+ version_requirements: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ hash: 3
42
+ segments:
43
+ - 1
44
+ - 6
45
+ version: "1.6"
46
+ requirement: *id002
47
+ prerelease: false
48
+ name: bundler
49
+ - !ruby/object:Gem::Dependency
50
+ type: :development
51
+ version_requirements: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ requirement: *id003
61
+ prerelease: false
62
+ name: rake
63
+ description: Access to Siemens PLCs, Simulation of Siemens PLCs
64
+ email:
65
+ - andy.rohr@mindclue.ch
66
+ executables: []
67
+
68
+ extensions: []
69
+
70
+ extra_rdoc_files: []
71
+
72
+ files:
73
+ - .gitignore
74
+ - Gemfile
75
+ - LICENSE.txt
76
+ - README.md
77
+ - Rakefile
78
+ - examples/client.rb
79
+ - examples/server.rb
80
+ - ffi-snap7.gemspec
81
+ - lib/ffi-snap7.rb
82
+ - lib/ffi-snap7/client/client.rb
83
+ - lib/ffi-snap7/client/wrapper.rb
84
+ - lib/ffi-snap7/server/server.rb
85
+ - lib/ffi-snap7/server/wrapper.rb
86
+ - lib/ffi-snap7/version.rb
87
+ homepage: https://github.com/arohr/ffi-snap7
88
+ licenses:
89
+ - LGPLv3
90
+ post_install_message:
91
+ rdoc_options: []
92
+
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ hash: 3
101
+ segments:
102
+ - 0
103
+ version: "0"
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ hash: 3
110
+ segments:
111
+ - 0
112
+ version: "0"
113
+ requirements: []
114
+
115
+ rubyforge_project:
116
+ rubygems_version: 1.8.29
117
+ signing_key:
118
+ specification_version: 3
119
+ summary: FFI Ruby wrapper for Snap7 (http://snap7.sourceforge.net/)
120
+ test_files: []
121
+