ccutrer-serialport 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.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/.rspec +3 -0
- data/.travis.yml +11 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +35 -0
- data/LICENSE +7 -0
- data/README.md +38 -0
- data/Rakefile +2 -0
- data/ccutrer-serialport.gemspec +24 -0
- data/lib/ccutrer-serialport.rb +22 -0
- data/lib/ccutrer/serial_port.rb +45 -0
- data/lib/ccutrer/serial_port/linux.rb +84 -0
- data/lib/ccutrer/serial_port/osx.rb +76 -0
- data/lib/ccutrer/serial_port/version.rb +7 -0
- data/spec/serialport_spec.rb +177 -0
- data/spec/spec_helper.rb +2 -0
- metadata +107 -0
    
        checksums.yaml
    ADDED
    
    | @@ -0,0 +1,7 @@ | |
| 1 | 
            +
            ---
         | 
| 2 | 
            +
            SHA256:
         | 
| 3 | 
            +
              metadata.gz: 5883d7fba69f81de31f8f8d2a21f25e1734fb25adce3a3ec9f873a67c9e96b43
         | 
| 4 | 
            +
              data.tar.gz: 780c226269254d5d413445159cdd8a3603d5e835c315eb00cc9c50557d1b1fbf
         | 
| 5 | 
            +
            SHA512:
         | 
| 6 | 
            +
              metadata.gz: 27f8bb29ca45fff55a30b0825dc1087f74259de5a81bb1a6671f90c95a52bf4e832f21ca40c79e84e1e486d5da362199d631a90134df34e09ce4b2162721acdc
         | 
| 7 | 
            +
              data.tar.gz: '08619960bba97f86e4752a14587b3b9d2a9b68723a76e681d7afe1e6f1f72a490c7cc23c9d43fc0155cc582db2e98e1323be9fcff23c45ce17a4d33dad711d1b'
         | 
    
        data/.gitignore
    ADDED
    
    
    
        data/.rspec
    ADDED
    
    
    
        data/.travis.yml
    ADDED
    
    
    
        data/Gemfile
    ADDED
    
    
    
        data/Gemfile.lock
    ADDED
    
    | @@ -0,0 +1,35 @@ | |
| 1 | 
            +
            PATH
         | 
| 2 | 
            +
              remote: .
         | 
| 3 | 
            +
              specs:
         | 
| 4 | 
            +
                ccutrer-serialport (1.0.0)
         | 
| 5 | 
            +
                  ffi (~> 1.9, >= 1.9.3)
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            GEM
         | 
| 8 | 
            +
              remote: http://rubygems.org/
         | 
| 9 | 
            +
              specs:
         | 
| 10 | 
            +
                diff-lcs (1.4.4)
         | 
| 11 | 
            +
                ffi (1.13.1)
         | 
| 12 | 
            +
                rake (13.0.1)
         | 
| 13 | 
            +
                rspec (3.0.0)
         | 
| 14 | 
            +
                  rspec-core (~> 3.0.0)
         | 
| 15 | 
            +
                  rspec-expectations (~> 3.0.0)
         | 
| 16 | 
            +
                  rspec-mocks (~> 3.0.0)
         | 
| 17 | 
            +
                rspec-core (3.0.4)
         | 
| 18 | 
            +
                  rspec-support (~> 3.0.0)
         | 
| 19 | 
            +
                rspec-expectations (3.0.4)
         | 
| 20 | 
            +
                  diff-lcs (>= 1.2.0, < 2.0)
         | 
| 21 | 
            +
                  rspec-support (~> 3.0.0)
         | 
| 22 | 
            +
                rspec-mocks (3.0.4)
         | 
| 23 | 
            +
                  rspec-support (~> 3.0.0)
         | 
| 24 | 
            +
                rspec-support (3.0.4)
         | 
| 25 | 
            +
             | 
| 26 | 
            +
            PLATFORMS
         | 
| 27 | 
            +
              ruby
         | 
| 28 | 
            +
             | 
| 29 | 
            +
            DEPENDENCIES
         | 
| 30 | 
            +
              ccutrer-serialport!
         | 
| 31 | 
            +
              rake (~> 13.0)
         | 
| 32 | 
            +
              rspec (~> 3.0.0)
         | 
| 33 | 
            +
             | 
| 34 | 
            +
            BUNDLED WITH
         | 
| 35 | 
            +
               2.1.4
         | 
    
        data/LICENSE
    ADDED
    
    | @@ -0,0 +1,7 @@ | |
| 1 | 
            +
            Copyright 2020 Cody Cutrer
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            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 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.
         | 
    
        data/README.md
    ADDED
    
    | @@ -0,0 +1,38 @@ | |
| 1 | 
            +
            # ccutrer-serialport
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            SerialPort is a simple Ruby gem for interacting with serial ports.
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            It does not require an extension thanks to using [FFI](https://github.com/ffi/ffi).
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            SerialPort objects inherit from `File` (and thus `IO`), so are compatible with all
         | 
| 8 | 
            +
            IO methods, in particular `read_partial`, and asynchronous IO.
         | 
| 9 | 
            +
             | 
| 10 | 
            +
            It was inspired by the [rubyserial](https://github.com/hybridgroup/rubyserial) gem.
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            ## Usage
         | 
| 13 | 
            +
             | 
| 14 | 
            +
            ```ruby
         | 
| 15 | 
            +
            require 'ccutrer-rubyserial'
         | 
| 16 | 
            +
            serialport = CCutrer::SerialPort.new '/dev/ttyACM0' # Defaults to 9600 baud, 8 data bits, and no parity
         | 
| 17 | 
            +
            serialport = CCutrer::SerialPort.new '/dev/ttyACM0', baud: 57600
         | 
| 18 | 
            +
            serialport = CCutrer::SerialPort.new '/dev/ttyACM0', baud: 19200, data_bits: 8, parity: :even
         | 
| 19 | 
            +
             | 
| 20 | 
            +
            serialport.read(4096)
         | 
| 21 | 
            +
            serialport.gets
         | 
| 22 | 
            +
             | 
| 23 | 
            +
            require 'io/wait'
         | 
| 24 | 
            +
            serialport.ready?
         | 
| 25 | 
            +
            serialport.wait_readable(1)
         | 
| 26 | 
            +
            ```
         | 
| 27 | 
            +
             | 
| 28 | 
            +
            ## Running the tests
         | 
| 29 | 
            +
             | 
| 30 | 
            +
            The test suite is written using rspec, just use the `rspec` command.
         | 
| 31 | 
            +
             | 
| 32 | 
            +
            ### Test dependencies
         | 
| 33 | 
            +
             | 
| 34 | 
            +
            To run the tests, you must also have `socat` installed.
         | 
| 35 | 
            +
             | 
| 36 | 
            +
            ## License
         | 
| 37 | 
            +
             | 
| 38 | 
            +
            MIT. See `LICENSE` for more details.
         | 
    
        data/Rakefile
    ADDED
    
    
| @@ -0,0 +1,24 @@ | |
| 1 | 
            +
            $:.push File.expand_path("../lib", __FILE__)
         | 
| 2 | 
            +
            require "ccutrer/serial_port/version"
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            Gem::Specification.new do |s|
         | 
| 5 | 
            +
              s.name        = "ccutrer-serialport"
         | 
| 6 | 
            +
              s.version     = CCutrer::SerialPort::VERSION
         | 
| 7 | 
            +
              s.summary     = "Linux/OS X RS-232 serial port communication"
         | 
| 8 | 
            +
              s.description = "Ruby only library that relies on FFI instead of an extension, and inherits from IO"
         | 
| 9 | 
            +
              s.homepage    = "https://github.com/ccutrer/ccutrer-serialport"
         | 
| 10 | 
            +
              s.authors     = ["Cody Cutrer"]
         | 
| 11 | 
            +
              s.platform    = Gem::Platform::RUBY
         | 
| 12 | 
            +
              s.license     = 'MIT'
         | 
| 13 | 
            +
             | 
| 14 | 
            +
              s.required_ruby_version = '>= 2.0'
         | 
| 15 | 
            +
             | 
| 16 | 
            +
              s.files         = `git ls-files`.split("\n")
         | 
| 17 | 
            +
              s.executables   = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
         | 
| 18 | 
            +
              s.require_paths = ["lib"]
         | 
| 19 | 
            +
             | 
| 20 | 
            +
              s.add_runtime_dependency 'ffi', '~> 1.9', '>= 1.9.3'
         | 
| 21 | 
            +
             | 
| 22 | 
            +
              s.add_development_dependency 'rake', '~> 13.0'
         | 
| 23 | 
            +
              s.add_development_dependency 'rspec', '~> 3.0.0'
         | 
| 24 | 
            +
            end
         | 
| @@ -0,0 +1,22 @@ | |
| 1 | 
            +
            # Copyright (c) 2014-2016 The Hybrid Group, 2020 Cody Cutrer
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require 'rbconfig'
         | 
| 4 | 
            +
            require 'ffi'
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            module CCutrer
         | 
| 7 | 
            +
              class SerialPort < File
         | 
| 8 | 
            +
                ON_WINDOWS = RbConfig::CONFIG['host_os'] =~ /mswin|windows|mingw/i
         | 
| 9 | 
            +
                ON_LINUX = RbConfig::CONFIG['host_os'] =~ /linux/i
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                if ON_WINDOWS
         | 
| 12 | 
            +
                  raise "rubyserial is not currently supported on windows"
         | 
| 13 | 
            +
                elsif ON_LINUX
         | 
| 14 | 
            +
                  require 'ccutrer/serial_port/linux'
         | 
| 15 | 
            +
                else
         | 
| 16 | 
            +
                  require 'ccutrer/serial_port/osx'
         | 
| 17 | 
            +
                end
         | 
| 18 | 
            +
              end
         | 
| 19 | 
            +
            end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
            require 'ccutrer/serial_port'
         | 
| 22 | 
            +
            require 'ccutrer/serial_port/version'
         | 
| @@ -0,0 +1,45 @@ | |
| 1 | 
            +
            # Copyright (c) 2014-2016 The Hybrid Group, 2020 Cody Cutrer
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require 'fcntl'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            class CCutrer::SerialPort < File
         | 
| 6 | 
            +
              def initialize(address, baud: 9600, data_bits: 8, parity: :none, stop_bits: 1)
         | 
| 7 | 
            +
                super(address, IO::RDWR | IO::NOCTTY | IO::NONBLOCK)
         | 
| 8 | 
            +
             | 
| 9 | 
            +
                raise IOError, "Not a serial port" unless tty?
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                fl = fcntl(Fcntl::F_GETFL, 0)
         | 
| 12 | 
            +
                fcntl(Fcntl::F_SETFL, ~Fcntl::O_NONBLOCK & fl)
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                @config = build_config(baud, data_bits, parity, stop_bits)
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                err = Posix.tcsetattr(fileno, Posix::TCSANOW, @config)
         | 
| 17 | 
            +
                if err == -1
         | 
| 18 | 
            +
                  raise SystemCallError, FFI.errno
         | 
| 19 | 
            +
                end
         | 
| 20 | 
            +
              end
         | 
| 21 | 
            +
             | 
| 22 | 
            +
              private
         | 
| 23 | 
            +
             | 
| 24 | 
            +
              def build_config(baud_rate, data_bits, parity, stop_bits)
         | 
| 25 | 
            +
                config = Posix::Termios.new
         | 
| 26 | 
            +
             | 
| 27 | 
            +
                config[:c_iflag]  = Posix::IGNPAR
         | 
| 28 | 
            +
                config[:c_ispeed] = Posix::BAUD_RATES[baud_rate]
         | 
| 29 | 
            +
                config[:c_ospeed] = Posix::BAUD_RATES[baud_rate]
         | 
| 30 | 
            +
                config[:c_cflag]  = Posix::DATA_BITS[data_bits] |
         | 
| 31 | 
            +
                  Posix::CREAD |
         | 
| 32 | 
            +
                  Posix::CLOCAL |
         | 
| 33 | 
            +
                  Posix::PARITY[parity] |
         | 
| 34 | 
            +
                  Posix::STOPBITS[stop_bits]
         | 
| 35 | 
            +
             | 
| 36 | 
            +
                # Masking in baud rate on OS X would corrupt the settings.
         | 
| 37 | 
            +
                if ON_LINUX
         | 
| 38 | 
            +
                  config[:c_cflag] = config[:c_cflag] | Posix::BAUD_RATES[baud_rate]
         | 
| 39 | 
            +
                end
         | 
| 40 | 
            +
             | 
| 41 | 
            +
                config[:cc_c][Posix::VMIN] = 0
         | 
| 42 | 
            +
             | 
| 43 | 
            +
                config
         | 
| 44 | 
            +
              end
         | 
| 45 | 
            +
            end
         | 
| @@ -0,0 +1,84 @@ | |
| 1 | 
            +
            # Copyright (c) 2014-2016 The Hybrid Group, 2020 Cody Cutrer
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module CCutrer::SerialPort::Posix
         | 
| 4 | 
            +
              extend FFI::Library
         | 
| 5 | 
            +
              ffi_lib FFI::Library::LIBC
         | 
| 6 | 
            +
             | 
| 7 | 
            +
              VTIME = 5
         | 
| 8 | 
            +
              TCSANOW = 0
         | 
| 9 | 
            +
              TCSETS = 0x5402
         | 
| 10 | 
            +
              IGNPAR = 0000004
         | 
| 11 | 
            +
              PARENB = 0000400
         | 
| 12 | 
            +
              PARODD = 0001000
         | 
| 13 | 
            +
              CSTOPB = 0000100
         | 
| 14 | 
            +
              CREAD  = 0000200
         | 
| 15 | 
            +
              CLOCAL = 0004000
         | 
| 16 | 
            +
              VMIN = 6
         | 
| 17 | 
            +
              NCCS = 32
         | 
| 18 | 
            +
             | 
| 19 | 
            +
              DATA_BITS = {
         | 
| 20 | 
            +
                5 => 0000000,
         | 
| 21 | 
            +
                6 => 0000020,
         | 
| 22 | 
            +
                7 => 0000040,
         | 
| 23 | 
            +
                8 => 0000060
         | 
| 24 | 
            +
              }
         | 
| 25 | 
            +
             | 
| 26 | 
            +
              BAUD_RATES = {
         | 
| 27 | 
            +
                0 => 0000000,
         | 
| 28 | 
            +
                50 => 0000001,
         | 
| 29 | 
            +
                75 => 0000002,
         | 
| 30 | 
            +
                110 => 0000003,
         | 
| 31 | 
            +
                134 => 0000004,
         | 
| 32 | 
            +
                150 => 0000005,
         | 
| 33 | 
            +
                200 => 0000006,
         | 
| 34 | 
            +
                300 => 0000007,
         | 
| 35 | 
            +
                600 => 0000010,
         | 
| 36 | 
            +
                1200 => 0000011,
         | 
| 37 | 
            +
                1800 => 0000012,
         | 
| 38 | 
            +
                2400 => 0000013,
         | 
| 39 | 
            +
                4800 => 0000014,
         | 
| 40 | 
            +
                9600 => 0000015,
         | 
| 41 | 
            +
                19200 => 0000016,
         | 
| 42 | 
            +
                38400 => 0000017,
         | 
| 43 | 
            +
                57600 => 0010001,
         | 
| 44 | 
            +
                115200 => 0010002,
         | 
| 45 | 
            +
                230400 => 0010003,
         | 
| 46 | 
            +
                460800 => 0010004,
         | 
| 47 | 
            +
                500000 => 0010005,
         | 
| 48 | 
            +
                576000 => 0010006,
         | 
| 49 | 
            +
                921600 => 0010007,
         | 
| 50 | 
            +
                1000000 => 0010010,
         | 
| 51 | 
            +
                1152000 => 0010011,
         | 
| 52 | 
            +
                1500000 => 0010012,
         | 
| 53 | 
            +
                2000000 => 0010013,
         | 
| 54 | 
            +
                2500000 => 0010014,
         | 
| 55 | 
            +
                3000000 => 0010015,
         | 
| 56 | 
            +
                3500000 => 0010016,
         | 
| 57 | 
            +
                4000000 => 0010017
         | 
| 58 | 
            +
              }
         | 
| 59 | 
            +
             | 
| 60 | 
            +
              PARITY = {
         | 
| 61 | 
            +
                :none => 0000000,
         | 
| 62 | 
            +
                :even => PARENB,
         | 
| 63 | 
            +
                :odd => PARENB | PARODD,
         | 
| 64 | 
            +
              }
         | 
| 65 | 
            +
             | 
| 66 | 
            +
              STOPBITS = {
         | 
| 67 | 
            +
                1 => 0x00000000,
         | 
| 68 | 
            +
                2 => CSTOPB
         | 
| 69 | 
            +
              }
         | 
| 70 | 
            +
             | 
| 71 | 
            +
              class Termios < FFI::Struct
         | 
| 72 | 
            +
                layout  :c_iflag, :uint,
         | 
| 73 | 
            +
                        :c_oflag, :uint,
         | 
| 74 | 
            +
                        :c_cflag, :uint,
         | 
| 75 | 
            +
                        :c_lflag, :uint,
         | 
| 76 | 
            +
                        :c_line, :uchar,
         | 
| 77 | 
            +
                        :cc_c, [ :uchar, NCCS ],
         | 
| 78 | 
            +
                        :c_ispeed, :uint,
         | 
| 79 | 
            +
                        :c_ospeed, :uint
         | 
| 80 | 
            +
              end
         | 
| 81 | 
            +
             | 
| 82 | 
            +
              attach_function :tcsetattr, [ :int, :int, Termios ], :int, blocking: true
         | 
| 83 | 
            +
              attach_function :tcgetattr, [ :int, Termios ], :int, blocking: true
         | 
| 84 | 
            +
            end
         | 
| @@ -0,0 +1,76 @@ | |
| 1 | 
            +
            # Copyright (c) 2014-2016 The Hybrid Group, 2020 Cody Cutrer
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module CCutrer::SerialPort::Posix
         | 
| 4 | 
            +
              extend FFI::Library
         | 
| 5 | 
            +
              ffi_lib FFI::Library::LIBC
         | 
| 6 | 
            +
             | 
| 7 | 
            +
              IGNPAR = 0x00000004
         | 
| 8 | 
            +
              PARENB = 0x00001000
         | 
| 9 | 
            +
              PARODD = 0x00002000
         | 
| 10 | 
            +
              VMIN = 16
         | 
| 11 | 
            +
              VTIME = 17
         | 
| 12 | 
            +
              CLOCAL = 0x00008000
         | 
| 13 | 
            +
              CSTOPB = 0x00000400
         | 
| 14 | 
            +
              CREAD  = 0x00000800
         | 
| 15 | 
            +
              CCTS_OFLOW = 0x00010000 # Clearing this disables RTS AND CTS.
         | 
| 16 | 
            +
              TCSANOW = 0
         | 
| 17 | 
            +
              NCCS = 20
         | 
| 18 | 
            +
             | 
| 19 | 
            +
              DATA_BITS = {
         | 
| 20 | 
            +
                5 => 0x00000000,
         | 
| 21 | 
            +
                6 => 0x00000100,
         | 
| 22 | 
            +
                7 => 0x00000200,
         | 
| 23 | 
            +
                8 => 0x00000300
         | 
| 24 | 
            +
              }
         | 
| 25 | 
            +
             | 
| 26 | 
            +
              BAUD_RATES = {
         | 
| 27 | 
            +
                0 => 0,
         | 
| 28 | 
            +
                50 => 50,
         | 
| 29 | 
            +
                75 => 75,
         | 
| 30 | 
            +
                110 => 110,
         | 
| 31 | 
            +
                134 => 134,
         | 
| 32 | 
            +
                150 => 150,
         | 
| 33 | 
            +
                200 => 200,
         | 
| 34 | 
            +
                300 => 300,
         | 
| 35 | 
            +
                600 => 600,
         | 
| 36 | 
            +
                1200 => 1200,
         | 
| 37 | 
            +
                1800 => 1800,
         | 
| 38 | 
            +
                2400 => 2400,
         | 
| 39 | 
            +
                4800 => 4800,
         | 
| 40 | 
            +
                9600 => 9600,
         | 
| 41 | 
            +
                19200 => 19200,
         | 
| 42 | 
            +
                38400 => 38400,
         | 
| 43 | 
            +
                7200 =>  7200,
         | 
| 44 | 
            +
                14400 => 14400,
         | 
| 45 | 
            +
                28800 => 28800,
         | 
| 46 | 
            +
                57600 => 57600,
         | 
| 47 | 
            +
                76800 => 76800,
         | 
| 48 | 
            +
                115200 => 115200,
         | 
| 49 | 
            +
                230400 => 230400
         | 
| 50 | 
            +
              }
         | 
| 51 | 
            +
             | 
| 52 | 
            +
              PARITY = {
         | 
| 53 | 
            +
                :none => 0x00000000,
         | 
| 54 | 
            +
                :even => PARENB,
         | 
| 55 | 
            +
                :odd => PARENB | PARODD,
         | 
| 56 | 
            +
              }
         | 
| 57 | 
            +
             | 
| 58 | 
            +
              STOPBITS = {
         | 
| 59 | 
            +
                1 => 0x00000000,
         | 
| 60 | 
            +
                2 => CSTOPB
         | 
| 61 | 
            +
              }
         | 
| 62 | 
            +
             | 
| 63 | 
            +
              class Termios < FFI::Struct
         | 
| 64 | 
            +
                layout  :c_iflag, :ulong,
         | 
| 65 | 
            +
                        :c_oflag, :ulong,
         | 
| 66 | 
            +
                        :c_cflag, :ulong,
         | 
| 67 | 
            +
                        :c_lflag, :ulong,
         | 
| 68 | 
            +
                        :c_line, :uchar,
         | 
| 69 | 
            +
                        :cc_c, [ :uchar, NCCS ],
         | 
| 70 | 
            +
                        :c_ispeed, :ulong,
         | 
| 71 | 
            +
                        :c_ospeed, :ulong
         | 
| 72 | 
            +
              end
         | 
| 73 | 
            +
             | 
| 74 | 
            +
              attach_function :tcsetattr, [ :int, :int, Termios ], :int, blocking: true
         | 
| 75 | 
            +
              attach_function :tcgetattr, [ :int, Termios ], :int, blocking: true
         | 
| 76 | 
            +
            end
         | 
| @@ -0,0 +1,177 @@ | |
| 1 | 
            +
            require 'ccutrer-serialport'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            describe CCutrer::SerialPort do
         | 
| 4 | 
            +
              before do
         | 
| 5 | 
            +
                @ports = []
         | 
| 6 | 
            +
                File.delete('socat.log') if File.file?('socat.log')
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                raise 'socat not found' unless (`socat -h` && $? == 0)
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                Thread.new do
         | 
| 11 | 
            +
                  system('socat -lf socat.log -d -d pty,raw,echo=0 pty,raw,echo=0')
         | 
| 12 | 
            +
                end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                @ptys = nil
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                loop do
         | 
| 17 | 
            +
                  if File.file? 'socat.log'
         | 
| 18 | 
            +
                    @file = File.open('socat.log', "r")
         | 
| 19 | 
            +
                    @fileread = @file.read
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                    unless @fileread.count("\n") < 3
         | 
| 22 | 
            +
                      @ptys = @fileread.scan(/PTY is (.*)/)
         | 
| 23 | 
            +
                      break
         | 
| 24 | 
            +
                    end
         | 
| 25 | 
            +
                  end
         | 
| 26 | 
            +
                end
         | 
| 27 | 
            +
             | 
| 28 | 
            +
                @ports = [@ptys[1][0], @ptys[0][0]]
         | 
| 29 | 
            +
             | 
| 30 | 
            +
                @sp2 = CCutrer::SerialPort.new(@ports[0])
         | 
| 31 | 
            +
                @sp = CCutrer::SerialPort.new(@ports[1])
         | 
| 32 | 
            +
              end
         | 
| 33 | 
            +
             | 
| 34 | 
            +
              after do
         | 
| 35 | 
            +
               @sp2.close
         | 
| 36 | 
            +
               @sp.close
         | 
| 37 | 
            +
              end
         | 
| 38 | 
            +
             | 
| 39 | 
            +
              it "should read and write" do
         | 
| 40 | 
            +
                @sp2.write('hello')
         | 
| 41 | 
            +
                # small delay so it can write to the other port.
         | 
| 42 | 
            +
                sleep 0.1
         | 
| 43 | 
            +
                check = @sp.read(5)
         | 
| 44 | 
            +
                expect(check).to eql('hello')
         | 
| 45 | 
            +
              end
         | 
| 46 | 
            +
             | 
| 47 | 
            +
              it "should convert ints to strings" do
         | 
| 48 | 
            +
                expect(@sp2.write(123)).to eql(3)
         | 
| 49 | 
            +
                sleep 0.1
         | 
| 50 | 
            +
                expect(@sp.read(3)).to eql('123')
         | 
| 51 | 
            +
              end
         | 
| 52 | 
            +
             | 
| 53 | 
            +
              it "write should return bytes written" do
         | 
| 54 | 
            +
                expect(@sp2.write('hello')).to eql(5)
         | 
| 55 | 
            +
              end
         | 
| 56 | 
            +
             | 
| 57 | 
            +
              it "reading nothing should be blank" do
         | 
| 58 | 
            +
                expect(@sp.read(5)).to be_nil
         | 
| 59 | 
            +
              end
         | 
| 60 | 
            +
             | 
| 61 | 
            +
              it "should give me nil on getbyte" do
         | 
| 62 | 
            +
                expect(@sp.getbyte).to be_nil
         | 
| 63 | 
            +
              end
         | 
| 64 | 
            +
             | 
| 65 | 
            +
              it 'should give me a zero byte from getbyte' do
         | 
| 66 | 
            +
                @sp2.write("\x00")
         | 
| 67 | 
            +
                sleep 0.1
         | 
| 68 | 
            +
                expect(@sp.getbyte).to eql(0)
         | 
| 69 | 
            +
              end
         | 
| 70 | 
            +
             | 
| 71 | 
            +
              it "should give me bytes" do
         | 
| 72 | 
            +
                @sp2.write('hello')
         | 
| 73 | 
            +
                # small delay so it can write to the other port.
         | 
| 74 | 
            +
                sleep 0.1
         | 
| 75 | 
            +
                check = @sp.getbyte
         | 
| 76 | 
            +
                expect([check].pack('C')).to eql('h')
         | 
| 77 | 
            +
              end
         | 
| 78 | 
            +
             | 
| 79 | 
            +
              describe "giving me lines" do
         | 
| 80 | 
            +
                it "should give me a line" do
         | 
| 81 | 
            +
                  @sp.write("no yes \n hello")
         | 
| 82 | 
            +
                  sleep 0.1
         | 
| 83 | 
            +
                  expect(@sp2.gets).to eql("no yes \n")
         | 
| 84 | 
            +
                end
         | 
| 85 | 
            +
             | 
| 86 | 
            +
                it "should give me a line with block" do
         | 
| 87 | 
            +
                  @sp.write("no yes \n hello")
         | 
| 88 | 
            +
                  sleep 0.1
         | 
| 89 | 
            +
                  result = ""
         | 
| 90 | 
            +
                  @sp2.each_line do |line|
         | 
| 91 | 
            +
                    result = line
         | 
| 92 | 
            +
                    break if !result.empty?
         | 
| 93 | 
            +
                  end
         | 
| 94 | 
            +
                  expect(result).to eql("no yes \n")
         | 
| 95 | 
            +
                end
         | 
| 96 | 
            +
             | 
| 97 | 
            +
                it "should accept a sep param" do
         | 
| 98 | 
            +
                  @sp.write('no yes END bleh')
         | 
| 99 | 
            +
                  sleep 0.1
         | 
| 100 | 
            +
                  expect(@sp2.gets('END')).to eql("no yes END")
         | 
| 101 | 
            +
                end
         | 
| 102 | 
            +
             | 
| 103 | 
            +
                it "should accept a limit param" do
         | 
| 104 | 
            +
                  @sp.write("no yes \n hello")
         | 
| 105 | 
            +
                  sleep 0.1
         | 
| 106 | 
            +
                  expect(@sp2.gets(4)).to eql("no y")
         | 
| 107 | 
            +
                end
         | 
| 108 | 
            +
             | 
| 109 | 
            +
                it "should accept limit and sep params" do
         | 
| 110 | 
            +
                  @sp.write("no yes END hello")
         | 
| 111 | 
            +
                  sleep 0.1
         | 
| 112 | 
            +
                  expect(@sp2.gets('END', 20)).to eql("no yes END")
         | 
| 113 | 
            +
                  @sp2.read(1000)
         | 
| 114 | 
            +
                  @sp.write("no yes END hello")
         | 
| 115 | 
            +
                  sleep 0.1
         | 
| 116 | 
            +
                  expect(@sp2.gets('END', 4)).to eql('no y')
         | 
| 117 | 
            +
                end
         | 
| 118 | 
            +
             | 
| 119 | 
            +
                it "should read a paragraph at a time" do
         | 
| 120 | 
            +
                  @sp.write("Something \n Something else \n\n and other stuff")
         | 
| 121 | 
            +
                  sleep 0.1
         | 
| 122 | 
            +
                  expect(@sp2.gets('')).to eql("Something \n Something else \n\n")
         | 
| 123 | 
            +
                end
         | 
| 124 | 
            +
              end
         | 
| 125 | 
            +
             | 
| 126 | 
            +
              describe 'config' do
         | 
| 127 | 
            +
                it 'should accept EVEN parity' do
         | 
| 128 | 
            +
                  @sp2.close
         | 
| 129 | 
            +
                  @sp.close
         | 
| 130 | 
            +
                  @sp2 = CCutrer::SerialPort.new(@ports[0], baud: 19200, data_bits: 8, parity: :even)
         | 
| 131 | 
            +
                  @sp = CCutrer::SerialPort.new(@ports[1], baud: 19200, data_bits: 8, parity: :even)
         | 
| 132 | 
            +
                  @sp.write("Hello!\n")
         | 
| 133 | 
            +
                  sleep 0.1
         | 
| 134 | 
            +
                  expect(@sp2.gets).to eql("Hello!\n")
         | 
| 135 | 
            +
                end
         | 
| 136 | 
            +
             | 
| 137 | 
            +
                it 'should accept ODD parity' do
         | 
| 138 | 
            +
                  @sp2.close
         | 
| 139 | 
            +
                  @sp.close
         | 
| 140 | 
            +
                  @sp2 = CCutrer::SerialPort.new(@ports[0], baud: 19200, data_bits: 8, parity: :odd)
         | 
| 141 | 
            +
                  @sp = CCutrer::SerialPort.new(@ports[1], baud: 19200, data_bits: 8, parity: :odd)
         | 
| 142 | 
            +
                  @sp.write("Hello!\n")
         | 
| 143 | 
            +
                  sleep 0.1
         | 
| 144 | 
            +
                  expect(@sp2.gets).to eql("Hello!\n")
         | 
| 145 | 
            +
                end
         | 
| 146 | 
            +
             | 
| 147 | 
            +
                it 'should accept 1 stop bit' do
         | 
| 148 | 
            +
                  @sp2.close
         | 
| 149 | 
            +
                  @sp.close
         | 
| 150 | 
            +
                  @sp2 = CCutrer::SerialPort.new(@ports[0], baud: 19200, data_bits: 8, parity: :none, stop_bits: 1)
         | 
| 151 | 
            +
                  @sp = CCutrer::SerialPort.new(@ports[1], baud: 19200, data_bits: 8, parity: :none, stop_bits: 1)
         | 
| 152 | 
            +
                  @sp.write("Hello!\n")
         | 
| 153 | 
            +
                  sleep 0.1
         | 
| 154 | 
            +
                  expect(@sp2.gets).to eql("Hello!\n")
         | 
| 155 | 
            +
                end
         | 
| 156 | 
            +
             | 
| 157 | 
            +
                it 'should accept 2 stop bits' do
         | 
| 158 | 
            +
                  @sp2.close
         | 
| 159 | 
            +
                  @sp.close
         | 
| 160 | 
            +
                  @sp2 = CCutrer::SerialPort.new(@ports[0], baud: 19200, data_bits: 8, parity: :none, stop_bits: 2)
         | 
| 161 | 
            +
                  @sp = CCutrer::SerialPort.new(@ports[1], baud: 19200, data_bits: 8, parity: :none, stop_bits: 2)
         | 
| 162 | 
            +
                  @sp.write("Hello!\n")
         | 
| 163 | 
            +
                  sleep 0.1
         | 
| 164 | 
            +
                  expect(@sp2.gets).to eql("Hello!\n")
         | 
| 165 | 
            +
                end
         | 
| 166 | 
            +
             | 
| 167 | 
            +
                it 'should set baud rate, check #46 fixed' do
         | 
| 168 | 
            +
                  @sp.close
         | 
| 169 | 
            +
                  rate = 600
         | 
| 170 | 
            +
                  @sp = CCutrer::SerialPort.new(@ports[1], baud: rate)
         | 
| 171 | 
            +
                  fd = @sp.fileno
         | 
| 172 | 
            +
                  termios = CCutrer::SerialPort::Posix::Termios.new
         | 
| 173 | 
            +
                  CCutrer::SerialPort::Posix.tcgetattr(fd, termios)
         | 
| 174 | 
            +
                  expect(termios[:c_ispeed]).to eql(CCutrer::SerialPort::Posix::BAUD_RATES[rate])
         | 
| 175 | 
            +
                end
         | 
| 176 | 
            +
              end
         | 
| 177 | 
            +
            end
         | 
    
        data/spec/spec_helper.rb
    ADDED
    
    
    
        metadata
    ADDED
    
    | @@ -0,0 +1,107 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification
         | 
| 2 | 
            +
            name: ccutrer-serialport
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            +
              version: 1.0.0
         | 
| 5 | 
            +
            platform: ruby
         | 
| 6 | 
            +
            authors:
         | 
| 7 | 
            +
            - Cody Cutrer
         | 
| 8 | 
            +
            autorequire:
         | 
| 9 | 
            +
            bindir: bin
         | 
| 10 | 
            +
            cert_chain: []
         | 
| 11 | 
            +
            date: 2020-09-25 00:00:00.000000000 Z
         | 
| 12 | 
            +
            dependencies:
         | 
| 13 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 14 | 
            +
              name: ffi
         | 
| 15 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 16 | 
            +
                requirements:
         | 
| 17 | 
            +
                - - "~>"
         | 
| 18 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 19 | 
            +
                    version: '1.9'
         | 
| 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'
         | 
| 30 | 
            +
                - - ">="
         | 
| 31 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 32 | 
            +
                    version: 1.9.3
         | 
| 33 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 34 | 
            +
              name: rake
         | 
| 35 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 36 | 
            +
                requirements:
         | 
| 37 | 
            +
                - - "~>"
         | 
| 38 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 39 | 
            +
                    version: '13.0'
         | 
| 40 | 
            +
              type: :development
         | 
| 41 | 
            +
              prerelease: false
         | 
| 42 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 43 | 
            +
                requirements:
         | 
| 44 | 
            +
                - - "~>"
         | 
| 45 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 46 | 
            +
                    version: '13.0'
         | 
| 47 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 48 | 
            +
              name: rspec
         | 
| 49 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 50 | 
            +
                requirements:
         | 
| 51 | 
            +
                - - "~>"
         | 
| 52 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 53 | 
            +
                    version: 3.0.0
         | 
| 54 | 
            +
              type: :development
         | 
| 55 | 
            +
              prerelease: false
         | 
| 56 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 57 | 
            +
                requirements:
         | 
| 58 | 
            +
                - - "~>"
         | 
| 59 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 60 | 
            +
                    version: 3.0.0
         | 
| 61 | 
            +
            description: Ruby only library that relies on FFI instead of an extension, and inherits
         | 
| 62 | 
            +
              from IO
         | 
| 63 | 
            +
            email:
         | 
| 64 | 
            +
            executables: []
         | 
| 65 | 
            +
            extensions: []
         | 
| 66 | 
            +
            extra_rdoc_files: []
         | 
| 67 | 
            +
            files:
         | 
| 68 | 
            +
            - ".gitignore"
         | 
| 69 | 
            +
            - ".rspec"
         | 
| 70 | 
            +
            - ".travis.yml"
         | 
| 71 | 
            +
            - Gemfile
         | 
| 72 | 
            +
            - Gemfile.lock
         | 
| 73 | 
            +
            - LICENSE
         | 
| 74 | 
            +
            - README.md
         | 
| 75 | 
            +
            - Rakefile
         | 
| 76 | 
            +
            - ccutrer-serialport.gemspec
         | 
| 77 | 
            +
            - lib/ccutrer-serialport.rb
         | 
| 78 | 
            +
            - lib/ccutrer/serial_port.rb
         | 
| 79 | 
            +
            - lib/ccutrer/serial_port/linux.rb
         | 
| 80 | 
            +
            - lib/ccutrer/serial_port/osx.rb
         | 
| 81 | 
            +
            - lib/ccutrer/serial_port/version.rb
         | 
| 82 | 
            +
            - spec/serialport_spec.rb
         | 
| 83 | 
            +
            - spec/spec_helper.rb
         | 
| 84 | 
            +
            homepage: https://github.com/ccutrer/ccutrer-serialport
         | 
| 85 | 
            +
            licenses:
         | 
| 86 | 
            +
            - MIT
         | 
| 87 | 
            +
            metadata: {}
         | 
| 88 | 
            +
            post_install_message:
         | 
| 89 | 
            +
            rdoc_options: []
         | 
| 90 | 
            +
            require_paths:
         | 
| 91 | 
            +
            - lib
         | 
| 92 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 93 | 
            +
              requirements:
         | 
| 94 | 
            +
              - - ">="
         | 
| 95 | 
            +
                - !ruby/object:Gem::Version
         | 
| 96 | 
            +
                  version: '2.0'
         | 
| 97 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 98 | 
            +
              requirements:
         | 
| 99 | 
            +
              - - ">="
         | 
| 100 | 
            +
                - !ruby/object:Gem::Version
         | 
| 101 | 
            +
                  version: '0'
         | 
| 102 | 
            +
            requirements: []
         | 
| 103 | 
            +
            rubygems_version: 3.1.2
         | 
| 104 | 
            +
            signing_key:
         | 
| 105 | 
            +
            specification_version: 4
         | 
| 106 | 
            +
            summary: Linux/OS X RS-232 serial port communication
         | 
| 107 | 
            +
            test_files: []
         |