rs_232 2.0.4

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,100 @@
1
+ require 'cucumber'
2
+ require 'cucumber/formatter/unicode'
3
+ require File.expand_path("../../../lib/rs_232", __FILE__)
4
+ require File.expand_path("../adapter", __FILE__)
5
+
6
+ # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
7
+ #
8
+ # relative path location method
9
+ #
10
+ def expand_path(path)
11
+ File.expand_path(path, File.dirname(__FILE__))
12
+ end
13
+
14
+
15
+ # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
16
+ #
17
+ # ENV
18
+ #
19
+ config = expand_path '../config/config.yml'
20
+ if File.file? config
21
+ require 'yaml'
22
+ env = YAML.load File.read config
23
+ env.each do |key, value|
24
+ if ENV['DEBUG'] or env['DEBUG']
25
+ if ENV[key]
26
+ puts "Set `#{key}` to `#{ENV[key]}`."
27
+ else
28
+ puts "Set `#{key}` to default `#{value}`."
29
+ end
30
+ end
31
+
32
+ ENV[key] ||= value.to_s if value
33
+ end
34
+ else
35
+ if ENV['DEBUG']
36
+ puts "Please check `features/support/config` folder for existing file `config.yml`"
37
+ puts
38
+ end
39
+ end
40
+
41
+ # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
42
+ #
43
+ # Load only if bundled console
44
+ #
45
+
46
+ unless $0 =~ /cucumber/i
47
+ runtime = Cucumber::Runtime.new
48
+ runtime.load_programming_language('rb')
49
+ end
50
+
51
+
52
+ # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
53
+ #
54
+ # logger impl
55
+ #
56
+
57
+ class LogFormatter < ::Logger::Formatter
58
+
59
+ def call(severity, time, progname, msg)
60
+ "#{format_datetime(time)}[TID:#{$$}] [EXT::#{severity}]: #{msg}\n"
61
+ end
62
+
63
+ private
64
+
65
+ def format_datetime(time)
66
+ time.strftime("%Y-%m-%d %H:%M:%S.") << "%06d " % time.usec
67
+ end
68
+
69
+ end
70
+
71
+ def logger
72
+ @logger ||= begin
73
+ l = ::Logger.new($stdout)
74
+ l.level = Logger::DEBUG
75
+ l.formatter = LogFormatter.new
76
+ l
77
+ end
78
+ end
79
+
80
+ # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
81
+
82
+ def serial_port
83
+ if ENV["SERIAL_PORT"].nil?
84
+ $stdout.write "Enter serial port name: "
85
+ ENV["SERIAL_PORT"] = $stdin.gets.chomp
86
+ end
87
+ ENV["SERIAL_PORT"]
88
+ end
89
+
90
+ # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
91
+
92
+ def adapter
93
+ @adapter ||= Adapter::Dev.new(serial_port)
94
+ end
95
+
96
+ # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
97
+
98
+ def transmit(string)
99
+ adpter.tx(string)
100
+ end
@@ -0,0 +1,5 @@
1
+ desc 'Run all tests and collect code coverage'
2
+ task :cov do
3
+ ENV['SIMPLECOV'] = 'features'
4
+ Rake::Task['default'].invoke
5
+ end
@@ -0,0 +1,23 @@
1
+ require 'cucumber/rake/task'
2
+ require 'cucumber/platform'
3
+
4
+ class Cucumber::Rake::Task
5
+ def set_profile_for_current_ruby
6
+ self.profile = if Cucumber::JRUBY
7
+ Cucumber::WINDOWS ? 'jruby_win' : 'jruby'
8
+ elsif Cucumber::WINDOWS_MRI
9
+ 'windows_mri'
10
+ elsif Cucumber::RUBY_1_9
11
+ 'ruby_1_9'
12
+ elsif Cucumber::RUBY_2_0
13
+ 'ruby_2_0'
14
+ end
15
+ end
16
+ end
17
+
18
+ Cucumber::Rake::Task.new(:features) do |t|
19
+ t.fork = true
20
+ t.set_profile_for_current_ruby
21
+ end
22
+
23
+ task :cucumber => :features
@@ -0,0 +1,12 @@
1
+ require 'rdoc/task'
2
+
3
+ desc "builds documentation for the 'rs_232' gem"
4
+ Rake::RDocTask.new :doc do |rd|
5
+
6
+ base = File.expand_path("../", File.dirname(__FILE__))
7
+ base.sub!(/^#{Regexp::escape FileUtils.pwd}\/?/, './')
8
+
9
+ rd.main = File.join(base, "README.md")
10
+ rd.rdoc_files.include(File.join(base, "README.md"), File.join(base, "lib/**/*.rb"))
11
+ rd.rdoc_dir = "./doc/rs_232"
12
+ end
@@ -0,0 +1,7 @@
1
+ task :ruby_env do
2
+ RUBY_APP = if RUBY_PLATFORM =~ /java/
3
+ "jruby"
4
+ else
5
+ "ruby"
6
+ end unless defined? RUBY_APP
7
+ end
@@ -0,0 +1,6 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ desc "Run RSpec"
4
+ RSpec::Core::RakeTask.new do |t|
5
+ t.verbose = true
6
+ end
@@ -0,0 +1,8 @@
1
+ module Rs232
2
+ module Version
3
+ MAJOR, MINOR, PATCH = 2, 0, 4
4
+ STRING = [MAJOR, MINOR, PATCH].join('.')
5
+ end
6
+
7
+ VERSION = Version::STRING
8
+ end
data/lib/rs_232.rb ADDED
@@ -0,0 +1,145 @@
1
+ require "rs_232/version"
2
+ require "rs_232.so"
3
+
4
+ #== Adapter implementation example, no event only read on-demand:
5
+ #
6
+ # @usage:
7
+ #
8
+ # instantiate adapter
9
+ # +adapter+ = +Serial.new("COM3")+ #=> [Object]
10
+ #
11
+ # write string
12
+ # +adapter.tx("Hello, World\n")+ #=> 13
13
+ #
14
+ # read all available bytes
15
+ # +adapter.rx(-1)+ #=> "Bye bye cruel world\n"
16
+ #
17
+ #
18
+ class Serial
19
+ attr_reader :interface
20
+ include CommPort
21
+
22
+ # == constructor with default params
23
+ # by default port will be configured with:
24
+ #
25
+ # @baud_rate = 115200 # BAUD_115200
26
+ # @data_bits = 8 # DATA_BITS_8
27
+ # @parity = 0 # PAR_NONE
28
+ # @stop_bits = 1 # STOP_1
29
+ # @flow_control = 0 # FLOW_OFF
30
+ #
31
+ #
32
+ def initialize(port)
33
+ @interface = Rs232.new(port)
34
+ connect
35
+ $stdout.puts "*** Rs232 instance has been initialized. Build v#{::Rs232::VERSION}"
36
+ end
37
+
38
+ # Open and configure interface
39
+ #
40
+ # @return [Bool]
41
+ #
42
+ def connect
43
+ @interface.open
44
+ # custom configuration should be there if required
45
+ # @interface.baud_rate = BAUD_115200
46
+ # @interface.data_bits = DATA_BITS_8
47
+ # @interface.parity = PAR_NONE
48
+ # @interface.stop_bits = STOP_1
49
+ # @interface.flow_control = FLOW_OFF
50
+ @open = open?
51
+ end
52
+
53
+ # == Write function implementation
54
+ #
55
+ # @param [String] bytes
56
+ # @return [Int]
57
+ #
58
+ def write(bytes)
59
+ @interface.write(bytes)
60
+ end
61
+
62
+ # == Closing interface and freeing structures
63
+ #
64
+ # @return [Bool]
65
+ #
66
+ def close
67
+ @interface.close
68
+ @open = open?
69
+ !open?
70
+ end
71
+
72
+ # == Flashing buffer function
73
+ #
74
+ def flush
75
+ @interface.flush
76
+ end
77
+
78
+ # @return [Bool]
79
+ #
80
+ def open?
81
+ @interface && !@interface.closed?
82
+ end
83
+
84
+ # == read() implementation example
85
+ #
86
+ # @param +count+ [Int]
87
+ # @param +blocking+ [Bool]
88
+ #
89
+ # @return [String]
90
+ #
91
+ # === Alternative implementation:
92
+ # @usage:
93
+ #
94
+ # +timeout+ = blocking_value ? 15000 : 0
95
+ # +@interface.timeout+ = +timeout+
96
+ # +@interface.read( +count+ )+
97
+ #
98
+ def read(count, blocking = false)
99
+ array = []
100
+
101
+ bytes_count = (count == -1) ? @interface.available? : count
102
+
103
+ if blocking
104
+ bytes = read_io_until(count, count)
105
+ array.push bytes if bytes
106
+ else
107
+ bytes_count.times do
108
+ byte = @interface.read(1)
109
+ array.push byte if byte
110
+ end
111
+ end
112
+ array.empty? ? nil : array.join
113
+ end
114
+
115
+ private
116
+
117
+ # == simulate blocking function
118
+ #
119
+ # @param +count+ [Int]
120
+ # @param +up_to+ [Int]
121
+ #
122
+ # no direct ruby usage
123
+ #
124
+ def block_io_until(count, up_to)
125
+ while @interface.available? < count && up_to > 0
126
+ up_to -= 1
127
+ end
128
+ up_to > 0
129
+ end
130
+
131
+ # == simulate blocking function
132
+ #
133
+ # @param +count+ [Int]
134
+ # @param +up_to+ [Int]
135
+ #
136
+ # no direct ruby usage
137
+ #
138
+ def read_io_until(count, up_to)
139
+ until block_io_until(count, up_to)
140
+ sleep 0.001
141
+ end
142
+ read(count)
143
+ end
144
+
145
+ end
data/rs_232.gemspec ADDED
@@ -0,0 +1,40 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rs_232/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'rs_232'
8
+ spec.version = Rs232::VERSION
9
+ spec.authors = ['Roman Lishtaba']
10
+ spec.email = ['roman@lishtaba.com']
11
+ spec.description = %q{rs-232 lib}
12
+ spec.summary = %q{rs-232 lib gem summary}
13
+ spec.homepage = 'http://www.lishtaba.com'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = Dir['**/*']
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.extensions = %w(ext/rs_232/extconf.rb)
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.3'
24
+ spec.add_development_dependency 'rake', '~> 0'
25
+ spec.add_development_dependency 'rspec', '~> 0'
26
+ spec.add_development_dependency 'cucumber', '~> 0'
27
+ spec.add_development_dependency 'rake-compiler', '~> 0'
28
+ spec.add_development_dependency 'simplecov', '~> 0'
29
+ spec.add_development_dependency 'pry', '~> 0'
30
+
31
+
32
+ spec.post_install_message = <<-MSG
33
+ **********************************************************
34
+ *** INFO: ***
35
+ *** You've installed the binary version of Rs-232 gem! ***
36
+ *** Bug reports are welcome: [roman@lishtaba.com] ***
37
+ *** happy codding!!! ***
38
+ **********************************************************
39
+ MSG
40
+ end
@@ -0,0 +1,15 @@
1
+ begin
2
+ # Suppress warnings in order not to pollute stdout which tests expectations rely on
3
+ $VERBOSE = nil if defined?(JRUBY_VERSION)
4
+
5
+ require 'simplecov'
6
+
7
+ SimpleCov.root(File.expand_path(File.dirname(__FILE__) + '/..'))
8
+ SimpleCov.start do
9
+ add_filter '/features/'
10
+ add_filter '/spec/'
11
+ end
12
+
13
+ rescue LoadError
14
+ warn("Unable to load simplecov gem")
15
+ end
@@ -0,0 +1,15 @@
1
+ require 'bundler'
2
+ require 'bundler/setup'
3
+ require 'rspec'
4
+ require 'rs_232'
5
+
6
+ Dir[File.expand_path('support/**/*.rb', File.dirname(__FILE__))].each do |f|
7
+ require f if File.file?(f)
8
+ end
9
+
10
+ RSpec.configure do |config|
11
+ config.color_enabled = true
12
+ config.formatter = :documentation
13
+ end
14
+
15
+ load File.expand_path '../simplecov_setup.rb', __FILE__
@@ -0,0 +1,23 @@
1
+ require 'pathname'
2
+
3
+ module Fixtures
4
+ def fixture_path(name)
5
+ File.join '../fixtures', name
6
+ end
7
+
8
+ def absolute_fixture_path(name)
9
+ File.expand_path fixture_path(name), File.dirname(__FILE__)
10
+ end
11
+
12
+ def fixture(name)
13
+ File.read absolute_fixture_path(name)
14
+ end
15
+
16
+ def binary_fixture(name)
17
+ File.open(absolute_fixture_path(name), "rb") { |f| f.read }
18
+ end
19
+
20
+ def fixtures_path
21
+ Pathname(absolute_fixture_path '')
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,198 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rs_232
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Roman Lishtaba
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: cucumber
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake-compiler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: rs-232 lib
112
+ email:
113
+ - roman@lishtaba.com
114
+ executables:
115
+ - env.rb
116
+ extensions:
117
+ - ext/rs_232/extconf.rb
118
+ extra_rdoc_files: []
119
+ files:
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - bin/env.rb
125
+ - cucumber.yml
126
+ - ext/rs_232/constants.c
127
+ - ext/rs_232/constants.h
128
+ - ext/rs_232/extconf.rb
129
+ - ext/rs_232/initializer.c
130
+ - ext/rs_232/initializer.h
131
+ - ext/rs_232/posix/port.c
132
+ - ext/rs_232/posix/port.h
133
+ - ext/rs_232/structs.h
134
+ - ext/rs_232/windows/port.c
135
+ - ext/rs_232/windows/port.h
136
+ - features/config/config.yml
137
+ - features/connection.feature
138
+ - features/step_definitions/connection_steps.rb
139
+ - features/support/adapter.rb
140
+ - features/support/adapter/dev.rb
141
+ - features/support/adapter/generic.rb
142
+ - features/support/adapter/rs_logger.rb
143
+ - features/support/after.rb
144
+ - features/support/env.rb
145
+ - gem_tasks/cov.rake
146
+ - gem_tasks/cucumber.rake
147
+ - gem_tasks/doc.rake
148
+ - gem_tasks/environment.rake
149
+ - gem_tasks/rspec.rake
150
+ - lib/rs_232.rb
151
+ - lib/rs_232/version.rb
152
+ - rs_232.gemspec
153
+ - spec/simplecov_setup.rb
154
+ - spec/spec_helper.rb
155
+ - spec/support/fixtures.rb
156
+ homepage: http://www.lishtaba.com
157
+ licenses:
158
+ - MIT
159
+ metadata: {}
160
+ post_install_message: |2
161
+ **********************************************************
162
+ *** INFO: ***
163
+ *** You've installed the binary version of Rs-232 gem! ***
164
+ *** Bug reports are welcome: [roman@lishtaba.com] ***
165
+ *** happy codding!!! ***
166
+ **********************************************************
167
+ rdoc_options: []
168
+ require_paths:
169
+ - lib
170
+ required_ruby_version: !ruby/object:Gem::Requirement
171
+ requirements:
172
+ - - ">="
173
+ - !ruby/object:Gem::Version
174
+ version: '0'
175
+ required_rubygems_version: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - ">="
178
+ - !ruby/object:Gem::Version
179
+ version: '0'
180
+ requirements: []
181
+ rubyforge_project:
182
+ rubygems_version: 2.2.2
183
+ signing_key:
184
+ specification_version: 4
185
+ summary: rs-232 lib gem summary
186
+ test_files:
187
+ - features/config/config.yml
188
+ - features/connection.feature
189
+ - features/step_definitions/connection_steps.rb
190
+ - features/support/adapter.rb
191
+ - features/support/adapter/dev.rb
192
+ - features/support/adapter/generic.rb
193
+ - features/support/adapter/rs_logger.rb
194
+ - features/support/after.rb
195
+ - features/support/env.rb
196
+ - spec/simplecov_setup.rb
197
+ - spec/spec_helper.rb
198
+ - spec/support/fixtures.rb