rs_232 2.0.5 → 2.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rs_232.rb CHANGED
@@ -1,145 +1,175 @@
1
- require "rs_232/version"
2
- require "rs_232.so"
1
+ require 'rs_232/version'
2
+ require 'rs_232.so'
3
3
 
4
- #== Adapter implementation example, no event only read on-demand:
4
+ module Rs232
5
+
6
+ #== Adapter implementation example:
5
7
  #
6
8
  # @usage:
7
9
  #
8
- # instantiate adapter
9
- # +adapter+ = +Serial.new("COM3")+ #=> [Object]
10
+ # # instantiate adapter
11
+ # +adapter+ = +Rs232::Adapter.new("/dev/tty.ACM0")+ #=> [Object]
10
12
  #
11
- # write string
12
- # +adapter.tx("Hello, World\n")+ #=> 13
13
+ # # write string
14
+ # +adapter.write("Hello, World\n")+ #=> 13
13
15
  #
14
- # read all available bytes
15
- # +adapter.rx(-1)+ #=> "Bye bye cruel world\n"
16
+ # # read all available bytes
17
+ # +adapter.read(-1)+ #=> "Bye bye cruel world\n"
16
18
  #
17
19
  #
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
20
+ class Adapter
21
+ attr_reader :interface
22
+ # == Top level module ::CommPort constants
23
+ #
24
+ # :VERSION,
25
+ #
26
+ # :BAUD_110,
27
+ # :BAUD_300,
28
+ # :BAUD_600,
29
+ # :BAUD_1200,
30
+ # :BAUD_2400,
31
+ # :BAUD_4800,
32
+ # :BAUD_9600,
33
+ # :BAUD_19200,
34
+ # :BAUD_38400,
35
+ # :BAUD_57600,
36
+ # :BAUD_115200,
37
+ #
38
+ # :DATA_BITS_5,
39
+ # :DATA_BITS_6,
40
+ # :DATA_BITS_7,
41
+ # :DATA_BITS_8,
42
+ #
43
+ # :PAR_NONE,
44
+ # :PAR_ODD,
45
+ # :PAR_EVEN,
46
+ #
47
+ # :STOP_BITS_1,
48
+ # :STOP_BITS_3,
49
+ #
50
+ # :FLOW_OFF,
51
+ # :FLOW_HARDWARE,
52
+ # :FLOW_XONXOFF,
53
+ #
54
+ # :Impl
55
+ #
37
56
 
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
57
 
53
- # == Write function implementation
54
- #
55
- # @param [String] bytes
56
- # @return [Int]
57
- #
58
- def write(bytes)
59
- @interface.write(bytes)
60
- end
58
+ # == constructor with default params
59
+ #
60
+ def initialize(port)
61
+ @interface = CommPort::Rs232.new(port)
62
+ connect
63
+ end
61
64
 
62
- # == Closing interface and freeing structures
63
- #
64
- # @return [Bool]
65
- #
66
- def close
67
- @interface.close
68
- @open = open?
69
- !open?
70
- end
65
+ # Open and configure interface
66
+ #
67
+ # @return [Bool]
68
+ #
69
+ def connect
70
+ @interface.open
71
71
 
72
- # == Flashing buffer function
73
- #
74
- def flush
75
- @interface.flush
76
- end
72
+ @interface.baud_rate = CommPort::BAUD_115200
73
+ @interface.data_bits = CommPort::DATA_BITS_8
74
+ @interface.parity = CommPort::PAR_NONE
75
+ @interface.stop_bits = CommPort::STOP_BITS_1
76
+ @interface.flow_control = CommPort::FLOW_OFF
77
77
 
78
- # @return [Bool]
79
- #
80
- def open?
81
- @interface && !@interface.closed?
82
- end
78
+ @open = open?
79
+ end
80
+
81
+ # == Write function implementation
82
+ #
83
+ # @param [String] bytes
84
+ # @return [Int]
85
+ #
86
+ def write(bytes)
87
+ @interface.write(bytes)
88
+ end
89
+
90
+ # == Closing interface and freeing structures
91
+ #
92
+ # @return [Bool]
93
+ #
94
+ def close
95
+ @interface.close
96
+ @open = open?
97
+ !open?
98
+ end
83
99
 
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
100
+ # == Flashing buffer function
101
+ #
102
+ def flush
103
+ @interface.flush
104
+ end
105
+
106
+ # @return [Bool]
107
+ #
108
+ def open?
109
+ @interface && !@interface.closed?
110
+ end
111
+
112
+ # == read() implementation example
113
+ #
114
+ # @param +count+ [Int]
115
+ # @param +blocking+ [Bool]
116
+ #
117
+ # @return [String]
118
+ #
119
+ # === Alternative implementation:
120
+ # @usage:
121
+ #
122
+ # +timeout+ = blocking_value ? 15000 : 0
123
+ # +@interface.timeout+ = +timeout+
124
+ # +@interface.read( +count+ )+
125
+ #
126
+ def read(count, blocking = false)
127
+ array = []
128
+
129
+ bytes_count = (count == -1) ? @interface.available? : count
130
+
131
+ if blocking
132
+ bytes = read_io_until(count, count)
133
+ array.push bytes if bytes
134
+ else
135
+ bytes_count.times do
136
+ byte = @interface.read(1)
137
+ array.push byte if byte
138
+ end
110
139
  end
140
+ array.empty? ? nil : array.join
111
141
  end
112
- array.empty? ? nil : array.join
113
- end
114
142
 
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
143
+ private
144
+
145
+ # == simulate blocking function
146
+ #
147
+ # @param +count+ [Int]
148
+ # @param +up_to+ [Int]
149
+ #
150
+ # no direct ruby usage
151
+ #
152
+ def block_io_until(count, up_to)
153
+ while @interface.available? < count && up_to > 0
154
+ up_to -= 1
155
+ end
156
+ up_to > 0
127
157
  end
128
- up_to > 0
129
- end
130
158
 
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
159
+ # == simulate blocking function
160
+ #
161
+ # @param +count+ [Int]
162
+ # @param +up_to+ [Int]
163
+ #
164
+ # no direct ruby usage
165
+ #
166
+ def read_io_until(count, up_to)
167
+ until block_io_until(count, up_to)
168
+ sleep 0.001
169
+ end
170
+ read(count)
141
171
  end
142
- read(count)
172
+
143
173
  end
144
174
 
145
175
  end
data/rs_232.gemspec CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
8
8
  spec.version = Rs232::VERSION
9
9
  spec.authors = ['Roman Lishtaba']
10
10
  spec.email = ['roman@lishtaba.com']
11
- spec.description = %q{rs-232 native extension library should help you use serial port devices withing the Darwin, Linux, Windows platforms}
12
- spec.summary = %q{rs-232 native extension library written on C}
11
+ spec.description = %q{This is a rs-232 posix implementation as a Ruby extension in C.}
12
+ spec.summary = %q{This is a rs-232 implementation as a Ruby extension in C.}
13
13
  spec.homepage = 'http://www.lishtaba.com'
14
14
  spec.license = 'MIT'
15
15
 
@@ -20,22 +20,21 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.extensions = %w(ext/rs_232/extconf.rb)
22
22
 
23
- spec.add_development_dependency 'bundler', '>= 1.5.3'
24
- spec.add_development_dependency 'rake', '>= 10.1.1'
25
- spec.add_development_dependency 'rspec', '>= 2.14.1'
26
- spec.add_development_dependency 'cucumber', '>= 1.3.11'
27
- spec.add_development_dependency 'rake-compiler', '>= 0.9.2'
28
- spec.add_development_dependency 'simplecov', '>= 0.8.2'
29
- spec.add_development_dependency 'pry', '>= 0.9.12.6'
23
+ spec.add_development_dependency 'bundler','~> 1.5', '>= 1.5.3'
24
+ spec.add_development_dependency 'rake', '~> 10.1', '>= 10.1.1'
25
+ spec.add_development_dependency 'rspec', '~> 2.14', '>= 2.14.1'
26
+ spec.add_development_dependency 'cucumber', '~> 1.3', '>= 1.3.11'
27
+ spec.add_development_dependency 'rake-compiler', '~> 0.9', '>= 0.9.2'
28
+ spec.add_development_dependency 'simplecov', '~> 0.8', '>= 0.8.2'
29
+ spec.add_development_dependency 'pry', '~> 0.9', '>= 0.9.12.6'
30
30
 
31
31
 
32
32
  spec.post_install_message = <<-MSG
33
33
  ****************************************************************
34
34
  *** INFO: ***
35
- *** You've installed the binary version of Rs-232 gem! ***
35
+ *** You've installed binary version of the Rs-232 gem! ***
36
36
  *** Gem version: #{Rs232::VERSION} ***
37
37
  *** Bug reports/questions are welcome: [roman@lishtaba.com] ***
38
- *** happy codding!!! ***
39
38
  ****************************************************************
40
39
  MSG
41
40
  end
data/rs_232.iml ADDED
@@ -0,0 +1,30 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="RUBY_MODULE" version="4">
3
+ <component name="NewModuleRootManager" inherit-compiler-output="true">
4
+ <exclude-output />
5
+ <content url="file://$MODULE_DIR$" />
6
+ <orderEntry type="jdk" jdkName="RVM: ruby-2.1.1" jdkType="RUBY_SDK" />
7
+ <orderEntry type="sourceFolder" forTests="false" />
8
+ <orderEntry type="library" scope="PROVIDED" name="builder (v3.2.2, RVM: ruby-2.1.1) [gem]" level="application" />
9
+ <orderEntry type="library" scope="PROVIDED" name="bundler (v1.5.3, RVM: ruby-2.1.1) [gem]" level="application" />
10
+ <orderEntry type="library" scope="PROVIDED" name="coderay (v1.1.0, RVM: ruby-2.1.1) [gem]" level="application" />
11
+ <orderEntry type="library" scope="PROVIDED" name="cucumber (v1.3.11, RVM: ruby-2.1.1) [gem]" level="application" />
12
+ <orderEntry type="library" scope="PROVIDED" name="diff-lcs (v1.2.5, RVM: ruby-2.1.1) [gem]" level="application" />
13
+ <orderEntry type="library" scope="PROVIDED" name="docile (v1.1.3, RVM: ruby-2.1.1) [gem]" level="application" />
14
+ <orderEntry type="library" scope="PROVIDED" name="gherkin (v2.12.2, RVM: ruby-2.1.1) [gem]" level="application" />
15
+ <orderEntry type="library" scope="PROVIDED" name="method_source (v0.8.2, RVM: ruby-2.1.1) [gem]" level="application" />
16
+ <orderEntry type="library" scope="PROVIDED" name="multi_json (v1.9.0, RVM: ruby-2.1.1) [gem]" level="application" />
17
+ <orderEntry type="library" scope="PROVIDED" name="multi_test (v0.0.3, RVM: ruby-2.1.1) [gem]" level="application" />
18
+ <orderEntry type="library" scope="PROVIDED" name="pry (v0.9.12.6, RVM: ruby-2.1.1) [gem]" level="application" />
19
+ <orderEntry type="library" scope="PROVIDED" name="rake (v10.1.1, RVM: ruby-2.1.1) [gem]" level="application" />
20
+ <orderEntry type="library" scope="PROVIDED" name="rake-compiler (v0.9.2, RVM: ruby-2.1.1) [gem]" level="application" />
21
+ <orderEntry type="library" scope="PROVIDED" name="rspec (v2.14.1, RVM: ruby-2.1.1) [gem]" level="application" />
22
+ <orderEntry type="library" scope="PROVIDED" name="rspec-core (v2.14.8, RVM: ruby-2.1.1) [gem]" level="application" />
23
+ <orderEntry type="library" scope="PROVIDED" name="rspec-expectations (v2.14.5, RVM: ruby-2.1.1) [gem]" level="application" />
24
+ <orderEntry type="library" scope="PROVIDED" name="rspec-mocks (v2.14.6, RVM: ruby-2.1.1) [gem]" level="application" />
25
+ <orderEntry type="library" scope="PROVIDED" name="simplecov (v0.8.2, RVM: ruby-2.1.1) [gem]" level="application" />
26
+ <orderEntry type="library" scope="PROVIDED" name="simplecov-html (v0.8.0, RVM: ruby-2.1.1) [gem]" level="application" />
27
+ <orderEntry type="library" scope="PROVIDED" name="slop (v3.4.7, RVM: ruby-2.1.1) [gem]" level="application" />
28
+ </component>
29
+ </module>
30
+
@@ -1,22 +1,47 @@
1
1
  class Adapter::Dev < Adapter::Generic
2
2
  attr_reader :interface
3
- include CommPort
4
-
5
- # == constructor with default params
6
- # by default port will be configured with:
3
+ # == Top level module ::CommPort constants
4
+ #
5
+ # :VERSION,
6
+ #
7
+ # :BAUD_110,
8
+ # :BAUD_300,
9
+ # :BAUD_600,
10
+ # :BAUD_1200,
11
+ # :BAUD_2400,
12
+ # :BAUD_4800,
13
+ # :BAUD_9600,
14
+ # :BAUD_19200,
15
+ # :BAUD_38400,
16
+ # :BAUD_57600,
17
+ # :BAUD_115200,
18
+ #
19
+ # :DATA_BITS_5,
20
+ # :DATA_BITS_6,
21
+ # :DATA_BITS_7,
22
+ # :DATA_BITS_8,
7
23
  #
8
- # @baud_rate = 115200 # BAUD_115200
9
- # @data_bits = 8 # DATA_BITS_8
10
- # @parity = 0 # PAR_NONE
11
- # @stop_bits = 1 # STOP_1
12
- # @flow_control = 0 # FLOW_OFF
24
+ # :PAR_NONE,
25
+ # :PAR_ODD,
26
+ # :PAR_EVEN,
13
27
  #
28
+ # :STOP_BITS_1,
29
+ # :STOP_BITS_3,
30
+ #
31
+ # :FLOW_OFF,
32
+ # :FLOW_HARDWARE,
33
+ # :FLOW_XONXOFF,
34
+ #
35
+ # :Impl
36
+ #
37
+
38
+
39
+ # == constructor with default params
14
40
  #
15
41
  def initialize(port, &block)
16
- @interface ||= Rs232.new(port)
42
+ @interface = CommPort::Rs232.new(port)
43
+ super
17
44
  connect
18
- $stdout.puts "*** Rs232 instance has been initialized. Build v#{CommPort::VERSION}"
19
- super(&block)
20
45
  end
21
46
 
22
47
  # Open and configure interface
@@ -25,12 +50,15 @@ class Adapter::Dev < Adapter::Generic
25
50
  #
26
51
  def connect
27
52
  @interface.open
28
- # custom configuration should be there if required
29
- # @interface.baud_rate = BAUD_115200
30
- # @interface.data_bits = DATA_BITS_8
31
- # @interface.parity = PAR_NONE
32
- # @interface.stop_bits = STOP_1
33
- # @interface.flow_control = FLOW_OFF
53
+
54
+ notify_with '%s has been received call to %s...' % [self, __method__]
55
+
56
+ @interface.baud_rate = CommPort::BAUD_115200
57
+ @interface.data_bits = CommPort::DATA_BITS_8
58
+ @interface.parity = CommPort::PAR_NONE
59
+ @interface.stop_bits = CommPort::STOP_BITS_1
60
+ @interface.flow_control = CommPort::FLOW_OFF
61
+
34
62
  @open = open?
35
63
  end
36
64
 
@@ -48,7 +76,8 @@ class Adapter::Dev < Adapter::Generic
48
76
  # @return [Bool]
49
77
  #
50
78
  def close
51
- @interface.flush
79
+ notify_with '%s has been received call to %s...' % [self, __method__]
80
+
52
81
  @interface.close
53
82
  @open = open?
54
83
  !open?
@@ -57,6 +86,8 @@ class Adapter::Dev < Adapter::Generic
57
86
  # == Flashing buffer function
58
87
  #
59
88
  def flush
89
+ notify_with '%s has been received call to %s...' % [self, __method__]
90
+
60
91
  @interface.flush
61
92
  end
62
93
 
@@ -81,7 +112,7 @@ class Adapter::Dev < Adapter::Generic
81
112
  # +@interface.read( +count+ )+
82
113
  #
83
114
  def read(count, blocking = false)
84
- array = []
115
+ array = []
85
116
 
86
117
  bytes_count = (count == -1) ? @interface.available? : count
87
118
 
@@ -4,20 +4,27 @@ module Adapter
4
4
 
5
5
  class Generic
6
6
  include MonitorMixin
7
- include RsLogger
8
7
 
9
- attr_accessor :event
8
+ DEFAULT_NOTIFIER = ->(message) { $stdout.puts message }
9
+
10
+ attr_accessor :notifier
10
11
 
11
12
  def initialize(*args, &block)
12
13
  Thread.abort_on_exception = true
13
- unless instance_variables.include?(:@rxd)
14
- @rxd = true
15
- end
16
- @event = block
17
- super(*args)
14
+ @rxd = true unless instance_variables.include?(:@rxd)
15
+ @notifier = block || DEFAULT_NOTIFIER
16
+ super()
18
17
  run
19
18
  end
20
19
 
20
+ def notifier=(value)
21
+ @notifier = value
22
+ end
23
+
24
+ def notifier
25
+ @notifier
26
+ end
27
+
21
28
  def reading_allowed?
22
29
  @rxd
23
30
  end
@@ -34,14 +41,14 @@ module Adapter
34
41
  def rx(int, blocking = false)
35
42
  byte = read(int, blocking)
36
43
  if byte
37
- logger.debug "RX [#{byte.length}]: #{byte.inspect}"
44
+ notify_with "RX [#{byte.length}]: #{byte.inspect}"
38
45
  end
39
46
  byte
40
47
  end
41
48
 
42
49
  def tx(bytes)
43
50
  int = write(bytes)
44
- logger.debug "TX [#{int}]: #{bytes.inspect}"
51
+ notify_with "TX [#{int}]: #{bytes.inspect}"
45
52
  int
46
53
  end
47
54
 
@@ -59,6 +66,12 @@ module Adapter
59
66
  end
60
67
  end
61
68
 
69
+ private
70
+
71
+ def notify_with(message)
72
+ @notifier[message]
73
+ end
74
+
62
75
  end
63
76
 
64
77
  end
@@ -0,0 +1,4 @@
1
+ module Adapter
2
+ require_relative 'adapter/generic'
3
+ require_relative 'adapter/dev'
4
+ end