Class: RS232

Inherits:
Object
  • Object
show all
Defined in:
lib/rs232.rb

Defined Under Namespace

Modules: Win32 Classes: CommTimeouts, DCB

Instance Attribute Summary collapse

Instance Method Summary collapse

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


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
57
58
59
60
61
62
63
64
65
# File 'lib/rs232.rb', line 27

def initialize address, params = {} 
  mode  = params[:mode]  || Win32::GENERIC_READ | Win32::GENERIC_WRITE
  share = params[:share] || 0 #Win32::FILE_SHARE_DELETE
  type  = params[:file]  || Win32::OPEN_EXISTING
  attr  = params[:attr]  || Win32::FILE_ATTRIBUTE_NORMAL

  @serial = Win32::CreateFileA( address, mode, share, nil, type, attr, nil) 
  @error  = Win32.error_check

  puts "RS232 >> got file handle 0x%.8x for com port %s" % [@serial, address]   

  # com port connection configuration with C struct
  DCB.new.tap do |p|  # check below for DCB struct definition
    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        

  # com port connection timeouts configuration with C struct
  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

#countObject (readonly)

number of last read/write bits



7
8
9
# File 'lib/rs232.rb', line 7

def count
  @count
end

#delimiterObject

flag set by client, if true reports read/write bits



5
6
7
# File 'lib/rs232.rb', line 5

def delimiter
  @delimiter
end

#errorObject (readonly)

number of last read/write bits



7
8
9
# File 'lib/rs232.rb', line 7

def error
  @error
end

#reportObject

flag set by client, if true reports read/write bits



5
6
7
# File 'lib/rs232.rb', line 5

def report
  @report
end

Instance Method Details

#grow_buffer(size) ⇒ Object

increases the buffer size for reading and writing



100
101
102
103
104
# File 'lib/rs232.rb', line 100

def grow_buffer size
  if @buffer.nil? || @buflen < size
    @buffer = FFI::MemoryPointer.new :char, @buflen = size
  end
end

#query(string) ⇒ Object

write + read helper method for queries



88
89
90
91
# File 'lib/rs232.rb', line 88

def query string
  write string
  read
end

#readObject

reads a string from the Serial port



80
81
82
83
84
85
# File 'lib/rs232.rb', line 80

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

#stopObject

closes the Com port TODO: sets this as object finilizer



94
95
96
97
# File 'lib/rs232.rb', line 94

def stop
  Win32::CloseHandle @serial
  @error = Win32.error_check
end

#write(string) ⇒ Object

writes a string to the Serial port automatically appends the delimiter characters stored in @delimiter



69
70
71
72
73
74
75
76
77
# File 'lib/rs232.rb', line 69

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
  @buffer.write_string "\0" #empty string buffer
  puts "write count %i" % @count.read_uint32 if @report
end