JavaMix

it`s all about JavaPrograming

sending sms using serialport communication API

before going to execute this program read how to install comport api for windows

/*  this is the program used to send the message from javaprogram to the mobile phone */

/*

Note :- before using this porogram  first read the post “serialport communication using Java” by me in this blog.

*/

package sms; // it is my own package
import java.io.*;
import java.util.*;
import javax.comm.*;          //for accessing serialport

/**
*
* @author  katta vijay
*
*
*/
public class OwnPort {
static CommPortIdentifier portId;
static CommPortIdentifier saveportId;
static Enumeration        portList;
static  SerialPort           serialPort;
static OutputStream      outputStream;
static InputStream      inputStream;
static boolean        outputBufferEmptyFlag = false;
public static void main(String[] args) {
boolean           portFound = false;
String           defaultPort= “COM1″;
System.out.println(“Set default port to “+defaultPort);
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(defaultPort)) {
System.out.println(“Found port: “+defaultPort);
portFound = true;
// init reader thread
OwnPort op=new OwnPort();
op.initwritetoport();
}
}

}
if (!portFound) {
System.out.println(“port ” + defaultPort + ” not found.”);
}
}
public void initwritetoport()
{
System.out.println(“inside  initwriteport”);
String   s1=”at”;
String   s2=”\r\n”;
String   s3=”at+cmgf=1″;
String   s4=”at+cmgs=\”9966606454\”";
//         char[]   s5={‘”‘,’9′,’3′,’4′,’6′,’4′,’6′,’2′,’1′,’6′,’5′,’”‘};
String s6=”32“;
//         for(int i=0;i<s5.length;i++)
//         {
//          System.out.println(s5.toString());
//         }
//       System.out.println(“s6=”+s6);
String        messageString = “hey ee mesage GSM nunchi vachindi!”;

// get the outputstream
try {
serialPort = (SerialPort) portId.open(“SimpleReadApp”, 2000);
} catch (PortInUseException e) {}

try {
// set port parameters
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
outputStream =serialPort.getOutputStream();
inputStream = serialPort.getInputStream();
}
catch (Exception e) {
e.printStackTrace();
}
try
{
System.out.println(“–1–”);
outputStream.write(s1.getBytes()); // AT command
//           System.out.println(s1.getBytes());
//           outputStream.wait(10000);
outputStream.write(s2.getBytes()); // enter
Thread.sleep(1000);  //sleeping thread
System.out.println(“–2–”);
//                outputStream.wait(1000);
outputStream.write(s3.getBytes());  //at+cmgf=1 command
//                outputStream.wait(1000);
outputStream.write(s2.getBytes()); //enter
Thread.sleep(1000);// thread sleeping
System.out.println(“–3–”);
//                outputStream.wait(1000);
outputStream.write(s4.getBytes()); //at+cmgs=”<mobilenumber>”
//                outputStream.wait(1000);
outputStream.write(s2.getBytes()); //enter
Thread.sleep(1000); //thread sleeping
System.out.println(“–4–”);
//                outputStream.wait(1000);
outputStream.write(messageString.getBytes()); // message
//                outputStream.wait(1000);
outputStream.write(s2.getBytes());
Thread.sleep(1000);
outputStream.write(s6.getBytes());
Thread.sleep(1000);
System.out.println(“–5–”);

//                outputStream.wait(1000);
byte[] readBuffer = new byte[23];
try
{
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
//               System.out.println(numBytes);

// print data

String result  = new String(readBuffer);
System.out.println(“Read: “+result);
}
}
catch(Exception e)
{
e.printStackTrace();
}
outputStream.close();
serialPort.close();
}
catch(Exception e)
{

}

}
}

Written by katta vijay

January 21, 2009 at 5:50 pm

5 Responses

Subscribe to comments with RSS.

  1. hai vijay
    i am doing the same project ,sending sms through gsm modem… my code is
    import java.io.*;
    import java.util.*;
    import javax.comm.*;
    import java.io.IOException;
    import java.util.Properties;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.DataInputStream;
    import java.io.FileInputStream;
    import java.io.DataOutputStream;
    import java.io.FileOutputStream;

    public class ReadSimple implements Runnable, SerialPortEventListener {
    static CommPortIdentifier portId;
    static Enumeration portList;
    OutputStream outputstream;
    InputStream inputStream;
    SerialPort serialPort;
    Thread readThread;
    public static void main(String[] args) {
    portList = CommPortIdentifier.getPortIdentifiers();

    while (portList.hasMoreElements()) {
    portId = (CommPortIdentifier) portList.nextElement();
    if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
    if (portId.getName().equals(“COM1″)) {
    System.out.println(“Found port:COM1 “);
    ReadSimple reader = new ReadSimple();
    }
    }
    }
    }
    public ReadSimple() {
    try {
    serialPort = (SerialPort) portId.open(“ReadSimpleApp”,500);

    } catch (PortInUseException e) {
    System.out.println(e);
    }
    try {
    inputStream = serialPort.getInputStream();
    OutputStream out=serialPort.getOutputStream();

    String line=”";
    line=”AT”+”r\n”;
    out.write(line.trim().getBytes());

    line=”";
    line=”AT+CMGS=9965844325″+”\r\n”;
    out.write(line.trim().getBytes());
    System.out.print(line);
    line=”helloworld”;
    //line=”ATD 996544325;”+”\r\n”;
    out.write(line.trim().getBytes());
    } catch (IOException e) {
    serialPort.close();
    System.out.println(e);
    }
    // catch(InterruptedException E){E.printStackTrace();}
    try {
    serialPort.addEventListener(this);
    } catch (TooManyListenersException e) {System.out.println(e);}
    serialPort.notifyOnDataAvailable(true);
    try {
    serialPort.setSerialPortParams(9600,
    SerialPort.DATABITS_8,
    SerialPort.STOPBITS_1,
    SerialPort.PARITY_NONE);
    } catch (UnsupportedCommOperationException e) {System.out.println(e);}
    readThread = new Thread(this);
    readThread.start();
    }

    public void run() {
    try {
    Thread.sleep(200);
    } catch (InterruptedException e) {System.out.println(e);}
    }

    public void serialEvent(SerialPortEvent event) {
    switch(event.getEventType()) {
    case SerialPortEvent.BI:
    case SerialPortEvent.OE:
    case SerialPortEvent.FE:
    case SerialPortEvent.PE:
    case SerialPortEvent.CD:
    case SerialPortEvent.CTS:
    case SerialPortEvent.DSR:
    case SerialPortEvent.RI:
    case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
    break;
    case SerialPortEvent.DATA_AVAILABLE:
    byte[] readBuffer = new byte[10];

    try {
    while (inputStream.available() > 0) {
    int numBytes = inputStream.read(readBuffer);
    }
    System.out.println(new String(readBuffer));

    } catch (IOException e) {System.out.println(e);}
    break;
    }
    }
    }

    in the above code it shows no error while running its no message is delivered to the mobile , i saw the thread http://javamix.wordpress.com/2009/01/21/sending-sms-using-serialport-communication-api/ …..can u send the complete code to send sms or can u alter the my code to send sms sucessfully , its very urgent for me ,thank god to ur post.

    arunram

    October 29, 2009 at 11:29 am

  2. hai vijay
    more over while saving your code it shows a error “some character could not be mapped usingcp1252 character encoding either change the encoding or remove the code which are not supported by cph1252″ so i i changed the code
    char[] s5={‘”‘,’9′,’3′,’4′,’6′,’4′,’6′,’2′,’1′,’6′,’5′,’”‘}
    }; TO

    private static char[] hexDigits = { ’0′, ’1′, ’2′, ’3′, ’4′, ’5′, ’6′, ’7′,
    ’8′, ’9′, ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’
    }; now i can able to save , similarly can you tell me the what is the necessary of the s6=”32″,whether i have to change this value finally i gave string values

    String s1=”AT”;
    String s2=”\r\n”;
    String s3=”AT+CMGF=1″;
    String s4=”AT+CMGS=\”9965844325\”";
    char[] hexDigits = { ’0′, ’1′, ’2′, ’3′, ’4′, ’5′, ’6′, ’7′,
    ’8′, ’9′, ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’
    };
    String s6=”32″;

    while running the program console the output like this

    Set default port to COM1
    Found port: COM1
    inside initwriteport
    –1–
    –2–
    –3–
    –4–
    –5–
    Read: AT

    OK
    AT+CMGF=1

    OK
    Read:
    AT+CMGS=”9965844325″

    Read:
    > first sms send by a
    Read: run ram!
    > 32send by a
    but message is not delivered to my number 9965844325…and also you have included the package package sms; whether i need to execute this code …i am expecting a quick reply from mail id is (“arunram.prf@gmail.com”)

    arunram

    October 29, 2009 at 1:18 pm

  3. hai

    i want to check the status of one field continously in my database if the status 1 , i want to send sms to that number if 0 means i wont send sms.for checking continuously serial communication should be enabled continuously,how to make serial communication available continuously to check my database status field to perform certian function

    arunram

    December 2, 2009 at 3:33 pm

  4. Hi,
    Nice to see a Complete program. But A Small Problem. I connected my Nokia 5230. then it reported as COM5. but no message is delivered. No Message in the out box too. Can you help about this issue or suggest me a handset/ data card which works successfully.

    Venkat Sangu

    November 26, 2010 at 4:52 pm

  5. [...] sending sms using serialport communication API « Learn Java Programming [...]


Comments are closed.

Follow

Get every new post delivered to your Inbox.