require 'bit-struct'

class RS232_9 < BitStruct
  unsigned :cd,  1,        
  unsigned :rd,  1,         
  unsigned :td,  1,      
  unsigned :dtr, 1,   
  unsigned :sg,  1,         
  unsigned :dsr, 1,        
  unsigned :rts, 1,       
  unsigned :cts, 1,         
  unsigned :ri,  1,        

  def self.new_with_int(value)
    data = {}
    fields.each_with_index {|f, i| data[f.name] = value[i]}
    new(data)
  end
end

num = rand(2**9 - 1)
puts 

sample1 = RS232_9.new([( % num.to_s(2)).reverse].pack())
puts sample1.inspect_detailed

sample2 = RS232_9.new_with_int(num)
puts sample2.inspect_detailed

puts