class SimCard::Sim

the initial idea is borrowed from here: www.dzone.com/snippets/send-and-receive-sms-text

Public Class Methods

new(user_options = {}) click to toggle source

connect to SIM card. user_options may contain:

  • port : device where the gsm modem is connected, default is ‘/dev/ttyUSB0’

  • speed : connection speed in bauds, default is 9600

  • pin : pin code to your sim card. not required if your sim does not require authorization via pin

  • sms_center_no : SMS center of you provider. required only if you want to send SMS messages

  • debug_mode: when set to true, will print all communication with SIM card to stdout

# File lib/sim_card/sim.rb, line 14
def initialize(user_options = {})
  default_options = {:port => '/dev/ttyUSB0', :speed => 9600}
  options = default_options.merge user_options
  
  @port = SerialPort.new options[:port], options[:speed]
  
  debug_mode = (options[:debug_mode] == true)
  @at_interface = RealAtInterface.new @port, debug_mode
  
  initial_check
  authorize options[:pin]
  # Set to text mode
  @at_interface.send "AT+CMGF=1"
  # Set SMSC number
  @at_interface.send "AT+CSCA=\"#{options[:sms_center_no]}\"" if options[:sms_center_no]
end

Public Instance Methods

close() click to toggle source

correctly disconnect from SIM card

# File lib/sim_card/sim.rb, line 32
def close
  @port.close
end
delete_sms_message(sms_message) click to toggle source

remove SMS message from SIM card memory

  • sms_message: instance of SimCard::SmsMessage to be deleted

# File lib/sim_card/sim.rb, line 56
def delete_sms_message sms_message
  @at_interface.send "AT+CMGD=#{sms_message.message_id}"
end
phonebook() click to toggle source

return instance of Phonebook

# File lib/sim_card/sim.rb, line 50
def phonebook
  Phonebook.new @at_interface
end
send_raw_at_command(cmd) click to toggle source

for hackers

# File lib/sim_card/sim.rb, line 67
def send_raw_at_command cmd
  @at_interface.send cmd
end
send_sms(number, message_text) click to toggle source

send SMS message

# File lib/sim_card/sim.rb, line 37
def send_sms number, message_text
  @at_interface.send "AT+CMGS=\"#{number}\""
  @at_interface.send "#{message_text[0..140]}#{26.chr}\r\r"
  sleep 3
  @at_interface.send "AT"
end
signal_strength() click to toggle source

signal strengh in dBm. -60 is almost perfect signal, -112 is very poor (call dropping bad)

# File lib/sim_card/sim.rb, line 61
def signal_strength
  sq = SimCard::SignalQuality.new @at_interface
  return sq.signal_strength
end
sms_messages() click to toggle source

list SMS messages in SIM memory

# File lib/sim_card/sim.rb, line 45
def sms_messages
  ReceivedSmsMessage.load_messages @at_interface
end

Private Instance Methods

authorize(pin) click to toggle source
# File lib/sim_card/sim.rb, line 82
def authorize pin
  auth_status = @at_interface.send "AT+CPIN?"
  if auth_status.include?('READY')
    # no pin required or already authorized
    return true
  elsif auth_status.include?('SIM PIN')
    response = @at_interface.send "AT+CPIN=\"#{pin}\""
    if response.include?('OK')
      return true
    else
      raise Error.new("SIM authorization failed: #{response.inspect}")
    end
  else
    raise Error.new("unknown SIM authorization status: #{auth_status.inspect}")
  end
end
initial_check() click to toggle source
# File lib/sim_card/sim.rb, line 73
def initial_check
  response = @at_interface.send "AT"
  if response.include?("OK")
    return true
  else
    raise Error.new("SIM is not responsing properly to initial handshake. response: #{response.inspect}")
  end
end