rubyserial-with-blocking 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +4 -0
- data/.rspec +3 -0
- data/.travis.yml +18 -0
- data/Gemfile +5 -0
- data/Guardfile +7 -0
- data/LICENSE +13 -0
- data/README.md +76 -0
- data/Rakefile +2 -0
- data/appveyor.yml +25 -0
- data/lib/rubyserial.rb +21 -0
- data/lib/rubyserial/linux_constants.rb +214 -0
- data/lib/rubyserial/osx_constants.rb +181 -0
- data/lib/rubyserial/posix.rb +140 -0
- data/lib/rubyserial/version.rb +5 -0
- data/lib/rubyserial/windows.rb +104 -0
- data/lib/rubyserial/windows_constants.rb +275 -0
- data/rubyserial.gemspec +21 -0
- data/spec/lib/posix_spec.rb +150 -0
- data/spec/rubyserial_spec.rb +138 -0
- data/spec/spec_helper.rb +86 -0
- metadata +95 -0
data/rubyserial.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
require "rubyserial/version"
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "rubyserial-with-blocking"
|
6
|
+
s.version = RubySerial::VERSION
|
7
|
+
s.summary = "FFI Ruby library for RS-232 serial port communication"
|
8
|
+
s.description = "FFI Ruby library for RS-232 serial port communication"
|
9
|
+
s.homepage = "https://github.com/hybridgroup/rubyserial"
|
10
|
+
s.authors = ["Adrian Zankich", "Theron Boerner", "Javier Cervantes", "Dirk Lüsebrink"]
|
11
|
+
s.platform = Gem::Platform::RUBY
|
12
|
+
s.license = 'Apache 2.0'
|
13
|
+
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
|
18
|
+
s.add_runtime_dependency 'ffi', '~> 1.9.3'
|
19
|
+
|
20
|
+
s.add_development_dependency 'guard-rspec'
|
21
|
+
end
|
@@ -0,0 +1,150 @@
|
|
1
|
+
describe Serial do
|
2
|
+
|
3
|
+
before :each do
|
4
|
+
@master, @slave = PTY.open
|
5
|
+
end
|
6
|
+
|
7
|
+
after :each do
|
8
|
+
#@slave.read_nonblock(255) rescue nil
|
9
|
+
@master.close rescue nil
|
10
|
+
@slave.close rescue nil
|
11
|
+
end
|
12
|
+
|
13
|
+
describe 'new config option settings' do
|
14
|
+
it 'has a queryable config attribute' do
|
15
|
+
expect(Serial.new(@slave.path).config).to be_kind_of(Hash)
|
16
|
+
end
|
17
|
+
it 'has a default baud rate' do
|
18
|
+
expect(Serial.new(@slave.path).config[:baude_rate]).to eq(9600)
|
19
|
+
end
|
20
|
+
it 'overwrites default baude rate' do
|
21
|
+
expect(Serial.new(@slave.path, 57600).config[:baude_rate]).to eq(57600)
|
22
|
+
end
|
23
|
+
it 'overwrites default baude rate with config' do
|
24
|
+
expect(
|
25
|
+
Serial.new(@slave.path, nil, nil, baude_rate: 57600
|
26
|
+
).config[:baude_rate]).to eq(57600)
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'has default data bits' do
|
30
|
+
expect(Serial.new(@slave.path).config[:data_bits]).to eq(8)
|
31
|
+
end
|
32
|
+
it 'overwrites default data bits' do
|
33
|
+
expect(Serial.new(@slave.path, nil, 7).config[:data_bits]).to eq(7)
|
34
|
+
end
|
35
|
+
it 'overwrites default data bits with config' do
|
36
|
+
expect(
|
37
|
+
Serial.new(@slave.path, nil, nil, data_bits: 7
|
38
|
+
).config[:data_bits]).to eq(7)
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'defaults to VMIN = 0' do
|
42
|
+
expect(Serial.new(@slave.path).config[:vmin]).to eq(0)
|
43
|
+
end
|
44
|
+
it 'allows overwriting VMIN' do
|
45
|
+
expect(
|
46
|
+
Serial.new(@slave.path, nil, nil, vmin: 1
|
47
|
+
).config[:vmin]).to eq(1)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe 'blocking read with VMIN=1' do
|
52
|
+
let(:sp) { Serial.new(@slave.path, nil, nil, vmin: 1) }
|
53
|
+
it 'blocks on read' do
|
54
|
+
s = ''
|
55
|
+
c = :start
|
56
|
+
t = Thread.new {
|
57
|
+
s = sp.read(255)
|
58
|
+
expect(s).to eq('hello')
|
59
|
+
c = :end
|
60
|
+
}
|
61
|
+
expect(c).to eq(:start)
|
62
|
+
expect(s).to eq('')
|
63
|
+
@master.write('hello')
|
64
|
+
t.join
|
65
|
+
expect(c).to eq(:end)
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'does not block with default VMIN=0' do
|
69
|
+
c = :start
|
70
|
+
Thread.new {
|
71
|
+
s = sp.read(255)
|
72
|
+
expect(s).to eq('')
|
73
|
+
c = :end
|
74
|
+
}.join
|
75
|
+
expect(c).to eq(:end)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe 'synchronous operation (ported from rubyserial_spec.rb)' do
|
80
|
+
|
81
|
+
let(:sp) { Serial.new @slave.path }
|
82
|
+
|
83
|
+
it "writes" do
|
84
|
+
sp.write('hello')
|
85
|
+
expect(@master.read_nonblock(0xff)).to eql('hello')
|
86
|
+
end
|
87
|
+
it "reads" do
|
88
|
+
@master.write('hello')
|
89
|
+
expect(sp.read(0xff)).to eql('hello')
|
90
|
+
end
|
91
|
+
|
92
|
+
it "converts ints to strings" do
|
93
|
+
expect(sp.write(123)).to eql(3)
|
94
|
+
expect(@master.read_nonblock(3)).to eql('123')
|
95
|
+
end
|
96
|
+
|
97
|
+
it "returns the numbers of bytes written" do
|
98
|
+
expect(sp.write('hello')).to eql(5)
|
99
|
+
end
|
100
|
+
|
101
|
+
it "reading nothing should be blank" do
|
102
|
+
expect(sp.read(5)).to eql('')
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should give me nil on getbyte" do
|
106
|
+
expect(sp.getbyte).to be_nil
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'should give me a zero byte from getbyte' do
|
110
|
+
@master.write("\x00")
|
111
|
+
expect(sp.getbyte).to eql(0)
|
112
|
+
end
|
113
|
+
|
114
|
+
it "should give me bytes" do
|
115
|
+
@master.write('hello')
|
116
|
+
expect([sp.getbyte].pack('C')).to eql('h')
|
117
|
+
end
|
118
|
+
|
119
|
+
describe "giving me lines" do
|
120
|
+
it "should give me a line" do
|
121
|
+
@master.write("no yes \n hello")
|
122
|
+
expect(sp.gets).to eql("no yes \n")
|
123
|
+
end
|
124
|
+
|
125
|
+
it "should accept a sep param" do
|
126
|
+
@master.write('no yes END bleh')
|
127
|
+
expect(sp.gets('END')).to eql("no yes END")
|
128
|
+
end
|
129
|
+
|
130
|
+
it "should accept a limit param" do
|
131
|
+
@master.write("no yes \n hello")
|
132
|
+
expect(sp.gets(4)).to eql("no y")
|
133
|
+
end
|
134
|
+
|
135
|
+
it "should accept limit and sep params" do
|
136
|
+
@master.write("no yes END hello")
|
137
|
+
expect(sp.gets('END', 20)).to eql("no yes END")
|
138
|
+
sp.read(1000) # to clear the device?
|
139
|
+
@master.write("no yes END hello")
|
140
|
+
expect(sp.gets('END', 4)).to eql('no y')
|
141
|
+
end
|
142
|
+
|
143
|
+
it "should read a paragraph at a time" do
|
144
|
+
@master.write("Something \n Something else \n\n and other stuff")
|
145
|
+
expect(sp.gets('')).to eql("Something \n Something else \n\n")
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
end # ~synchronous operation
|
150
|
+
end
|
@@ -0,0 +1,138 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
__END__
|
4
|
+
describe "rubyserial" do
|
5
|
+
before do
|
6
|
+
@ports = []
|
7
|
+
require 'rbconfig'
|
8
|
+
if RbConfig::CONFIG['host_os'] =~ /mswin|windows|mingw/i
|
9
|
+
# NOTE: Tests on windows require com0com
|
10
|
+
# https://github.com/hybridgroup/rubyserial/raw/appveyor_deps/setup_com0com_W7_x64_signed.exe
|
11
|
+
@ports[0] = "\\\\.\\CNCA0"
|
12
|
+
@ports[1] = "\\\\.\\CNCB0"
|
13
|
+
else
|
14
|
+
File.delete('socat.log') if File.file?('socat.log')
|
15
|
+
|
16
|
+
raise 'socat not found' unless (`socat -h` && $? == 0)
|
17
|
+
|
18
|
+
Thread.new do
|
19
|
+
system('socat -lf socat.log -d -d pty,raw,echo=0 pty,raw,echo=0')
|
20
|
+
end
|
21
|
+
|
22
|
+
@ptys = nil
|
23
|
+
|
24
|
+
loop do
|
25
|
+
if File.file? 'socat.log'
|
26
|
+
@file = File.open('socat.log', "r")
|
27
|
+
@fileread = @file.read
|
28
|
+
|
29
|
+
unless @fileread.count("\n") < 3
|
30
|
+
@ptys = @fileread.scan(/PTY is (.*)/)
|
31
|
+
break
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
@ports = [@ptys[1][0], @ptys[0][0]]
|
37
|
+
end
|
38
|
+
|
39
|
+
@sp2 = Serial.new(@ports[0])
|
40
|
+
@sp = Serial.new(@ports[1])
|
41
|
+
end
|
42
|
+
|
43
|
+
after do
|
44
|
+
@sp2.close
|
45
|
+
@sp.close
|
46
|
+
end
|
47
|
+
=begin
|
48
|
+
describe 'blocking read support' do
|
49
|
+
let(:s_in) { @sp }
|
50
|
+
let(:s_out) { @sp2 }
|
51
|
+
it 'hands out the file descriptor object' do
|
52
|
+
expect(s_in.fd).to be_kind_of(Interger)
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should read and write" do
|
56
|
+
s_out.write('hello')
|
57
|
+
check = s_in.read(5)
|
58
|
+
expect(check).to eql('hello')
|
59
|
+
end
|
60
|
+
end
|
61
|
+
=end
|
62
|
+
it "should read and write" do
|
63
|
+
@sp2.write('hello')
|
64
|
+
# small delay so it can write to the other port.
|
65
|
+
sleep 0.1
|
66
|
+
check = @sp.read(5)
|
67
|
+
expect(check).to eql('hello')
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should convert ints to strings" do
|
71
|
+
expect(@sp2.write(123)).to eql(3)
|
72
|
+
sleep 0.1
|
73
|
+
expect(@sp.read(3)).to eql('123')
|
74
|
+
end
|
75
|
+
|
76
|
+
it "write should return bytes written" do
|
77
|
+
expect(@sp2.write('hello')).to eql(5)
|
78
|
+
end
|
79
|
+
|
80
|
+
it "reading nothing should be blank" do
|
81
|
+
expect(@sp.read(5)).to eql('')
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should give me nil on getbyte" do
|
85
|
+
expect(@sp.getbyte).to be_nil
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'should give me a zero byte from getbyte' do
|
89
|
+
@sp2.write("\x00")
|
90
|
+
sleep 0.1
|
91
|
+
expect(@sp.getbyte).to eql(0)
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should give me bytes" do
|
95
|
+
@sp2.write('hello')
|
96
|
+
# small delay so it can write to the other port.
|
97
|
+
sleep 0.1
|
98
|
+
check = @sp.getbyte
|
99
|
+
expect([check].pack('C')).to eql('h')
|
100
|
+
end
|
101
|
+
|
102
|
+
|
103
|
+
describe "giving me lines" do
|
104
|
+
it "should give me a line" do
|
105
|
+
@sp.write("no yes \n hello")
|
106
|
+
sleep 0.1
|
107
|
+
expect(@sp2.gets).to eql("no yes \n")
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should accept a sep param" do
|
111
|
+
@sp.write('no yes END bleh')
|
112
|
+
sleep 0.1
|
113
|
+
expect(@sp2.gets('END')).to eql("no yes END")
|
114
|
+
end
|
115
|
+
|
116
|
+
it "should accept a limit param" do
|
117
|
+
@sp.write("no yes \n hello")
|
118
|
+
sleep 0.1
|
119
|
+
expect(@sp2.gets(4)).to eql("no y")
|
120
|
+
end
|
121
|
+
|
122
|
+
it "should accept limit and sep params" do
|
123
|
+
@sp.write("no yes END hello")
|
124
|
+
sleep 0.1
|
125
|
+
expect(@sp2.gets('END', 20)).to eql("no yes END")
|
126
|
+
@sp2.read(1000)
|
127
|
+
@sp.write("no yes END hello")
|
128
|
+
sleep 0.1
|
129
|
+
expect(@sp2.gets('END', 4)).to eql('no y')
|
130
|
+
end
|
131
|
+
|
132
|
+
it "should read a paragraph at a time" do
|
133
|
+
@sp.write("Something \n Something else \n\n and other stuff")
|
134
|
+
sleep 0.1
|
135
|
+
expect(@sp2.gets('')).to eql("Something \n Something else \n\n")
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
$:.unshift(File.dirname(__FILE__))
|
3
|
+
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
4
|
+
|
5
|
+
require 'rubyserial'
|
6
|
+
require 'pty'
|
7
|
+
|
8
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
9
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
10
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause this
|
11
|
+
# file to always be loaded, without a need to explicitly require it in any files.
|
12
|
+
#
|
13
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
14
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
15
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
16
|
+
# individual file that may not need all of that loaded. Instead, make a
|
17
|
+
# separate helper file that requires this one and then use it only in the specs
|
18
|
+
# that actually need it.
|
19
|
+
#
|
20
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
21
|
+
# users commonly want.
|
22
|
+
#
|
23
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
24
|
+
RSpec.configure do |config|
|
25
|
+
|
26
|
+
# The settings below are suggested to provide a good initial experience
|
27
|
+
# with RSpec, but feel free to customize to your heart's content.
|
28
|
+
=begin
|
29
|
+
# These two settings work together to allow you to limit a spec run
|
30
|
+
# to individual examples or groups you care about by tagging them with
|
31
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
32
|
+
# get run.
|
33
|
+
config.filter_run :focus
|
34
|
+
config.run_all_when_everything_filtered = true
|
35
|
+
|
36
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
37
|
+
# file, and it's useful to allow more verbose output when running an
|
38
|
+
# individual spec file.
|
39
|
+
if config.files_to_run.one?
|
40
|
+
# Use the documentation formatter for detailed output,
|
41
|
+
# unless a formatter has already been configured
|
42
|
+
# (e.g. via a command-line flag).
|
43
|
+
config.default_formatter = 'doc'
|
44
|
+
end
|
45
|
+
|
46
|
+
# Print the 10 slowest examples and example groups at the
|
47
|
+
# end of the spec run, to help surface which specs are running
|
48
|
+
# particularly slow.
|
49
|
+
config.profile_examples = 10
|
50
|
+
|
51
|
+
# Run specs in random order to surface order dependencies. If you find an
|
52
|
+
# order dependency and want to debug it, you can fix the order by providing
|
53
|
+
# the seed, which is printed after each run.
|
54
|
+
# --seed 1234
|
55
|
+
config.order = :random
|
56
|
+
|
57
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
58
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
59
|
+
# test failures related to randomization by passing the same `--seed` value
|
60
|
+
# as the one that triggered the failure.
|
61
|
+
Kernel.srand config.seed
|
62
|
+
|
63
|
+
# rspec-expectations config goes here. You can use an alternate
|
64
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
65
|
+
# assertions if you prefer.
|
66
|
+
config.expect_with :rspec do |expectations|
|
67
|
+
# Enable only the newer, non-monkey-patching expect syntax.
|
68
|
+
# For more details, see:
|
69
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
70
|
+
expectations.syntax = :expect
|
71
|
+
end
|
72
|
+
|
73
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
74
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
75
|
+
config.mock_with :rspec do |mocks|
|
76
|
+
# Enable only the newer, non-monkey-patching expect syntax.
|
77
|
+
# For more details, see:
|
78
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
79
|
+
mocks.syntax = :expect
|
80
|
+
|
81
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
82
|
+
# a real object. This is generally recommended.
|
83
|
+
mocks.verify_partial_doubles = true
|
84
|
+
end
|
85
|
+
=end
|
86
|
+
end
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rubyserial-with-blocking
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Adrian Zankich
|
8
|
+
- Theron Boerner
|
9
|
+
- Javier Cervantes
|
10
|
+
- Dirk Lüsebrink
|
11
|
+
autorequire:
|
12
|
+
bindir: bin
|
13
|
+
cert_chain: []
|
14
|
+
date: 2015-04-23 00:00:00.000000000 Z
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: ffi
|
18
|
+
requirement: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - "~>"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.9.3
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 1.9.3
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: guard-rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
type: :development
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
description: FFI Ruby library for RS-232 serial port communication
|
45
|
+
email:
|
46
|
+
executables: []
|
47
|
+
extensions: []
|
48
|
+
extra_rdoc_files: []
|
49
|
+
files:
|
50
|
+
- ".gitignore"
|
51
|
+
- ".rspec"
|
52
|
+
- ".travis.yml"
|
53
|
+
- Gemfile
|
54
|
+
- Guardfile
|
55
|
+
- LICENSE
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- appveyor.yml
|
59
|
+
- lib/rubyserial.rb
|
60
|
+
- lib/rubyserial/linux_constants.rb
|
61
|
+
- lib/rubyserial/osx_constants.rb
|
62
|
+
- lib/rubyserial/posix.rb
|
63
|
+
- lib/rubyserial/version.rb
|
64
|
+
- lib/rubyserial/windows.rb
|
65
|
+
- lib/rubyserial/windows_constants.rb
|
66
|
+
- rubyserial.gemspec
|
67
|
+
- spec/lib/posix_spec.rb
|
68
|
+
- spec/rubyserial_spec.rb
|
69
|
+
- spec/spec_helper.rb
|
70
|
+
homepage: https://github.com/hybridgroup/rubyserial
|
71
|
+
licenses:
|
72
|
+
- Apache 2.0
|
73
|
+
metadata: {}
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options: []
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
requirements: []
|
89
|
+
rubyforge_project:
|
90
|
+
rubygems_version: 2.4.5
|
91
|
+
signing_key:
|
92
|
+
specification_version: 4
|
93
|
+
summary: FFI Ruby library for RS-232 serial port communication
|
94
|
+
test_files: []
|
95
|
+
has_rdoc:
|