wood burning stoves 2.0*
The moose likes Java in General and the fly likes send SMS by using Serial Port Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic
Author

send SMS by using Serial Port

yousef atya
Greenhorn

Joined: May 29, 2009
Posts: 3
hi ......, Iam trying to develop a java application that use to pass At Commands to my mobile through using Java Communication API(serial port), in any way this application should send sms message by using AT Commands , this my program:

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

public class SimpleRead implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;

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("COM6")) {
// if (portId.getName().equals("/dev/term/a")) {
SimpleRead reader = new SimpleRead();
}
}
}
}

public SimpleRead() {
try {
serialPort = (SerialPort) portId.open("SimpleReadApp",0);
} 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+CPIN?"+"\r\n";
out.write(line.trim().getBytes());

line="";
line="ATD 0599281991;"+"\r\n";
out.write(line.trim().getBytes());
} catch (IOException e) {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;
}
}
}

there is no error in this code , but nothing happen in the mobile , what I mean when I send "ATD anyphonenumber;" nothing happen
actually this command should call any phonenumber.

can any one tell me how I can solve this problem, please help me.


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

Please Use One Thread Per Question. Let's continue here.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: send SMS by using Serial Port
 
Similar Threads
Send SMS by using Serial Port
help using javax.comm api
unable to read the data from serial port in rfid
how can I send the output of a servlet to a jsp page?
sending sms through gsm modem