uart 1.0.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.
Files changed (7) hide show
  1. checksums.yaml +7 -0
  2. data/Manifest.txt +5 -0
  3. data/README.md +65 -0
  4. data/Rakefile +17 -0
  5. data/lib/uart.rb +121 -0
  6. data/test/test_uart.rb +10 -0
  7. metadata +114 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 420b84bb7b98fbb48d2903cb131a786de7abf2fa59701508caa2af31eb2bc84a
4
+ data.tar.gz: 0dc729e9d484f02989030f1439443482aae3ef20cfa3c75837cc1d5bf704d1a6
5
+ SHA512:
6
+ metadata.gz: 981faa3038d236252babca6e52b0c99c2aebcc1b9b19e5bae521f8498688b3e031eda492b34be2df3747469b328cccf1f90ff5340a82ee5fdc1ba7038adde4a3
7
+ data.tar.gz: c34504e3892300e0a8489ccd9ca83e9aa4c8a9e2b656f9c614560f6e07be3e4ad715f956009cb8e4c69f56d96c769bffbf7006284125b0a9b51522810fa98010
@@ -0,0 +1,5 @@
1
+ Manifest.txt
2
+ README.md
3
+ Rakefile
4
+ lib/uart.rb
5
+ test/test_uart.rb
@@ -0,0 +1,65 @@
1
+ # UART
2
+
3
+ * https://github.com/tenderlove/uart
4
+
5
+ ## DESCRIPTION:
6
+
7
+ UART is a simple wrapper around the ruby-termios gem that gives you an easy
8
+ interface for setting up a UART or serial connection. This gem is written in
9
+ pure Ruby. This gem relies on ruby-termios to provide bindings to the termios
10
+ system call, but uses those bindings to set up a serial connection in pure Ruby.
11
+
12
+ ## FEATURES/PROBLEMS:
13
+
14
+ * No C code
15
+ * No FFI code
16
+ * Seems to work
17
+
18
+ ## SYNOPSIS:
19
+
20
+ Here is an example of writing to an LCD screen over UART. The speed is 9600,
21
+ 8 data bits, no parity, and one stop bit:
22
+
23
+ ```ruby
24
+ require 'uart'
25
+
26
+ UART.open '/dev/tty.usbserial-00000000' do |serial|
27
+ str = 'Hello World!'
28
+ serial.write [0x8A, 0xA8, 0x00, 0x00, str.bytesize].pack 'C5'
29
+ serial.write str.b
30
+ p serial.read(4).unpack('C4').map { |x| x.to_s(16) }
31
+ end
32
+ ```
33
+
34
+ ## REQUIREMENTS:
35
+
36
+ * ruby-termios
37
+
38
+ ## INSTALL:
39
+
40
+ * `gem install uart`
41
+
42
+ ## LICENSE:
43
+
44
+ (The MIT License)
45
+
46
+ Copyright (c) 2018 Aaron Patterson
47
+
48
+ Permission is hereby granted, free of charge, to any person obtaining
49
+ a copy of this software and associated documentation files (the
50
+ 'Software'), to deal in the Software without restriction, including
51
+ without limitation the rights to use, copy, modify, merge, publish,
52
+ distribute, sublicense, and/or sell copies of the Software, and to
53
+ permit persons to whom the Software is furnished to do so, subject to
54
+ the following conditions:
55
+
56
+ The above copyright notice and this permission notice shall be
57
+ included in all copies or substantial portions of the Software.
58
+
59
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
60
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
61
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
62
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
63
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
64
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
65
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,17 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+
6
+ Hoe.plugin :minitest
7
+ Hoe.plugin :gemspec # `gem install hoe-gemspec`
8
+ Hoe.plugin :git # `gem install hoe-git`
9
+
10
+ Hoe.spec 'uart' do
11
+ developer('Aaron Patterson', 'tenderlove@ruby-lang.org')
12
+ self.readme_file = 'README.md'
13
+ self.extra_rdoc_files = FileList['*.md']
14
+ self.extra_deps << ['ruby-termios']
15
+ end
16
+
17
+ # vim: syntax=ruby
@@ -0,0 +1,121 @@
1
+ require 'termios'
2
+ require 'fcntl'
3
+
4
+ module UART
5
+ VERSION = '1.0.0'
6
+
7
+ ##
8
+ # Open a UART as a file.
9
+ #
10
+ # For example, we have a serial connection on `/dev/tty.usbserial-00000000'.
11
+ # It's speed is 9600, 8 data bits, 1 stop bit, and no parity:
12
+ #
13
+ # serial = UART.open '/dev/tty.usbserial-00000000', 9600, '8N1'
14
+ # serial.write 'whatever'
15
+ #
16
+ # A speed of 9600, 8 data bits, no parity, and 1 stop bit is default, so
17
+ # we can reduce the code to this:
18
+ #
19
+ # serial = UART.open '/dev/tty.usbserial-00000000'
20
+ # serial.write 'whatever'
21
+ #
22
+ # Finally, we can give a block and the file will be automatically closed for
23
+ # us like this:
24
+ #
25
+ # UART.open '/dev/tty.usbserial-00000000' do |serial|
26
+ # serial.write 'whatever'
27
+ # end
28
+ def open filename, speed = 9600, mode = '8N1'
29
+ f = File.open filename, File::RDWR|Fcntl::O_NOCTTY|Fcntl::O_NDELAY
30
+ f.binmode
31
+ f.sync = true
32
+
33
+ # enable blocking reads, otherwise read timeout won't work
34
+ f.fcntl Fcntl::F_SETFL, f.fcntl(Fcntl::F_GETFL, 0) & ~Fcntl::O_NONBLOCK
35
+
36
+ t = Termios.tcgetattr f
37
+ t.iflag = 0
38
+ t.oflag = 0
39
+ t.lflag = 0
40
+
41
+ if mode =~ /^(\d)(\w)(\d)$/
42
+ t = data_bits t, $1.to_i
43
+ t = stop_bits t, $3.to_i
44
+ t = parity t, { 'N' => :none, 'E' => :even, 'O' => :odd }[$2]
45
+ t = speed t, speed
46
+ t = read_timeout t, 5
47
+ t = reading t
48
+ else
49
+ raise ArgumentError, "Invalid format for mode"
50
+ end
51
+
52
+ Termios.tcsetattr f, Termios::TCSANOW, t
53
+ Termios.tcflush f, Termios::TCIOFLUSH
54
+
55
+ if block_given?
56
+ begin
57
+ yield f
58
+ ensure
59
+ f.close
60
+ end
61
+ else
62
+ f
63
+ end
64
+ end
65
+ module_function :open
66
+
67
+ def data_bits t, val
68
+ t.cflag &= ~Termios::CSIZE # clear previous values
69
+ t.cflag |= Termios.const_get("CS#{val}") # Set the data bits
70
+ t
71
+ end
72
+ module_function :data_bits
73
+
74
+ def stop_bits t, val
75
+ case val
76
+ when 1 then t.cflag &= ~Termios::CSTOPB
77
+ when 2 then t.cflag |= Termios::CSTOPB
78
+ else
79
+ raise
80
+ end
81
+ t
82
+ end
83
+ module_function :stop_bits
84
+
85
+ def parity t, val
86
+ case val
87
+ when :none
88
+ t.cflag &= ~Termios::PARENB
89
+ when :even
90
+ t.cflag |= Termios::PARENB # Enable parity
91
+ t.cflag &= ~Termios::PARODD # Make it not odd
92
+ when :odd
93
+ t.cflag |= Termios::PARENB # Enable parity
94
+ t.cflag |= Termios::PARODD # Make it odd
95
+ else
96
+ raise
97
+ end
98
+ t
99
+ end
100
+ module_function :parity
101
+
102
+ def speed t, speed
103
+ t.ispeed = Termios.const_get("B#{speed}")
104
+ t.ospeed = Termios.const_get("B#{speed}")
105
+ t
106
+ end
107
+ module_function :speed
108
+
109
+ def read_timeout t, val
110
+ t.cc[Termios::VTIME] = val
111
+ t.cc[Termios::VMIN] = 0
112
+ t
113
+ end
114
+ module_function :read_timeout
115
+
116
+ def reading t
117
+ t.cflag |= Termios::CLOCAL | Termios::CREAD
118
+ t
119
+ end
120
+ module_function :reading
121
+ end
@@ -0,0 +1,10 @@
1
+ require 'minitest/autorun'
2
+ require 'uart'
3
+
4
+ class TestUart < MiniTest::Test
5
+ def test_sanity
6
+ assert "lol, I need to figure out a good way to make a fake serial port"
7
+ assert "but I don't want to use mocks"
8
+ assert "I think there is a way to do that without mocks"
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: uart
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Aaron Patterson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-01-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ruby-termios
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rdoc
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: hoe
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.16'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.16'
69
+ description: |-
70
+ UART is a simple wrapper around the ruby-termios gem that gives you an easy
71
+ interface for setting up a UART or serial connection. This gem is written in
72
+ pure Ruby. This gem relies on ruby-termios to provide bindings to the termios
73
+ system call, but uses those bindings to set up a serial connection in pure Ruby.
74
+ email:
75
+ - tenderlove@ruby-lang.org
76
+ executables: []
77
+ extensions: []
78
+ extra_rdoc_files:
79
+ - Manifest.txt
80
+ - README.md
81
+ files:
82
+ - Manifest.txt
83
+ - README.md
84
+ - Rakefile
85
+ - lib/uart.rb
86
+ - test/test_uart.rb
87
+ homepage: https://github.com/tenderlove/uart
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options:
93
+ - "--main"
94
+ - README.md
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.7.3
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: UART is a simple wrapper around the ruby-termios gem that gives you an easy
113
+ interface for setting up a UART or serial connection
114
+ test_files: []