Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge pull request #5 from rlishtaba/release/v2.3.1 #6

Open
wants to merge 13 commits into
base: release/v2.3.1
Choose a base branch
from
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ lib/*.jar
.cproject
atlassian-ide-plugin.xml
rspec.xml
ruby-rs-232.xcodeproj
script/ruby
/vendor
ext/rs_232/Makefile
4 changes: 2 additions & 2 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2013, Ingenico Inc.
Copyright (c) 2013, Roman Lishtaba.

MIT License

Expand All @@ -16,7 +16,7 @@ included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
NONINFINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
115 changes: 18 additions & 97 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,105 +20,26 @@ Or install it yourself as:

## Usage

See `examples` folder for details.
You may use this gem directly as a low-level transport layer in your communication module. As an alternative you may check another gem called (ruby-digital-transport)[https://github.com/rlishtaba/ruby-digital-transport] which is kind of transport abstraction porviding unified interface to SerialPort (using this gem), TCP and USB-HID.

```ruby
class SerialPortAdapter
include CommPort

attr_reader :interface
private :interface

# constructor with default params
# by default port will be configured with:
#
# baud_rate = 115200 # BAUD_115200
# data_bits = 8 # DATA_BITS_8
# parity = 0 # PAR_NONE
# stop_bits = 1 # STOP_BITS_1
# flow_control = 0 # FLOW_OFF
#
# see other public constants on CommPort namespace
#
def initialize(port, options = {})
@port = port
@options = options
@open = false
@interface = Rs232.new(port)
connect
end

def connect
return if open?
can_configure_timeout = interface.respond_to?(:connecting_timeout)
interface.connecting_timeout = @options.fetch(:connecting_timeout, 60) if can_configure_timeout
interface.open
configure_interface!
@open = open?
end

def write(bytes)
interface.write(bytes)
end

def close
return unless open?
flush
interface.close
@open = open?
!open?
end

def flush
interface.flush
end

def open?
interface && !interface.closed?
end

def recv(count)
array = []
chunk = read_io_until(count, count)
array.push chunk if chunk
array.empty? ? nil : array.join
end

def recv_nonblock(count)
array = []
chunks_count = (count == -1) ? interface.available? : count
chunks_count.times do
chunk = interface.read(1)
array.push chunk if chunk
end
array.empty? ? nil : array.join
end

def read(count, blocking = false)
blocking ? recv(count) : recv_nonblock(count)
end

private

def configure_interface!
interface.baud_rate = @options.fetch(:baud_rate, BAUD_115200).to_i
interface.data_bits = @options.fetch(:data_bits, DATA_BITS_8).to_i
interface.parity = @options.fetch(:parity, PAR_NONE).to_i
interface.stop_bits = @options.fetch(:stop_bits, STOP_BITS_1).to_i
interface.flow_control = @options.fetch(:flow_control, FLOW_OFF).to_i
end

def block_io_until(count, up_to)
up_to -= 1 while interface.available? < count && up_to > 0
up_to > 0
end

def read_io_until(count, up_to)
sleep 0.001 until block_io_until(count, up_to)
read(count)
end
end

> include Rs232
> port = new_serial_port('/dev/tty.ACM0', baud_rate: 9600)
#=> #<Rs232::Impl @port='/dev/tty.ACM0'>
> port.open?
#=> false
> port.connect # rasing IOError in case of port couldn't be opened.
#=> #<Rs232::Impl @port='/dev/tty.ACM0'>
> port.pending_bytes
#=> 15
> port.read(15)
#=> 'Hello, World!!!'
> port.write("Hi")
#=> 2
> port.close
#=> true
> port.open?
#=> false
```

## Contributing
Expand Down
36 changes: 29 additions & 7 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
lib = File.expand_path '../lib', __FILE__
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include? lib

require 'bundler/setup'
require 'bundler/gem_tasks'
require 'rake'

load 'tasks/clean.rake'
load 'tasks/cov.rake'
load 'tasks/rspec.rake'
load 'tasks/cucumber.rake'
load 'tasks/compile.rake'
require 'rake/extensiontask'
Rake::ExtensionTask.new('rs_232') do |ext|
ext.name = 'rs_232_native'
ext.source_pattern = '*.{c,cpp}'
end

require 'rake/clean'
CLEAN.include %w(**/*.{log} doc coverage tmp pkg **/*.{o,so,bundle} Makefile)

require 'simplecov'
task :cov do
ENV['SIMPLECOV'] = 'features'
Rake::Task['default'].invoke
end

require 'cucumber/rake/task'
Cucumber::Rake::Task.new(:features) do |t|
t.fork = true
t.profile = :default
end

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new do |t|
t.fail_on_error = false
t.verbose = true
t.rspec_opts = '--format RspecJunitFormatter --out rspec.xml --tag ~wip'
end

task default: [:clobber, :compile, :spec, :features]
task default: [:clobber, :compile, :spec]
3 changes: 0 additions & 3 deletions examples/README.md

This file was deleted.

97 changes: 0 additions & 97 deletions examples/serial_port_adapter.rb

This file was deleted.

50 changes: 50 additions & 0 deletions ext/rs_232/Constants.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2013, Roman Lishtaba.
*
* Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted,
* provided that the above copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*
**/

/*
* @author Roman Lishtaba
*/

#include "Constants.h"

void Constants_Init(VALUE namespace)
{
rb_define_const(namespace, "BAUD_110", INT2FIX(BAUD110));
rb_define_const(namespace, "BAUD_300", INT2FIX(BAUD300));
rb_define_const(namespace, "BAUD_600", INT2FIX(BAUD600));
rb_define_const(namespace, "BAUD_1200", INT2FIX(BAUD1200));
rb_define_const(namespace, "BAUD_2400", INT2FIX(BAUD2400));
rb_define_const(namespace, "BAUD_4800", INT2FIX(BAUD4800));
rb_define_const(namespace, "BAUD_9600", INT2FIX(BAUD9600));
rb_define_const(namespace, "BAUD_19200", INT2FIX(BAUD19200));
rb_define_const(namespace, "BAUD_38400", INT2FIX(BAUD38400));
rb_define_const(namespace, "BAUD_57600", INT2FIX(BAUD57600));
rb_define_const(namespace, "BAUD_115200", INT2FIX(BAUD115200));

rb_define_const(namespace, "DATA_BITS_5", INT2FIX(DATA_5));
rb_define_const(namespace, "DATA_BITS_6", INT2FIX(DATA_6));
rb_define_const(namespace, "DATA_BITS_7", INT2FIX(DATA_7));
rb_define_const(namespace, "DATA_BITS_8", INT2FIX(DATA_8));

rb_define_const(namespace, "PAR_NONE", INT2FIX(PAR_NONE));
rb_define_const(namespace, "PAR_ODD", INT2FIX(PAR_ODD));
rb_define_const(namespace, "PAR_EVEN", INT2FIX(PAR_EVEN));

rb_define_const(namespace, "STOP_BITS_1", INT2FIX(STOP_1));
rb_define_const(namespace, "STOP_BITS_3", INT2FIX(STOP_2));

rb_define_const(namespace, "FLOW_OFF", INT2FIX(FLOW_OFF));
rb_define_const(namespace, "FLOW_HARDWARE", INT2FIX(FLOW_HARDWARE));
rb_define_const(namespace, "FLOW_XONXOFF", INT2FIX(FLOW_XONXOFF));
}
Loading