tallakt-picopc 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,113 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # http://www.ruby-forum.com/topic/61294
4
+
5
+ require 'win32ole'
6
+ require File.dirname(__FILE__) + "/olegen_opc_automation"
7
+
8
+ module PicOpc
9
+ class PicOpcException < StandardError
10
+ attr_reader :cause
11
+
12
+ def initialize cause
13
+ @cause = cause
14
+ end
15
+
16
+ def self.[](e)
17
+ raise e if e.class == PicOpcException
18
+ e2 = new(e)
19
+ raise e2, "OPC/OLE Exception: #{e.message}", e.backtrace
20
+ end
21
+ end
22
+
23
+ def PicOpc.connect(opc_server_name, options = {})
24
+ po = Client.new opc_server_name, options
25
+ begin
26
+ yield po
27
+ ensure
28
+ po.cleanup
29
+ end
30
+ end
31
+
32
+ class Client
33
+
34
+ def initialize(opc_server_name, options = {})
35
+ begin
36
+ @opc_automation = OPC_Automation_1.new
37
+ @opc_automation.Connect opc_server_name, 'localhost'
38
+ @groups = @opc_automation.OPCGroups
39
+ @group = @groups.add 'picopc_group'
40
+ @opc_items = @group.OPCItems
41
+ @items = {}
42
+ @handle = 1
43
+ if options[:cache]
44
+ @source = OPCDataSource::OPCCache
45
+ else
46
+ @source = OPCDataSource::OPCDevice
47
+ end
48
+ rescue Exception => e
49
+ PicOpcException[e] # wrap all exceptions in this type
50
+ end
51
+ end
52
+
53
+ def cleanup
54
+ begin
55
+ @groups.RemoveAll
56
+ @opc_automation.Disconnect
57
+ rescue Exception => e
58
+ PicOpcException[e] # wrap all exceptions in this type
59
+ end
60
+ end
61
+
62
+ def add_item(name)
63
+ if not @items.key? name
64
+ @items[name] = @opc_items.AddItem(name, @handle)
65
+ @handle += 1
66
+ end
67
+ @items[name]
68
+ end
69
+
70
+
71
+ def read(name)
72
+ begin
73
+ item = add_item name
74
+ return_value = WIN32OLE_VARIANT.new 0, WIN32OLE::VARIANT::VT_VARIANT|WIN32OLE::VARIANT::VT_BYREF
75
+ item.read @source, return_value
76
+ return_value.value
77
+ rescue Exception => e
78
+ PicOpcException[e] # wrap all exceptions in this type
79
+ end
80
+ end
81
+
82
+
83
+ def write(name, value)
84
+ begin
85
+ item = add_item name
86
+ item.write value
87
+ rescue Exception => e
88
+ PicOpcException[e] # wrap all exceptions in this type
89
+ end
90
+ end
91
+
92
+ def [](name)
93
+ read name
94
+ end
95
+
96
+ def []=(name, value)
97
+ write name, value
98
+ end
99
+
100
+ def tag(name, reference)
101
+ # Get a handle to the singleton class of obj
102
+ metaclass = class << self; self; end
103
+
104
+ metaclass.send :define_method, name do
105
+ read reference
106
+ end
107
+
108
+ metaclass.send :define_method, (name.to_s + '=').to_sym do |value|
109
+ write reference, value
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,59 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ MATRIKON = 'Matrikon.OPC.Simulation'
4
+
5
+ describe PicOpc do
6
+ it 'Should be able to read a tag from Matrikon.OPC.Simulator' do
7
+ po = PicOpc::Client.new MATRIKON
8
+ po.read('Random.Int1').should_not be_nil
9
+ po['Random.Int1'].should_not be_nil
10
+ end
11
+ it 'Should be able to write a tag from Matrikon.OPC.Simulator' do
12
+ tag = 'Bucket Brigade.String'
13
+ po = PicOpc::Client.new MATRIKON
14
+ po.write(tag, 'Hello')
15
+ po[tag].should == 'Hello'
16
+ po.write(tag, 'There')
17
+ po[tag].should == 'There'
18
+ po[tag] = 'Folks'
19
+ po[tag].should == 'Folks'
20
+ end
21
+
22
+ it 'Should let through exceptions when passing block' do
23
+ lambda {
24
+ PicOpc.connect MATRIKON do |matrikon|
25
+ raise 'ok'
26
+ end
27
+ }.should raise_exception(RuntimeError) {|ex| ex.message.should == 'ok' }
28
+ end
29
+
30
+ it 'Should should return types of the correct type' do
31
+ PicOpc.connect MATRIKON do |m|
32
+ [true, false].should be_member m['Random.Boolean']
33
+ m['Random.Int1'].should be_a_kind_of(Fixnum)
34
+ m['Random.Real8'].should be_a_kind_of(Float)
35
+ m['Random.String'].should be_a_kind_of(String)
36
+ m['Random.String'].should be_a_kind_of(String)
37
+ tmp = m['Random.ArrayOfString']
38
+ tmp.should be_a_kind_of(Array)
39
+ tmp.first.should be_a_kind_of(String)
40
+ end
41
+ end
42
+
43
+ it 'Should support reading tags created with the tag function' do
44
+ PicOpc.connect MATRIKON do |m|
45
+ m.tag :int, 'Random.Int1'
46
+ m.int.should be_a_kind_of(Fixnum)
47
+ end
48
+ end
49
+
50
+ it 'Should support writing tags created with the tag function' do
51
+ PicOpc.connect MATRIKON do |m|
52
+ m.tag :int, 'Bucket Brigade.Int1'
53
+ m.int = 5
54
+ m.int.should == 5
55
+ m.int = 6
56
+ m.int.should == 6
57
+ end
58
+ end
59
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'picopc'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
@@ -0,0 +1,58 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{tallakt-picopc}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Tallak Tveide"]
12
+ s.date = %q{2010-02-17}
13
+ s.description = %q{A simple DCOM OPC library for ruby that allows simple access to OPC servers but with poor efficiency}
14
+ s.email = %q{tallak@tveide.net}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".gitignore",
21
+ "LICENSE",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "lib/picopc.rb",
26
+ "lib/picopc/olegen_opc_automation.rb",
27
+ "lib/picopc/picopc.rb",
28
+ "spec/picopc_spec.rb",
29
+ "spec/spec.opts",
30
+ "spec/spec_helper.rb",
31
+ "tallakt-picopc.gemspec"
32
+ ]
33
+ s.homepage = %q{http://github.com/tallakt/picopc}
34
+ s.rdoc_options = ["--charset=UTF-8", "--title", "PicOpc -- Tiny Ruby OPC", "--main", "README", "--line-numbers"]
35
+ s.require_paths = ["lib"]
36
+ s.required_ruby_version = Gem::Requirement.new(">= 1.9.0")
37
+ s.requirements = ["To run tests, you must install Matrikon OPC Simulator", "PicOpc will only run on Windows systems due to DCOM dependency"]
38
+ s.rubygems_version = %q{1.3.5}
39
+ s.summary = %q{Pico OPC: very tiny OPc implementation for Ruby}
40
+ s.test_files = [
41
+ "spec/picopc_spec.rb",
42
+ "spec/spec_helper.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
+ s.specification_version = 3
48
+
49
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
50
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
51
+ else
52
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
53
+ end
54
+ else
55
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
56
+ end
57
+ end
58
+
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tallakt-picopc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tallak Tveide
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-17 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.9
24
+ version:
25
+ description: A simple DCOM OPC library for ruby that allows simple access to OPC servers but with poor efficiency
26
+ email: tallak@tveide.net
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - .gitignore
36
+ - LICENSE
37
+ - README.rdoc
38
+ - Rakefile
39
+ - VERSION
40
+ - lib/picopc.rb
41
+ - lib/picopc/olegen_opc_automation.rb
42
+ - lib/picopc/picopc.rb
43
+ - spec/picopc_spec.rb
44
+ - spec/spec.opts
45
+ - spec/spec_helper.rb
46
+ - tallakt-picopc.gemspec
47
+ has_rdoc: true
48
+ homepage: http://github.com/tallakt/picopc
49
+ licenses: []
50
+
51
+ post_install_message:
52
+ rdoc_options:
53
+ - --charset=UTF-8
54
+ - --title
55
+ - PicOpc -- Tiny Ruby OPC
56
+ - --main
57
+ - README
58
+ - --line-numbers
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 1.9.0
66
+ version:
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ version:
73
+ requirements:
74
+ - To run tests, you must install Matrikon OPC Simulator
75
+ - PicOpc will only run on Windows systems due to DCOM dependency
76
+ rubyforge_project:
77
+ rubygems_version: 1.3.5
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: "Pico OPC: very tiny OPc implementation for Ruby"
81
+ test_files:
82
+ - spec/picopc_spec.rb
83
+ - spec/spec_helper.rb