Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Send SMS by using Serial Port

 
yousef atya
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20552
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please Use Code Tags. You can edit your post to add them.
 
Ranganath Junpal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Please first check if your mobile is connected to which port and try to execute the commands using the hyperterminal or Tera term com or minicom console. If you are able to communicate with your mobile from the console then there is some problem with your program may be its not able to detect the proper port..

Do let know which os are you using..


-R
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic