module Serial
Attributes
baud[R]
Determine the current serial port baud rate by querying the underlying operating system
data_bits[R]
Determine the current serial port data bits by querying the underlying operating system
parity[R]
Determine the current serial port parity by querying the underlying operating system
read_timeout[RW]
Get/Set the read_timeout
-
0< nonblocking IO
-
0 - blocking IO
-
>0 - Timeout in seconds (Float)
stop_bits[R]
Determine the current serial port stop bits by querying the underlying operating system
Public Class Methods
new(port: '/dev/tty or COM1')
click to toggle source
new(port: '/dev/tty or COM1', baud: 9600, data_bits: 8, stop_bits: 1, parity: :none)
Create a new Ruby IO configured with the serial port parameters
# File lib/ffi-serial.rb 39 def self.new(config) 40 driver = if ('Windows_NT' == ENV['OS']) 41 @@loaded_ffi_serial_windows ||= begin 42 require 'ffi-serial/windows' 43 true 44 end 45 Windows 46 else 47 @@loaded_ffi_serial_posix ||= begin 48 require 'ffi-serial/posix' 49 true 50 end 51 Posix 52 end 53 54 config = config.each_with_object({}) { |(k,v),r| r[k.to_s.strip.chomp.downcase.gsub(/\-|\_|\s/, '')] = v } 55 56 port = config.delete('port') { raise ArgumentError.new ':port not specified' } 57 baud = config.delete('baud') { 9600 } 58 data_bits = config.delete('databits') { 8 } 59 stop_bits = config.delete('stopbits') { 1 } 60 parity = config.delete('parity') { :none } 61 62 if !config.empty? 63 raise ArgumentError.new "Unknown options specified: #{config.keys}" 64 end 65 66 # Create a new Ruby IO pointing to the serial port and configure it 67 # using the OS specific function 68 new_instance = driver.method(:new).call( 69 port, 70 Integer(baud), 71 Integer(data_bits), 72 Integer(stop_bits), 73 parity.to_s.strip.chomp.downcase.to_sym) 74 75 new_instance 76 end