Class: RS232
- Inherits:
-
Object
- Object
- RS232
- Defined in:
- lib/rs232.rb
Defined Under Namespace
Modules: Win32 Classes: CommTimeouts, DCB
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#delimiter ⇒ Object
Returns the value of attribute delimiter.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#report ⇒ Object
Returns the value of attribute report.
Instance Method Summary collapse
-
#grow_buffer(size) ⇒ Object
increase the buffer size for reading and writing.
-
#initialize(address, params = {}) ⇒ RS232
constructor
serial port object constructor also sets the parameters and timeout properties through an hash argument hash arguments options: :mode :file :attr :dcblength :baudrate :bytesize :stopbits :parity :delimiter :read_interval_timeout :read_total_timeout_multiplier :read_total_timeout_constant :write_total_timeout_multiplier :write_total_timeout_constant.
-
#query(string) ⇒ Object
write and read helper method.
-
#read ⇒ Object
read a string from the Serial port.
-
#stop ⇒ Object
close the Com port.
-
#write(string) ⇒ Object
writes a string to the Serial port and happens the delimiter characters stores in @delimiters.
Constructor Details
#initialize(address, params = {}) ⇒ RS232
serial port object constructor also sets the parameters and timeout properties through an hash argument
hash arguments options:
:mode
:file
:attr
:dcblength
:baudrate
:bytesize
:stopbits
:parity
:delimiter
:read_interval_timeout
:read_total_timeout_multiplier
:read_total_timeout_constant
:write_total_timeout_multiplier
:write_total_timeout_constant
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/rs232.rb', line 26 def initialize address, params = {} mode = param[:mode] || Win32::GENERIC_READ | Win32::GENERIC_WRITE type = param[:file] || Win32::OPEN_EXISTING attr = param[:attr] || Win32::FILE_ATTRIBUTE_NORMAL @serial = Win32::CreateFileA( address, mode, 0, nil, type, attr, nil) @error = Win32.error_check puts "RS232 >> got file handle 0x%.8x for com port %s" % [@serial, address] DCB.new.tap do |p| p[:dcblength] = DCB::Sizeof Win32::GetCommState @serial, p p[:baudrate] = params[:baudrate] || 9600 p[:bytesize] = params[:bytesize] || 8 p[:stopbits] = params[:stopbits] || DCB::ONESTOPBIT p[:parity] = params[:parity] || DCB::NOPARITY Win32::SetCommState @serial, p @error = Win32.error_check end CommTimeouts.new.tap do |timeouts| timeouts[:read_interval_timeout] = params[:read_interval_timeout] || 50 timeouts[:read_total_timeout_multiplier] = params[:read_total_timeout_multiplier] || 50 timeouts[:read_total_timeout_constant] = params[:read_total_timeout_constant] || 10 timeouts[:write_total_timeout_multiplier] = params[:write_total_timeout_multiplier] || 50 timeouts[:write_total_timeout_constant] = params[:write_total_timeout_constant] || 10 Win32::SetCommTimeouts @serial, timeouts @error = Win32.error_check end grow_buffer 128 @count = FFI::MemoryPointer.new :uint, 1 @report = false @delimiter = params[:delimiter] || "\r\n" end |
Instance Attribute Details
#count ⇒ Object (readonly)
Returns the value of attribute count.
7 8 9 |
# File 'lib/rs232.rb', line 7 def count @count end |
#delimiter ⇒ Object
Returns the value of attribute delimiter.
6 7 8 |
# File 'lib/rs232.rb', line 6 def delimiter @delimiter end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
7 8 9 |
# File 'lib/rs232.rb', line 7 def error @error end |
#report ⇒ Object
Returns the value of attribute report.
6 7 8 |
# File 'lib/rs232.rb', line 6 def report @report end |
Instance Method Details
#grow_buffer(size) ⇒ Object
increase the buffer size for reading and writing
89 90 91 |
# File 'lib/rs232.rb', line 89 def grow_buffer size @buffer = FFI::MemoryPointer.new :char, @buflen = size if @buffer.nil? || @buflen < size end |
#query(string) ⇒ Object
write and read helper method
77 78 79 80 |
# File 'lib/rs232.rb', line 77 def query string write string read end |
#read ⇒ Object
read a string from the Serial port
69 70 71 72 73 74 |
# File 'lib/rs232.rb', line 69 def read Win32::ReadFile @serial, @buffer, @buflen, @count, nil @error = Win32.error_check puts "read count %i" % @count.read_uint32 if @report @buffer.read_string.chomp end |
#stop ⇒ Object
close the Com port
83 84 85 86 |
# File 'lib/rs232.rb', line 83 def stop Win32::CloseHandle @serial @error = Win32.error_check end |
#write(string) ⇒ Object
writes a string to the Serial port and happens the delimiter characters stores in @delimiters
59 60 61 62 63 64 65 66 |
# File 'lib/rs232.rb', line 59 def write string command = "%s%s" % [string.chomp, @delimiter] grow_buffer command.length @buffer.write_string command Win32::WriteFile @serial, @buffer, command.length, @count, nil @error = Win32.error_check puts "write count %i" % @count.read_uint32 if @report end |