| | |

Sending SMS using serialport in Java

before going to execute this program read here  <a href="http://javamix.wordpress.com/2009/03/30/installing-comport-api-in-windows/">how to install comport api for windows</a>

/*  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="rn";
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)
{

}

}
}

Similar Posts

One Comment

  1. hi …

    plz can one help, how to receive the SMS on serial ports … its very important …. plzzzzz help me…

Leave a Reply

Your email address will not be published. Required fields are marked *