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.
- checksums.yaml +4 -4
- data/Gemfile.lock +8 -8
- data/README.md +183 -42
- data/atlassian-ide-plugin.xml +5 -0
- data/cucumber.yml +3 -3
- data/ext/rs_232/constants.h +1 -1
- data/ext/rs_232/extconf.rb +29 -62
- data/features/connection.feature +6 -4
- data/features/support/after.rb +1 -2
- data/features/support/env.rb +11 -43
- data/gem_tasks/environment.rake +4 -4
- data/gem_tasks/rspec.rake +1 -1
- data/lib/rs_232/version.rb +1 -1
- data/lib/rs_232.rb +152 -122
- data/rs_232.gemspec +10 -11
- data/rs_232.iml +30 -0
- data/{features → spec}/support/adapter/dev.rb +51 -20
- data/{features → spec}/support/adapter/generic.rb +22 -9
- data/spec/support/adapter.rb +4 -0
- metadata +57 -20
- data/bin/env.rb +0 -9
- data/features/support/adapter/rs_logger.rb +0 -36
- data/features/support/adapter.rb +0 -5
- data/pkg/rs_232-2.0.5.gem +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0b19997f03f98c0496bda194020c1917d87bc185
|
4
|
+
data.tar.gz: 0bbca6b87935a65a3eb780b54eaccc8c0c988f00
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a683dfd3c2aa42c3399f6fa520059657a6baf76e0299c588588fa10b59b3a70eec4ffe57afed94d0391d8dc6bb3dd38665a9f186e5260eb8592f24fc1c902ece
|
7
|
+
data.tar.gz: 4c1167898a7ce5db45fdc7a4e666fc4d3fee19b0693e6e3c566ea9696dbbcef73217c0d4936cbd498b7687e8a3b67d01f82700c77908d745d30a83d2083d26c2
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
rs_232 (2.0.
|
4
|
+
rs_232 (2.0.6)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -47,11 +47,11 @@ PLATFORMS
|
|
47
47
|
ruby
|
48
48
|
|
49
49
|
DEPENDENCIES
|
50
|
-
bundler (>= 1.5.3)
|
51
|
-
cucumber (>= 1.3.11)
|
52
|
-
pry (>= 0.9.12.6)
|
53
|
-
rake (>= 10.1.1)
|
54
|
-
rake-compiler (>= 0.9.2)
|
50
|
+
bundler (~> 1.5, >= 1.5.3)
|
51
|
+
cucumber (~> 1.3, >= 1.3.11)
|
52
|
+
pry (~> 0.9, >= 0.9.12.6)
|
53
|
+
rake (~> 10.1, >= 10.1.1)
|
54
|
+
rake-compiler (~> 0.9, >= 0.9.2)
|
55
55
|
rs_232!
|
56
|
-
rspec (>= 2.14.1)
|
57
|
-
simplecov (>= 0.8.2)
|
56
|
+
rspec (~> 2.14, >= 2.14.1)
|
57
|
+
simplecov (~> 0.8, >= 0.8.2)
|
data/README.md
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
# Rs232
|
2
2
|
|
3
|
-
|
3
|
+
This is a rs-232 implementation as a Ruby extension in C.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
7
7
|
Add this line to your application's Gemfile:
|
8
8
|
|
9
|
-
gem '
|
9
|
+
gem 'rs_232'
|
10
10
|
|
11
11
|
And then execute:
|
12
12
|
|
@@ -14,51 +14,192 @@ And then execute:
|
|
14
14
|
|
15
15
|
Or install it yourself as:
|
16
16
|
|
17
|
-
$ gem install
|
17
|
+
$ gem install rs_232
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
21
|
```ruby
|
22
|
+
|
22
23
|
require "rs_232"
|
23
24
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
25
|
+
###### Windows
|
26
|
+
|
27
|
+
@adapter = Rs232::Adapter.new("COM3")
|
28
|
+
|
29
|
+
###### Linux
|
30
|
+
|
31
|
+
@adapter = Rs232::Adapter.new("/dev/tty.usb00000")
|
32
|
+
|
33
|
+
###### Darwin
|
34
|
+
|
35
|
+
@adapter = Rs232::Adapter.new("/dev/ttyACM0")
|
36
|
+
|
37
|
+
###### or implement your own adapter, here is example
|
38
|
+
|
39
|
+
class MyAdapter
|
40
|
+
attr_reader :interface
|
41
|
+
|
42
|
+
# == Top level module ::CommPort constants
|
43
|
+
#
|
44
|
+
# :VERSION,
|
45
|
+
#
|
46
|
+
# :BAUD_110,
|
47
|
+
# :BAUD_300,
|
48
|
+
# :BAUD_600,
|
49
|
+
# :BAUD_1200,
|
50
|
+
# :BAUD_2400,
|
51
|
+
# :BAUD_4800,
|
52
|
+
# :BAUD_9600,
|
53
|
+
# :BAUD_19200,
|
54
|
+
# :BAUD_38400,
|
55
|
+
# :BAUD_57600,
|
56
|
+
# :BAUD_115200,
|
57
|
+
#
|
58
|
+
# :DATA_BITS_5,
|
59
|
+
# :DATA_BITS_6,
|
60
|
+
# :DATA_BITS_7,
|
61
|
+
# :DATA_BITS_8,
|
62
|
+
#
|
63
|
+
# :PAR_NONE,
|
64
|
+
# :PAR_ODD,
|
65
|
+
# :PAR_EVEN,
|
66
|
+
#
|
67
|
+
# :STOP_BITS_1,
|
68
|
+
# :STOP_BITS_3,
|
69
|
+
#
|
70
|
+
# :FLOW_OFF,
|
71
|
+
# :FLOW_HARDWARE,
|
72
|
+
# :FLOW_XONXOFF,
|
73
|
+
#
|
74
|
+
# :Impl
|
75
|
+
#
|
76
|
+
|
77
|
+
|
78
|
+
# == constructor with default params
|
79
|
+
#
|
80
|
+
def initialize(port)
|
81
|
+
@interface = CommPort::Rs232.new(port)
|
82
|
+
connect
|
83
|
+
end
|
84
|
+
|
85
|
+
# Open and configure interface
|
86
|
+
#
|
87
|
+
# @return [Bool]
|
88
|
+
#
|
89
|
+
def connect
|
90
|
+
@interface.open
|
91
|
+
|
92
|
+
@interface.baud_rate = CommPort::BAUD_115200
|
93
|
+
@interface.data_bits = CommPort::DATA_BITS_8
|
94
|
+
@interface.parity = CommPort::PAR_NONE
|
95
|
+
@interface.stop_bits = CommPort::STOP_BITS_1
|
96
|
+
@interface.flow_control = CommPort::FLOW_OFF
|
97
|
+
|
98
|
+
@open = open?
|
99
|
+
end
|
100
|
+
|
101
|
+
# == Write function implementation
|
102
|
+
#
|
103
|
+
# @param [String] bytes
|
104
|
+
# @return [Int]
|
105
|
+
#
|
106
|
+
def write(bytes)
|
107
|
+
@interface.write(bytes)
|
108
|
+
end
|
109
|
+
|
110
|
+
# == Closing interface and freeing structures
|
111
|
+
#
|
112
|
+
# @return [Bool]
|
113
|
+
#
|
114
|
+
def close
|
115
|
+
@interface.close
|
116
|
+
@open = open?
|
117
|
+
!open?
|
118
|
+
end
|
119
|
+
|
120
|
+
# == Flashing buffer function
|
121
|
+
#
|
122
|
+
def flush
|
123
|
+
@interface.flush
|
124
|
+
end
|
125
|
+
|
126
|
+
# @return [Bool]
|
127
|
+
#
|
128
|
+
def open?
|
129
|
+
@interface && !@interface.closed?
|
130
|
+
end
|
131
|
+
|
132
|
+
# == read() implementation example
|
133
|
+
#
|
134
|
+
# @param +count+ [Int]
|
135
|
+
# @param +blocking+ [Bool]
|
136
|
+
#
|
137
|
+
# @return [String]
|
138
|
+
#
|
139
|
+
# === Alternative implementation:
|
140
|
+
# @usage:
|
141
|
+
#
|
142
|
+
# +timeout+ = blocking_value ? 15000 : 0
|
143
|
+
# +@interface.timeout+ = +timeout+
|
144
|
+
# +@interface.read( +count+ )+
|
145
|
+
#
|
146
|
+
def read(count, blocking = false)
|
147
|
+
array = []
|
148
|
+
|
149
|
+
bytes_count = (count == -1) ? @interface.available? : count
|
150
|
+
|
151
|
+
if blocking
|
152
|
+
bytes = read_io_until(count, count)
|
153
|
+
array.push bytes if bytes
|
154
|
+
else
|
155
|
+
bytes_count.times do
|
156
|
+
byte = @interface.read(1)
|
157
|
+
array.push byte if byte
|
158
|
+
end
|
159
|
+
end
|
160
|
+
array.empty? ? nil : array.join
|
161
|
+
end
|
162
|
+
|
163
|
+
private
|
164
|
+
|
165
|
+
# == simulate blocking function
|
166
|
+
#
|
167
|
+
# @param +count+ [Int]
|
168
|
+
# @param +up_to+ [Int]
|
169
|
+
#
|
170
|
+
# no direct ruby usage
|
171
|
+
#
|
172
|
+
def block_io_until(count, up_to)
|
173
|
+
while @interface.available? < count && up_to > 0
|
174
|
+
up_to -= 1
|
175
|
+
end
|
176
|
+
up_to > 0
|
177
|
+
end
|
178
|
+
|
179
|
+
# == simulate blocking function
|
180
|
+
#
|
181
|
+
# @param +count+ [Int]
|
182
|
+
# @param +up_to+ [Int]
|
183
|
+
#
|
184
|
+
# no direct ruby usage
|
185
|
+
#
|
186
|
+
def read_io_until(count, up_to)
|
187
|
+
until block_io_until(count, up_to)
|
188
|
+
sleep 0.001
|
189
|
+
end
|
190
|
+
read(count)
|
191
|
+
end
|
192
|
+
|
193
|
+
end
|
194
|
+
|
195
|
+
###### Then =>
|
196
|
+
|
197
|
+
adapter = MyAdapter.new("/dev/ttyACM0")
|
198
|
+
|
199
|
+
adapter.open? #=> true
|
200
|
+
|
201
|
+
adapter.write 'Hello, world!" #=> 13
|
62
202
|
|
203
|
+
```
|
63
204
|
|
64
|
-
## Contributing
|
205
|
+
## Contributing
|
data/cucumber.yml
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
rerun = File.file?('rerun.txt') ? IO.read('rerun.txt') : ""
|
4
4
|
rerun_opts = rerun.to_s.strip.empty? ? "--format progress features" : "--format pretty #{rerun}"
|
5
|
-
std_opts = "--format pretty features --tags ~@wip
|
5
|
+
std_opts = "--format pretty features --tags ~@wip -r features --strict"
|
6
6
|
|
7
7
|
|
8
8
|
wip_opts = "--color -r features --tags @wip:3,@wip-new-core"
|
@@ -14,8 +14,8 @@ legacy_opts << " --tags ~@wip-jruby" if defined?(JRUBY_VERSION)
|
|
14
14
|
|
15
15
|
%>
|
16
16
|
|
17
|
-
default: <%= std_opts %> --tags ~@jruby features
|
18
|
-
windows_mri: <%= std_opts %> --tags ~@jruby
|
17
|
+
default: <%= std_opts %> --tags ~@jruby features
|
18
|
+
windows_mri: <%= std_opts %> --tags ~@jruby
|
19
19
|
|
20
20
|
ruby_1_9: <%= std_opts %> --tags ~@jruby
|
21
21
|
ruby_2_0: <%= std_opts %> --tags ~@jruby
|
data/ext/rs_232/constants.h
CHANGED
data/ext/rs_232/extconf.rb
CHANGED
@@ -2,8 +2,8 @@ require 'mkmf'
|
|
2
2
|
require 'rbconfig'
|
3
3
|
|
4
4
|
if ENV['DEBUG_C']
|
5
|
-
$CFLAGS <<
|
6
|
-
$CFLAGS <<
|
5
|
+
$CFLAGS << ' -DDEBUG'
|
6
|
+
$CFLAGS << ' -g -O'
|
7
7
|
$stdout.puts "compiling in debug mode... flags: #{$CFLAGS}"
|
8
8
|
end
|
9
9
|
|
@@ -11,82 +11,49 @@ def root(path)
|
|
11
11
|
File.expand_path("../#{path}/", __FILE__)
|
12
12
|
end
|
13
13
|
|
14
|
-
dir_config(
|
14
|
+
dir_config('rs_232')
|
15
15
|
|
16
16
|
$warnflags = '-Wall'
|
17
17
|
|
18
|
-
CPU = case RbConfig::CONFIG['host_cpu'].downcase
|
19
|
-
when /i[3456]86/
|
20
|
-
if RbConfig::CONFIG['host_os'] =~ /darwin/ && 0xfee1deadbeef.is_a?(Fixnum)
|
21
|
-
"x86_64"
|
22
|
-
else
|
23
|
-
"i386"
|
24
|
-
end
|
25
|
-
|
26
|
-
when /amd64|x86_64/
|
27
|
-
"x86_64"
|
28
|
-
|
29
|
-
when /ppc64|powerpc64/
|
30
|
-
"powerpc64"
|
31
|
-
|
32
|
-
when /ppc|powerpc/
|
33
|
-
"powerpc"
|
34
|
-
|
35
|
-
when /^arm/
|
36
|
-
"arm"
|
37
|
-
|
38
|
-
else
|
39
|
-
RbConfig::CONFIG['host_cpu']
|
40
|
-
end
|
41
|
-
|
42
|
-
$stdout.puts "Detecting CPU... "
|
43
|
-
$stdout.puts "CPU: #{CPU}"
|
44
|
-
|
45
18
|
OS = case RbConfig::CONFIG['host_os'].downcase
|
46
19
|
when /linux/
|
47
|
-
|
20
|
+
'linux'
|
48
21
|
when /darwin/
|
49
|
-
|
22
|
+
'darwin'
|
50
23
|
when /freebsd/
|
51
|
-
|
24
|
+
'freebsd'
|
52
25
|
when /openbsd/
|
53
|
-
|
26
|
+
'openbsd'
|
54
27
|
when /sunos|solaris/
|
55
|
-
|
28
|
+
'solaris'
|
56
29
|
when /mswin|mingw/
|
57
|
-
|
30
|
+
'windows'
|
58
31
|
else
|
59
32
|
RbConfig::CONFIG['host_os'].downcase
|
60
33
|
end
|
61
34
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
have_header(
|
69
|
-
have_header(
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
$INCFLAGS += " -I$(srcdir)/windows"
|
74
|
-
have_header("windows.h")
|
75
|
-
have_header("fcntl.h")
|
76
|
-
have_header("io.h")
|
77
|
-
have_header("ruby/io.h")
|
78
|
-
have_header("rubyio.h")
|
35
|
+
have_header('ruby.h')
|
36
|
+
have_header('stdio.h')
|
37
|
+
|
38
|
+
if OS == 'windows'
|
39
|
+
$VPATH << '$(srcdir)/windows'
|
40
|
+
$INCFLAGS += ' -I$(srcdir)/windows'
|
41
|
+
have_header('windows.h')
|
42
|
+
have_header('fcntl.h')
|
43
|
+
have_header('io.h')
|
44
|
+
have_header('ruby/io.h')
|
45
|
+
have_header('rubyio.h')
|
79
46
|
elsif %w(linux darwin).include? OS
|
80
|
-
$VPATH <<
|
81
|
-
$INCFLAGS +=
|
82
|
-
have_header(
|
83
|
-
have_header(
|
84
|
-
have_header(
|
85
|
-
have_header(
|
86
|
-
have_header(
|
87
|
-
have_header(
|
47
|
+
$VPATH << ' $(srcdir)/posix'
|
48
|
+
$INCFLAGS += ' -I$(srcdir)/posix'
|
49
|
+
have_header('termios.h')
|
50
|
+
have_header('unistd.h')
|
51
|
+
have_header('string.h')
|
52
|
+
have_header('fcntl.h')
|
53
|
+
have_header('errno.h')
|
54
|
+
have_header('sys/ioctl.h')
|
88
55
|
else
|
89
|
-
raise "RS-233 implementation is not tested for this #{OS}
|
56
|
+
raise "RS-233 implementation is not tested for this #{OS} platform."
|
90
57
|
end
|
91
58
|
|
92
59
|
$objs = %w(constants.o port.o initializer.o)
|
data/features/connection.feature
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
Feature: Connection
|
2
2
|
|
3
|
-
|
3
|
+
As an QA
|
4
|
+
in order to test connection object I will create new one and ensure that it is opened
|
4
5
|
|
6
|
+
Scenario: Should have an ability to check open or not
|
5
7
|
|
6
|
-
|
7
|
-
|
8
|
-
|
8
|
+
Given connection instance "should" be available
|
9
|
+
Then I "close" connection
|
10
|
+
And connection instance "should not" be available
|
data/features/support/after.rb
CHANGED
data/features/support/env.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'cucumber'
|
2
|
-
require '
|
3
|
-
|
4
|
-
require File.expand_path("../adapter", __FILE__)
|
2
|
+
require 'rs_232'
|
3
|
+
require_relative '../../spec/support/adapter'
|
5
4
|
|
6
5
|
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
7
6
|
#
|
@@ -11,33 +10,6 @@ def expand_path(path)
|
|
11
10
|
File.expand_path(path, File.dirname(__FILE__))
|
12
11
|
end
|
13
12
|
|
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
13
|
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
42
14
|
#
|
43
15
|
# Load only if bundled console
|
@@ -63,7 +35,7 @@ class LogFormatter < ::Logger::Formatter
|
|
63
35
|
private
|
64
36
|
|
65
37
|
def format_datetime(time)
|
66
|
-
time.strftime(
|
38
|
+
time.strftime('%Y-%m-%d %H:%M:%S.') << "%06d " % time.usec
|
67
39
|
end
|
68
40
|
|
69
41
|
end
|
@@ -80,21 +52,17 @@ end
|
|
80
52
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
81
53
|
|
82
54
|
def serial_port
|
83
|
-
if ENV[
|
84
|
-
$stdout.write
|
85
|
-
ENV[
|
55
|
+
if ENV['SERIAL_PORT'].nil?
|
56
|
+
$stdout.write 'Enter serial port details: '
|
57
|
+
ENV['SERIAL_PORT'] = $stdin.gets.chomp
|
86
58
|
end
|
87
|
-
ENV[
|
59
|
+
ENV['SERIAL_PORT']
|
88
60
|
end
|
89
61
|
|
90
62
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
91
63
|
|
92
64
|
def adapter
|
93
|
-
@adapter ||= Adapter::Dev.new(serial_port)
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
def transmit(string)
|
99
|
-
adpter.tx(string)
|
100
|
-
end
|
65
|
+
@adapter ||= Adapter::Dev.new(serial_port) do |message|
|
66
|
+
logger.debug message
|
67
|
+
end
|
68
|
+
end
|
data/gem_tasks/environment.rake
CHANGED
data/gem_tasks/rspec.rake
CHANGED