Notices


Reply
Thread Tools
Posts: 451 | Thanked: 334 times | Joined on Sep 2009
#51
OK:
1. Using http://talk.maemo.org/showpost.php?p...5&postcount=20 as inspiration, I see how to send a number of AT commands one by one to pnatd via python.
2. The references http://www.control.com.sg/at_commands_sms.aspx http://www.smssolutions.net/tutorials/gsm/sendsmsat/ and http://www.activexperts.com/xmstoolk...ommands/nokia/ show the necessary AT commands for SMS sending.

As I'm not familiar with python, can someone help me with the following:

To send the SMS number and text, you need to input AT+CMGS="+yyyyy", hit Enter then enter the message text and hit Ctrl-Z, maybe twice. How do I modify the above python code to send Ctrl-z?
 
Posts: 451 | Thanked: 334 times | Joined on Sep 2009
#52
Got it:

to send an SMS from the commandline with Python:

Code:
#!/usr/bin/env python2.5

import pexpect
import time
from subprocess import *

child = pexpect.spawn('pnatd');
child.send('at\r');
time.sleep(0.25);
child.send('at+cmgf=1\r');
time.sleep(0.25);
child.send('at+cmgs="+XXXXXXX"\r');
child.send('SMSTEXTSMSTEXTSMSTEXT');
child.send(chr(26));
child.send(chr(26));
child.sendeof();
Working on an Emacs way now, it should be easy. Complications only for converting UTF-8 text to hex etc. but will get it done...
 

The Following User Says Thank You to 白い熊 For This Useful Post:
Posts: 254 | Thanked: 122 times | Joined on Nov 2009
#53
Timers are not good. You'd better read replies from modem and proceed, when you get confirmation, that it is ready for the next command.
https://garage.maemo.org/plugins/scm...et&view=markup
 

The Following User Says Thank You to KiberGus For This Useful Post:
Posts: 451 | Thanked: 334 times | Joined on Sep 2009
#54
I'll take a look into it.

Anyhow, here's my next question, the above way only sends SMSes up to 160 chars, i.e. if the SMS is longer, sending it in one AT command doesn't split it into multiples and trasmit. The SMS is not transmitted.

Obviously one could break it up manually and send successively, but that way, it's not gonna be automatically merged on the other end.

Anyone know how to enable long SMS sending this way via AT commands?
 
Posts: 451 | Thanked: 334 times | Joined on Sep 2009
#55
I need some help with the Unicode SMS sending.

As per http://www.smssolutions.net/tutorials/gsm/sendsmsat/ I can't send

Code:
AT+CSMP=1,167,0,8
to specify the data coding scheme for Unicode messages.

In fact querying the N900 with
Code:
AT+CSMP=?
only gives OK as the answer, but the above command results in an error.

How can I set the coding for Unicode to successfully send a Unicode SMS?
 
hawaii's Avatar
Posts: 1,030 | Thanked: 792 times | Joined on Jun 2009
#56
Has anybody found a way to send CLASS 0 SMS messages?
 
Posts: 840 | Thanked: 823 times | Joined on Nov 2009
#57
I've tried to sow the functions of both pende and shaunramos' scripst into one with command line arguments and option processing

Code:
#!/usr/bin/env python2.5          
import sched, time                
import dbus                       
import gobject                    
from dbus.mainloop.glib import DBusGMainLoop

def octify(str):
        '''     
        Returns a list of octet bytes representing
        each char of the input str.               
        '''                                       

        bytes = map(ord, str)
        bitsconsumed = 0     
        referencebit = 7     
        octets = []          

        while len(bytes):
                byte = bytes.pop(0)
                byte = byte >> bitsconsumed
                                           
                try:                       
                        nextbyte = bytes[0]
                        bitstocopy = (nextbyte & (0xff >> referencebit)) << referencebit
                        octet = (byte | bitstocopy)                                     

                except:
                        octet = (byte | 0x00)

                if bitsconsumed != 7:
                        octets.append(byte | bitstocopy)
                        bitsconsumed += 1               
                        referencebit -= 1               
                else:                                   
                        bitsconsumed = 0                
                        referencebit = 7                

        return octets

def semi_octify(str):
        '''          
        Expects a string containing two digits.
        Returns an octet -                     
        first nibble in the octect is the first
        digit and the second nibble represents 
        the second digit.                      
        '''                                    
        try:                                   
                digit_1 = int(str[0])          
                digit_2 = int(str[1])          
                octet = (digit_2 << 4) | digit_1
        except:                                 
                octet = (1 << 4) | digit_1      

        return octet


def deoctify(arr):

        referencebit = 1
        doctect = []    
        bnext = 0x00    

        for i in arr:

                bcurr = ((i & (0xff >> referencebit)) << referencebit) >> 1
                bcurr = bcurr | bnext                                      

                if referencebit != 7:
                        doctect.append( bcurr )
                        bnext = (i & (0xff << (8 - referencebit)) ) >> 8 - referencebit
                        referencebit += 1                                              
                else:                                                                  
                        doctect.append( bcurr )                                        
                        bnext = (i & (0xff << (8 - referencebit)) ) >> 8 - referencebit
                        doctect.append( bnext )                                        
                        bnext = 0x00                                                   
                        referencebit = 1                                               

        return ''.join([chr(i) for i in doctect])


def createPDUmessage(number, msg):
        '''                       
        Returns a list of bytes to represent a valid PDU message
        '''                                                     
        numlength = len(number)                                 
        if (numlength % 2) == 0:                                
                rangelength = numlength                         
        else:                                                   
                number = number + 'F'                           
                rangelength = len(number)                       

        octifiednumber = [ semi_octify(number[i:i+2]) for i in range(0,rangelength,2) ]
        octifiedmsg = octify(msg)                                                      
        HEADER = 1                                                                     
        FIRSTOCTETOFSMSDELIVERMSG = 10                                                 
        ADDR_TYPE = 129 #unknown format                                                
        number_length = len(number)                                                    
        msg_length = len(msg)                                                          
        pdu_message = [HEADER, FIRSTOCTETOFSMSDELIVERMSG, number_length, ADDR_TYPE]    
        pdu_message.extend(octifiednumber)                                             
        pdu_message.append(0)                                                          
        pdu_message.append(0)                                                          
        pdu_message.append(msg_length)                                                 
        pdu_message.extend(octifiedmsg)                                                
        return pdu_message                                                             


def sendmessage(number, message):

        bus = dbus.SystemBus()
        smsobject = bus.get_object('com.nokia.phone.SMS', '/com/nokia/phone/SMS/ba212ae1')
        smsiface = dbus.Interface(smsobject, 'com.nokia.csd.SMS.Outgoing')
        arr = dbus.Array(createPDUmessage(number.replace('+', '00'), message))

        msg = dbus.Array([arr])
        smsiface.Send(msg,'')


def callback(pdumsg, msgcenter, somestring, sendernumber):

        msglength = int(pdumsg[18])
        msgarray = pdumsg[19:len(pdumsg)]

        msg = deoctify(msgarray)

        if msg > 0:
               print 'New message received from %s' % sendernumber
               print 'Message length %d' % msglength
               print 'Message: %s' % msg

               if msg == "ping":
                       print "Sending reply: pong"
                       sendmessage(sendernumber.replace("+","00"), "pong")
               else:
                       print "Unknown command"


def listen():
        DBusGMainLoop(set_as_default=True)
        bus = dbus.SystemBus() #should connect to system bus instead of session because the former is where the incoming signals come from
        bus.add_signal_receiver(callback, path='/com/nokia/phone/SMS', dbus_interface='Phone.SMS', signal_name='IncomingSegment')
        gobject.MainLoop().run()


if __name__ == '__main__':
  import time
  
  def schedule_task(schedule, fn, *args):
      import sched
      s = sched.scheduler(time.time, time.sleep)
      startTime = time.mktime(time.strptime(schedule, '%b %d %H:%M %Y'))
      s.enterabs(startTime, 0, fn, args)
      s.run()

  import getopt, sys
  try:  
    opts, args = getopt.getopt(sys.argv[1:],"hlt:", ["help","listen","time="])

  except getopt.GetoptError, err:
    # print help information and exit:
    print str(err) # will print something like "option -a not recognized"
    usage()
    sys.exit(2)  
  listening = False 
  timeofday = ''
  for opt, arg in opts:
    if opt in ("-h", "--help"):
      usage()                     
      sys.exit()                  
    elif opt in ("-l", "--listen"):
      listening = True                 
    elif opt in ("-t", "--time"):
      timeofday = arg                           
    else:
      assert False, "unhandled option"
  
  number = args[0]           
  msg = args[1]
  if msg != '':
    if timeofday == '':
        sendmessage(number, msg)
    else:
        today = time.strftime('%b %d x %Y', time.localtime())
        schedule = today.replace('x', timeofday)
        schedule_task(schedule, sendmessage, number, msg)
  if listening:
    listen()
here it is if anybody is intersted. I need to create a usage help function but don't know the standard notation for help. anybody know?

so the options are currently:
to schedule a text
[-t time] or [--time=time]
to listen and reply to ping SMS
[-l] or [--listen]

the arguments are given like
./scriptname.py number message
I'd be greatful if somebody more familiar with python could go over the script too.

Last edited by Cue; 2010-03-16 at 04:21.
 

The Following 3 Users Say Thank You to Cue For This Useful Post:
x61's Avatar
Posts: 932 | Thanked: 278 times | Joined on Sep 2009 @ Kentucky
#58
You go through all this python code simply to send a text message? You folks have time.
 
Posts: 840 | Thanked: 823 times | Joined on Nov 2009
#59
Originally Posted by x61 View Post
You go through all this python code simply to send a text message? You folks have time.
lol, yeah; but now thanks to pende and shaunramos I can send a text at any time and even if I forget my phone at home.
 
qole's Avatar
Moderator | Posts: 7,109 | Thanked: 8,820 times | Joined on Oct 2007 @ Vancouver, BC, Canada
#60
x61: they're going to all this trouble so they can script SMS messages. This means, like Cue said, you can send SMS messages remotely, or send a message at a scheduled time (or place, if you tie into the GPS), or you could even write an auto-reply script, ("I'm away from the phone at the moment!").

The dark side of all of this is that a trojan could use this code to send secret text messages without the owner knowing about it.
__________________
qole.org --- twitter --- Easy Debian wiki page
Please don't send me a private message, post to the appropriate thread.
Thank you all for your donations!
 
Reply


 
Forum Jump


All times are GMT. The time now is 19:40.