i am running this and i am sending sms
public class PScanner implements SerialPortEventListener, Runnable {
CommPortIdentifier pid = null;
SerialPort sp;
BufferedReader input;
OutputStream output;
static char ch = '"';
static String dest = ch + "**********" + ch;
String line1 = "AT+CMGF=1\r\n";
String line2 = "AT+CMGS=" + dest + "\r\n";
String line3 = "hello" + "\r\n";
public PScanner() {
new Thread(this).start();
}
public void read() {
try {
Enumeration e = CommPortIdentifier.getPortIdentifiers();
while (e.hasMoreElements()) {
CommPortIdentifier cpi = (CommPortIdentifier) e.nextElement();
if (cpi.getName().equals("COM11")) {
pid = cpi;
break;
}
}
sp = (SerialPort) pid.open(getClass().getName(), 2000);
sp.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
InputStream is = sp.getInputStream();
input = new BufferedReader(new InputStreamReader(is));
output = sp.getOutputStream();
output.write(line1.getBytes());
output.write(line1.getBytes());
output.write(line2.getBytes());
output.write(line3.getBytes());
output.write(26);
output.flush();
sp.addEventListener(this);
sp.notifyOnDataAvailable(true);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public synchronized void serialEvent(SerialPortEvent oEvent) {
System.out.println("serialEvent CallBack");
try{
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE){
String line = "";
while ((line = input.readLine())!= null) {
System.out.println(line);
}
}
}catch(Exception ex){
ex.printStackTrace();
}
}
public synchronized void close() {
if (sp != null) {
System.out.println("not null");
sp.removeEventListener();
sp.close();
}
}
@Override
public void run() {
read();
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
Logger.getLogger(PScanner.class.getName()).log(Level.SEVERE, null, ex);
} finally {
System.out.println("done");
}
}
}