Thursday, September 7, 2006

What is SMS?

SMS is gaining mass popularity and billions of messages are exchanged across the world. Though typing may be cumbersome and delivery is not gauranteed, SMS popularity has encouraged development of MMS.

I'm more interested in the actual communication itself so you can try out a Java implentation I have written but first some basic understading may be helpful. Basic stuff is from the GSM specifications by ETSI (3.40 and 3.38) and the AT command set.

The basic process for sending a SMS (MO) is to first create the message in text mode or as a PDU (Protocol Description Unit), then send it over to a SMSC (SMS Center). The SMSC which is usually operated by a telco then delivers the message via appropriate means to the destination (MT). Sounds simple and even practically its no rocket science, just the need for proper infrastructure in place. Communications between different telcos is sometimes tricky since they use competing and often propriety protocols such as OIS (Sema), SMPP (Logica), CIMD (Nokia), UCP (CMG). However not bothering at the SMSC level end users are more interested in sending and recieving SMS.

Sending/Recieving a SMS: There can be many encoding sets for the characters you use that sets the limit for the length of your SMS. Using the GSM 7-bit encoding it is 160 characters and may be 60 chars if a 16-bit encoding is used (say for typing chinese characters). Encoding is set using the AT+CSCS command. Using the HEX encoding all characters can be easily represented. Next a SMSC number is required where the modem must forward your sms, this is usually in the SIM card already, otherwise can be set using AT+CSCA. Next it must contain the destination address (DA). Additional fields include origin address (OA), timestamp, status request and a validity period.

The message mode can be set using AT+CMGF. The PDU is quite complex, for a good description see http://emctone.tripod.com/html/smspdu.html

The message can then be sent using AT+CMGS.

Reading a message is also very simple and can be done using AT+CMGL to list all messages or AT+CMGR for a specific one. Next deleting a message is done using AT+CMGD.

AT Help: If you are not familiar with the AT command syntax, search some tutorial. Use hyper terminal that comes with Windows. For example if you use AT+CMGL=? a list of valid options shows. Generally AT+=? shows help for that command.

Try GSMS - a simple Java package for communicating using SMS with your GSM modem/phone!

Friday, September 1, 2006

GSM + SMS = GSMS

First published 2003 sometime!

GSMS is a very simple attempt to send and recieve SMS via a GSM modem. Generally it should also work correctly with a GPRS modem. You can use:

  • A modem as those from Wavecom
  • You mobile phone over infra-red (iR)
  • Your mobile phone connected to your PC by a cable


Generally all newer phones support their use as a GSM Modem so check with your phone manual. In all cases you need to know the Serial Port you connect your device to, such as COM2,COM3 etc and then connect to it.

To use this API you need the Java Comm API that you can download from java.sun.com. Follow CLOSELY the instructions for installing the comm API otherwise it wont work. Look for the jdk1.2.html         file for installing help when using J2SE1.2 and later.

A code snippet to help you get started. Download here.


import java.io.*;
import java.util.*;
import javax.comm.*;

//import packages
....

public static void main(String[] args){
    GSMModem myModem;
    try{
        //SEND
        //connect
        myModem=new GSMModem("COM2",CONST.CONNECT_BAUD_9600);
        //init
        myModem.init();
        //see wat modem i have
        System.out.println(myModem.specs());
        //send sms
        myModem.setSMSMode(1);//send as text
        myModem.flushInput();//clear reply in input stream
        System.out.print("Sending...");
        myModem.sendSMS("99999999","sending as a text");
        System.out.println(myModem.reply());

        //close connection
        myModem.disconnect();

        //RECV 
        Thread.sleep(5000);//just to relax sim card for a while =)no need if using independently
        myModem=new GSMModem("COM2",CONST.CONNECT_BAUD_9600);
        myModem.init();
        //show sms on sim
        LinkedList ll=myModem.showAllSMS();
        if(ll.size()==0) System.out.println("No msgs");
        for(int i=0;i<ll.size();i++)
           System.out.println(ll.get(i));

         //how to delete<br>
         //myModem.deleteSMS(((SMSText)ll.get(0)).getIndex());
         //close connection
         myModem.disconnect();
    }catch(Exception e) {
         System.out.println(e); 
    }
}

You can generate JavaDocs using the source or view online.


Whats still missing/bugs:
  • PDU Format: Cant handle PDU format, uses text only (AT+CMGF=1)
  • HEX:Some bug in converting GSM 7 Bit to ASCII - does not do correctly for the funny characters, pls send me a corrected version if you figure it out.
Licensed as GPL, I'd appreciate if you send to me any extensions you write using this package.