• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Code of SMS application

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Servlet



import javax.comm.*;
import java.io.*;
import java.sql.Connection;
import java.util.Enumeration;
import java.util.Calendar;
import java.util.Date;
import java.util.TooManyListenersException;


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



import Student.NewBean;



public class SmSer extends HttpServlet {
Connection con;
boolean m1=true;
public void destroy() {
super.destroy();
}

private static final int DELAY_BETWEEN_CHARS = 20;


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

Student.NewBean bean=new Student.NewBean(con);
new Thread(bean).start();
//bean.getGot();
request.setAttribute("bean", bean);
RequestDispatcher rd=request.getRequestDispatcher("/Sms.jsp");
rd.forward(request,response);

}

public void init(ServletConfig config) throws ServletException {

try
{
ServletContext ctx=config.getServletContext();
con=(Connection)ctx.getAttribute("Connection");
if(con!=null)
{

}


}
catch(Exception e)
{
e.printStackTrace(); }
}
}



NewBean



package Student;

import javax.comm.*;
import java.io.*;
import java.sql.Connection;
import java.util.Enumeration;
import java.util.Calendar;
import java.util.Date;
import java.util.TooManyListenersException;


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



public class NewBean extends HttpServlet implements Runnable, SerialPortEventListener {
SerialPort portID;
Enumeration portList;
InputStream incoming;
InputStream inputStream;
OutputStream outgoing;
Thread readThread;
CommPortIdentifier portIdentifier;
InputStream keyInput;
StringBuffer command = new StringBuffer(200);
int c;
Connection con;
boolean b1;
String h1;
private static final int DELAY_BETWEEN_CHARS = 20;
static CommPortIdentifier portId;
//static Enumeration portList;

//InputStream inputStream;
SerialPort serialPort;
//Thread readThread;



public NewBean(Connection con) {

this.con=con;


portList = CommPortIdentifier.getPortIdentifiers();

while ( portList.hasMoreElements()) {
portIdentifier = (CommPortIdentifier)portList.nextElement();
System.out.println(portIdentifier.toString());
System.out.println(portIdentifier.getName());
}

try {
portIdentifier = CommPortIdentifier.getPortIdentifier("COM1");
portID = (SerialPort)portIdentifier.open("Serial",10);

// here we can get the input & output stream of this serial port
// we can put the AT command into this stream
incoming = portID.getInputStream();
outgoing = portID.getOutputStream();
portID.addEventListener(this);
portID.notifyOnDataAvailable(true);
try {
portID.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
}catch(UnsupportedCommOperationException ucoe) {System.out.println("unsupported serial setting command");}

} catch( NoSuchPortException pe ) { System.out.println("No such port existing");
} catch(PortInUseException piue ) { System.out.println("Sorry, this port is in use now");
} catch(IOException ioe) {
System.out.println("maybe we can't get the input/output of serial port");
}catch(TooManyListenersException tmel) {}
}

private void sendStr(String s) throws Exception
{
for (int i = 0; i < s.length(); i ++)
{
try { Thread.sleep(DELAY_BETWEEN_CHARS); } catch (Exception e) {}
outgoing.write((byte) s.charAt(i));
outgoing.flush();
}
}
private void sendChar(char s) throws Exception
{
//for (int i = 0; i < s.length(); i ++)
//{
try { Thread.sleep(DELAY_BETWEEN_CHARS); } catch (Exception e) {}
outgoing.write((byte) s);
outgoing.flush();
//}
}

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:
try {
readIncoming();
}catch(Exception ioe) {};
break;
} // end of switch(event.getEventType())
}

private void readIncoming() throws Exception {
int c;
StringBuffer sb = new StringBuffer(200);

try {
while (true ) {
c = incoming.read();
if ( c == -1 ) {
break;
}
sb.append((char)c);
}
while ( sb.charAt(0) == 13 || sb.charAt(0) == 10 ) sb.delete(0,1);
System.out.println("-------------- here is the input stream ---------");
System.out.println(sb.toString());
System.out.println("-------------- here is the input stream ---------");
} catch (IOException e) {
System.out.println("we can't read from input");
}

}
public void setGot(String h)
{
this.h1=h;

}
public String getGot()
{
return h1;


}

public void run() {
try {
if (true) {

sendStr("AT+CMGS=my number");
try{

serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
} catch (PortInUseException e) {}
inputStream = portID.getInputStream();
System.out.println(inputStream);
if(inputStream.equals("<CR><LF>"))
{
System.out.println("here in cr lf");
}
//sendStr(+CMGS=9397624332");
sendChar((char)0x0d);
Thread.sleep(1000);
sendStr("This Test SMS is sent from CBLOOMS Infotech Services(SMS application) ");
Thread.sleep(10);
sendChar((char)0x1a);
setGot("Message sent Successfully");

/*while( true ) {
c = System.in.read();
if ( c == 0x0d || c == 0x0a ) {
break;
}
command.append((char)c);
}*/

//if ( command.toString().equalsIgnoreCase("quit") )
//break;

//System.out.println("try to send "+command.toString());

//command.delete(0,command.length());

}

}catch(Exception e) {

setGot("Message unsent Successfully");
System.out.println("we failed at SENDing stage");


}

portID.close();
System.out.println("port has been closed");
}

}


I am unable get response from modem when it off(power off)..How to check it
& i am unable to forward to jsp
In jsp it should print Message sent successfully ,and i need to get acknowledgement.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic