lib_serial 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +23 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +34 -0
- data/Rakefile +2 -0
- data/lib/lib_serial/driver.rb +26 -0
- data/lib/lib_serial/drivers/linux.rb +64 -0
- data/lib/lib_serial/drivers/mac_os.rb +64 -0
- data/lib/lib_serial/serial_port.rb +26 -0
- data/lib/lib_serial/version.rb +3 -0
- data/lib/lib_serial.rb +21 -0
- data/lib_serial.gemspec +20 -0
- metadata +56 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1adcd9b62946210edc66a311e87100786bd33af6
|
4
|
+
data.tar.gz: 6296ceba5addfc2d8a56aa0557e0e9e3e6938e0b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 36d4dd743db945b5d3168d9bfe577cca3fdaba702e9e9e0149f7c056f4c46e4cc90041182b7e6105db8c896c804cfe1c55c6526fd08370603b0bbbb7a5e8b441
|
7
|
+
data.tar.gz: 499d742b9b0f83878ad36186da1656b8cd4f11c8dc4e11675840d65496d75c80027a795521634d1d851cac695f90853f5298cdb70281880c15c4c430d4d87a5b
|
data/.gitignore
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
23
|
+
.idea/*
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Dustin Ward
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# LibSerial
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'lib_serial'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install lib_serial
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it ( https://github.com/[my-github-username]/lib_serial/fork )
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create a new Pull Request
|
30
|
+
=======
|
31
|
+
lib_serial
|
32
|
+
==========
|
33
|
+
|
34
|
+
Serial Port library for Linux/MacOS
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'lib_serial/drivers/linux'
|
2
|
+
require 'lib_serial/drivers/mac_os'
|
3
|
+
|
4
|
+
module LibSerial
|
5
|
+
module Drivers
|
6
|
+
def baud_rates
|
7
|
+
[110,150,300,600,1200,2400,4800,9600,19200,38400,57600,115200]
|
8
|
+
end
|
9
|
+
|
10
|
+
def parity
|
11
|
+
{
|
12
|
+
none: '-parenb',
|
13
|
+
odd: 'parenb parodd',
|
14
|
+
even: 'parenb -parodd'
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
def flow_control
|
19
|
+
{
|
20
|
+
none: 'clocal -crtscts -ixon -ixoff',
|
21
|
+
rts_cts: '-clocal crtscts -ixon -ixoff',
|
22
|
+
xon_xoff: '-clocal -crtscts ixon ixoff'
|
23
|
+
}
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module LibSerial
|
2
|
+
module Drivers
|
3
|
+
class Linux
|
4
|
+
attr_accessor :file_descriptor, :device
|
5
|
+
|
6
|
+
def initialize(device)
|
7
|
+
self.device = device
|
8
|
+
self.open
|
9
|
+
self.execute("stty -F #{self.device} raw")
|
10
|
+
self.set_baud_rate
|
11
|
+
self.set_parity
|
12
|
+
self.set_char_length
|
13
|
+
self.set_stop_bits
|
14
|
+
self.set_flow_control
|
15
|
+
end
|
16
|
+
|
17
|
+
def open
|
18
|
+
self.file_descriptor = File.open(self.device, 'r+')
|
19
|
+
end
|
20
|
+
|
21
|
+
def read
|
22
|
+
buffer = ''
|
23
|
+
buffer << self.file_descriptor.readchar
|
24
|
+
buffer
|
25
|
+
end
|
26
|
+
|
27
|
+
def write(data)
|
28
|
+
ret = self.file_descriptor.write(data)
|
29
|
+
self.file_descriptor.flush
|
30
|
+
ret
|
31
|
+
end
|
32
|
+
|
33
|
+
def close
|
34
|
+
end
|
35
|
+
|
36
|
+
def set_baud_rate(rate = 38400)
|
37
|
+
## TODO: Check for valid baud rate
|
38
|
+
self.execute("stty -F #{self.device} #{rate.to_i}")
|
39
|
+
end
|
40
|
+
|
41
|
+
def set_parity(parity = :none)
|
42
|
+
## TODO: Check for valid parity here
|
43
|
+
self.execute("stty -F #{self.device} #{LibSerial::Drivers.parity[parity]}")
|
44
|
+
end
|
45
|
+
|
46
|
+
def set_char_length(length = 8)
|
47
|
+
self.execute("stty -F #{self.device} cs#{length.to_i}")
|
48
|
+
end
|
49
|
+
|
50
|
+
def set_stop_bits(length = 1)
|
51
|
+
self.execute("stty -F #{self.device} #{(length == 1) ? '-' : ''}cstopb")
|
52
|
+
end
|
53
|
+
|
54
|
+
def set_flow_control(flow = :none)
|
55
|
+
self.execute("stty -F #{self.device} #{LibSerial::Drivers.flow_control[flow]}")
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
def execute(cmd)
|
60
|
+
system(cmd)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module LibSerial
|
2
|
+
module Drivers
|
3
|
+
class MacOS
|
4
|
+
attr_accessor :file_descriptor, :device
|
5
|
+
|
6
|
+
def initialize(device)
|
7
|
+
self.device = device
|
8
|
+
self.open
|
9
|
+
self.execute("stty -f #{self.device} raw")
|
10
|
+
self.set_baud_rate
|
11
|
+
self.set_parity
|
12
|
+
self.set_char_length
|
13
|
+
self.set_stop_bits
|
14
|
+
self.set_flow_control
|
15
|
+
end
|
16
|
+
|
17
|
+
def open
|
18
|
+
self.file_descriptor = File.open(self.device, 'r+')
|
19
|
+
end
|
20
|
+
|
21
|
+
def read
|
22
|
+
buffer = ''
|
23
|
+
buffer << self.file_descriptor.readchar
|
24
|
+
buffer
|
25
|
+
end
|
26
|
+
|
27
|
+
def write(data)
|
28
|
+
ret = self.file_descriptor.write(data)
|
29
|
+
self.file_descriptor.flush
|
30
|
+
ret
|
31
|
+
end
|
32
|
+
|
33
|
+
def close
|
34
|
+
end
|
35
|
+
|
36
|
+
def set_baud_rate(rate = 38400)
|
37
|
+
## TODO: Check for valid baud rate
|
38
|
+
self.execute("stty -f #{self.device} #{rate.to_i}")
|
39
|
+
end
|
40
|
+
|
41
|
+
def set_parity(parity = :none)
|
42
|
+
## TODO: Check for valid parity here
|
43
|
+
self.execute("stty -f #{self.device} #{LibSerial::Drivers.parity[parity]}")
|
44
|
+
end
|
45
|
+
|
46
|
+
def set_char_length(length = 8)
|
47
|
+
self.execute("stty -f #{self.device} cs#{length.to_i}")
|
48
|
+
end
|
49
|
+
|
50
|
+
def set_stop_bits(length = 1)
|
51
|
+
self.execute("stty -f #{self.device} #{(length == 1) ? '-' : ''}cstopb")
|
52
|
+
end
|
53
|
+
|
54
|
+
def set_flow_control(flow = :none)
|
55
|
+
self.execute("stty -f #{self.device} #{LibSerial::Drivers.flow_control[flow]}")
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
def execute(cmd)
|
60
|
+
system(cmd)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module LibSerial
|
2
|
+
class SerialPort
|
3
|
+
attr_accessor :driver
|
4
|
+
|
5
|
+
def initialize(device = '/dev/ttyAMA0', options = {})
|
6
|
+
if LibSerial.mac?
|
7
|
+
self.driver = LibSerial::Drivers::MacOS.new(device)
|
8
|
+
elsif LibSerial.linux? || LibSerial.unix?
|
9
|
+
self.driver = LibSerial::Drivers::Linux.new(device)
|
10
|
+
else
|
11
|
+
raise 'Invalid Platform'
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def read
|
16
|
+
loop do
|
17
|
+
data = self.driver.read
|
18
|
+
yield data
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def write(data)
|
23
|
+
self.driver.write(data)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/lib_serial.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'lib_serial/version'
|
2
|
+
require 'lib_serial/driver'
|
3
|
+
require 'lib_serial/serial_port'
|
4
|
+
|
5
|
+
module LibSerial
|
6
|
+
def windows?
|
7
|
+
(/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
|
8
|
+
end
|
9
|
+
|
10
|
+
def mac?
|
11
|
+
(/darwin/ =~ RUBY_PLATFORM) != nil
|
12
|
+
end
|
13
|
+
|
14
|
+
def unix?
|
15
|
+
!LibSerial.windows?
|
16
|
+
end
|
17
|
+
|
18
|
+
def linux?
|
19
|
+
LibSerial.unix? and not LibSerial.mac?
|
20
|
+
end
|
21
|
+
end
|
data/lib_serial.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'lib_serial/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'lib_serial'
|
8
|
+
spec.version = LibSerial::VERSION
|
9
|
+
spec.authors = ['Dustin Ward']
|
10
|
+
spec.email = ['dustin.n.ward@gmail.com']
|
11
|
+
spec.summary = 'Linux/MacOS serial port gem'
|
12
|
+
spec.description = 'Linux/MacOS serial port gem without native c code'
|
13
|
+
spec.homepage = 'http://www.boxcar21.com'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
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
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lib_serial
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dustin Ward
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-14 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Linux/MacOS serial port gem without native c code
|
14
|
+
email:
|
15
|
+
- dustin.n.ward@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".gitignore"
|
21
|
+
- Gemfile
|
22
|
+
- LICENSE.txt
|
23
|
+
- README.md
|
24
|
+
- Rakefile
|
25
|
+
- lib/lib_serial.rb
|
26
|
+
- lib/lib_serial/driver.rb
|
27
|
+
- lib/lib_serial/drivers/linux.rb
|
28
|
+
- lib/lib_serial/drivers/mac_os.rb
|
29
|
+
- lib/lib_serial/serial_port.rb
|
30
|
+
- lib/lib_serial/version.rb
|
31
|
+
- lib_serial.gemspec
|
32
|
+
homepage: http://www.boxcar21.com
|
33
|
+
licenses:
|
34
|
+
- MIT
|
35
|
+
metadata: {}
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
requirements: []
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 2.2.2
|
53
|
+
signing_key:
|
54
|
+
specification_version: 4
|
55
|
+
summary: Linux/MacOS serial port gem
|
56
|
+
test_files: []
|