Class: SimCard::Sim

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

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(user_options = {}) ⇒ Sim

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



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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

Instance Method Details

#closeObject

correctly disconnect from SIM card



32
33
34
# File 'lib/sim_card/sim.rb', line 32

def close
  @port.close
end

#delete_sms_message(sms_message) ⇒ Object

remove SMS message from SIM card memory

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



56
57
58
# File 'lib/sim_card/sim.rb', line 56

def delete_sms_message sms_message
  @at_interface.send "AT+CMGD=#{sms_message.message_id}"
end

#phonebookObject

return instance of Phonebook



50
51
52
# File 'lib/sim_card/sim.rb', line 50

def phonebook
  Phonebook.new @at_interface
end

#send_raw_at_command(cmd) ⇒ Object

for hackers



67
68
69
# File 'lib/sim_card/sim.rb', line 67

def send_raw_at_command cmd
  @at_interface.send cmd
end

#send_sms(number, message_text) ⇒ Object

send SMS message



37
38
39
40
41
42
# 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_strengthObject

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



61
62
63
64
# File 'lib/sim_card/sim.rb', line 61

def signal_strength
  sq = SimCard::SignalQuality.new @at_interface
  return sq.signal_strength
end

#sms_messagesObject

list SMS messages in SIM memory



45
46
47
# File 'lib/sim_card/sim.rb', line 45

def sms_messages
  ReceivedSmsMessage.load_messages @at_interface
end