rubyserial 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 248fe3f8e44d655481dcc7f26c7367507e4741d5
4
- data.tar.gz: df82381fdd9677680094bf844efcae566c327ffe
3
+ metadata.gz: d95d4b0f3e735071995dbac944d07a17061880ee
4
+ data.tar.gz: 833f60ee4c09de550c3695e25b6e12c33f9cbf43
5
5
  SHA512:
6
- metadata.gz: c78bec3db3c730e71b269d7e1123ad54e895049e9d4cfdee3e2db9b6028dc73f6f0dadfae6f072d6aff0b006c7b8d00eac9d9345a4e7d6912a7270d7f2f3ca2a
7
- data.tar.gz: 811f6208954f7e4f2c0ddd46f53af5388d5288b1f2a5a4bf0fa71085fac127de0f80439c5e43975c22055bdef4154eb19bd41895bbf89562df08c024a2f32fba
6
+ metadata.gz: 7a21a0889ec09eefa24810723930ce669e5a5028b3f88c559fd2ef3a8f24b25701cb73e7a7f13c7bf47e969ada36d1aaec861f7ce93f498e58ce23e4c912a9d2
7
+ data.tar.gz: 622b237d44d95a52528e0f68ac52d06257d8821ae99176419c538e9c869f3d4967d2e6bf7d798afd48f71065f8cad7dd20f8c6d8df23ed25beb6718551b1d296
@@ -7,8 +7,12 @@ rvm:
7
7
  - jruby
8
8
  - jruby-head
9
9
  - rbx-2.2.10
10
+ matrix:
11
+ allow_failures:
12
+ - rvm: jruby-head
13
+ - rvm: ruby-head
10
14
  install:
11
15
  - sudo apt-get update && sudo apt-get install --force-yes socat
12
16
  script:
13
17
  - bundle install
14
- - bundle exec rspec
18
+ - bundle exec rspec -fd
data/README.md CHANGED
@@ -1,6 +1,10 @@
1
1
  # rubyserial
2
2
 
3
- RubySerial is a simple RubyGem for reading from and writing to serial ports.
3
+ RubySerial is a simple Ruby gem for reading from and writing to serial ports.
4
+
5
+ Unlike other Ruby serial port implementations, it supports all of the most popular Ruby implementations (MRI, JRuby, & Rubinius) on the most popular operating systems (OSX, Linux, & Windows). And it does not require any native compilation thanks to using RubyFFI [https://github.com/ffi/ffi](https://github.com/ffi/ffi).
6
+
7
+ The interface to RubySerial should be (mostly) compatible with other Ruby serialport gems, so you should be able to drop in the new gem, change the `require` and use it as a replacement. If not, please let us know so we can address any issues.
4
8
 
5
9
  [![Build Status](https://travis-ci.org/hybridgroup/rubyserial.svg)](https://travis-ci.org/hybridgroup/rubyserial)
6
10
 
@@ -43,16 +43,20 @@ class Serial
43
43
  end
44
44
 
45
45
  def write(data)
46
+ data = data.to_s
46
47
  n = 0
47
48
  while data.size > n do
48
49
  buff = FFI::MemoryPointer.from_string(data[n..-1].to_s)
49
- i = RubySerial::Posix.write(@fd, buff, buff.size)
50
+ i = RubySerial::Posix.write(@fd, buff, buff.size-1)
50
51
  if i == -1
51
52
  raise RubySerial::Exception, RubySerial::Posix::ERROR_CODES[FFI.errno]
52
53
  else
53
54
  n = n+i
54
55
  end
55
56
  end
57
+
58
+ # return number of bytes written
59
+ n
56
60
  end
57
61
 
58
62
  def read(size)
@@ -78,6 +82,20 @@ class Serial
78
82
  end
79
83
  end
80
84
 
85
+ def gets(sep=$/, limit=nil)
86
+ sep = "\n\n" if sep == ''
87
+ # This allows the method signature to be (sep) or (limit)
88
+ (limit = sep; sep="\n") if sep.is_a? Integer
89
+ bytes = []
90
+ loop do
91
+ current_byte = getbyte
92
+ bytes << current_byte unless current_byte.nil?
93
+ break if (bytes.last(sep.bytes.to_a.size) == sep.bytes.to_a) || ((bytes.size == limit) if limit)
94
+ end
95
+
96
+ bytes.map { |e| e.chr }.join
97
+ end
98
+
81
99
  private
82
100
 
83
101
  def build_config(baude_rate, data_bits)
@@ -95,4 +113,4 @@ class Serial
95
113
 
96
114
  config
97
115
  end
98
- end
116
+ end
@@ -1,5 +1,5 @@
1
1
  module RubySerial
2
2
  unless const_defined?('VERSION')
3
- VERSION = "0.1.2"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -68,10 +68,25 @@ class Serial
68
68
  def write(data)
69
69
  buff = FFI::MemoryPointer.from_string(data.to_s)
70
70
  count = FFI::MemoryPointer.new :uint32, 1
71
- err = RubySerial::Win32.WriteFile(@fd, buff, buff.size, count, nil)
71
+ err = RubySerial::Win32.WriteFile(@fd, buff, buff.size-1, count, nil)
72
72
  if err == 0
73
73
  raise RubySerial::Exception, RubySerial::Win32::ERROR_CODES[FFI.errno]
74
74
  end
75
+ count.read_string.unpack('H4').join().to_i(16)
76
+ end
77
+
78
+ def gets(sep=$/, limit=nil)
79
+ sep = "\n\n" if sep == ''
80
+ # This allows the method signature to be (sep) or (limit)
81
+ (limit = sep; sep="\n") if sep.is_a? Integer
82
+ bytes = []
83
+ loop do
84
+ current_byte = getbyte
85
+ bytes << current_byte unless current_byte.nil?
86
+ break if (bytes.last(sep.bytes.to_a.size) == sep.bytes) || ((bytes.size == limit) if limit)
87
+ end
88
+
89
+ bytes.map { |e| e.chr }.join
75
90
  end
76
91
 
77
92
  def close
@@ -0,0 +1,131 @@
1
+ require 'rubyserial'
2
+
3
+ describe "rubyserial" do
4
+ before do
5
+ @ports = []
6
+ require 'rbconfig'
7
+ if RbConfig::CONFIG['host_os'] =~ /mswin|windows|mingw/i
8
+ @command_output = `powershell "Get-WmiObject Win32_SerialPort | Select-Object DeviceID, Description"`
9
+ @prefix = "\\\\.\\"
10
+ @coms = @command_output.scan(/COM\d*/).uniq
11
+ @file_array = @command_output.split("\n")
12
+ @file_array.each do |line|
13
+ if line.include?('com0com')
14
+ @coms.each do |e|
15
+ if line =~ /#{e}/
16
+ @ports << "#{@prefix}#{e}"
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ if @ports[0].nil?
23
+ puts "Heya mister (or miss(es)). For this to work you need com0com "\
24
+ "installed. It doesn't look like you have it so rerun this after you"\
25
+ "install it from here: https://code.google.com/p/powersdr-iq/download"\
26
+ "s/detail?name=setup_com0com_W7_x64_signed.exe&can=2&q="
27
+ end
28
+ else
29
+ File.delete('socat.log') if File.file?('socat.log')
30
+
31
+ Thread.new do
32
+ system('socat -lf socat.log -d -d pty,raw,echo=0 pty,raw,echo=0')
33
+ end
34
+
35
+ @ptys = nil
36
+
37
+ loop do
38
+ if File.file? 'socat.log'
39
+ @file = File.open('socat.log', "r")
40
+ @fileread = @file.read
41
+
42
+ unless @fileread.count("\n") < 3
43
+ @ptys = @fileread.scan(/PTY is (.*)/)
44
+ break
45
+ end
46
+ end
47
+ end
48
+
49
+ @ports = [@ptys[1][0], @ptys[0][0]]
50
+ end
51
+
52
+ @sp2 = Serial.new(@ports[0])
53
+ @sp = Serial.new(@ports[1])
54
+ end
55
+
56
+ after do
57
+ @sp2.close
58
+ @sp.close
59
+ end
60
+
61
+ it "should read and write" do
62
+ @sp2.write('hello')
63
+ # small delay so it can write to the other port.
64
+ sleep 0.1
65
+ check = @sp.read(5)
66
+ expect(check).to eql('hello')
67
+ end
68
+
69
+ it "should convert ints to strings" do
70
+ expect(@sp2.write(123)).to eql(3)
71
+ sleep 0.1
72
+ expect(@sp.read(3)).to eql('123')
73
+ end
74
+
75
+ it "write should return bytes written" do
76
+ expect(@sp2.write('hello')).to eql(5)
77
+ end
78
+
79
+ it "reading nothing should be blank" do
80
+ expect(@sp.read(5)).to eql('')
81
+ end
82
+
83
+ it "should give me nil on getbyte" do
84
+ expect(@sp.getbyte).to be_nil
85
+ end
86
+
87
+ it "should give me bytes" do
88
+ @sp2.write('hello')
89
+ # small delay so it can write to the other port.
90
+ sleep 0.1
91
+ check = @sp.getbyte
92
+ expect([check].pack('C')).to eql('h')
93
+ end
94
+
95
+
96
+ describe "giving me lines" do
97
+ it "should give me a line" do
98
+ @sp.write("no yes \n hello")
99
+ sleep 0.1
100
+ expect(@sp2.gets).to eql("no yes \n")
101
+ end
102
+
103
+ it "should accept a sep param" do
104
+ @sp.write('no yes END bleh')
105
+ sleep 0.1
106
+ expect(@sp2.gets('END')).to eql("no yes END")
107
+ end
108
+
109
+ it "should accept a limit param" do
110
+ @sp.write("no yes \n hello")
111
+ sleep 0.1
112
+ expect(@sp2.gets(4)).to eql("no y")
113
+ end
114
+
115
+ it "should accept limit and sep params" do
116
+ @sp.write("no yes END hello")
117
+ sleep 0.1
118
+ expect(@sp2.gets('END', 20)).to eql("no yes END")
119
+ @sp2.read(1000)
120
+ @sp.write("no yes END hello")
121
+ sleep 0.1
122
+ expect(@sp2.gets('END', 4)).to eql('no y')
123
+ end
124
+
125
+ it "should read a paragraph at a time" do
126
+ @sp.write("Something \n Something else \n\n and other stuff")
127
+ sleep 0.1
128
+ expect(@sp2.gets('')).to eql("Something \n Something else \n\n")
129
+ end
130
+ end
131
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubyserial
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adrian Zankich
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-07-07 00:00:00.000000000 Z
13
+ date: 2014-11-05 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: ffi
@@ -47,7 +47,7 @@ files:
47
47
  - lib/rubyserial/windows.rb
48
48
  - lib/rubyserial/windows_constants.rb
49
49
  - rubyserial.gemspec
50
- - spec/basic_rw_spec.rb
50
+ - spec/rubyserial_spec.rb
51
51
  - spec/spec_helper.rb
52
52
  homepage: https://github.com/hybridgroup/rubyserial
53
53
  licenses:
@@ -1,35 +0,0 @@
1
- describe "basic reading and writing" do
2
- before do
3
- File.delete('socat.log') if File.file?('socat.log')
4
-
5
- Thread.new do
6
- system('socat -lf socat.log -d -d pty,raw,echo=0 pty,raw,echo=0')
7
- end
8
-
9
- @ptys = nil
10
-
11
- loop do
12
- if File.file? 'socat.log'
13
- @file = File.open('socat.log', "r")
14
- @fileread = @file.read
15
- unless @fileread.count("\n") < 3
16
- @ptys = @fileread.scan(/PTY is (.*)/)
17
- break
18
- end
19
- end
20
- end
21
-
22
- require 'rubyserial'
23
- @sp2 = Serial.new(@ptys[1][0])
24
- @sp = Serial.new(@ptys[0][0])
25
- @sp2.write('hello')
26
-
27
- # small delay so it can write to the other port.
28
- sleep 0.1
29
- end
30
-
31
- it "should read and write" do
32
- check = @sp.read(5)
33
- expect(check).to eql('hello')
34
- end
35
- end