Java Reference
In-Depth Information
reply
#
sendCmd("AT
!
CMGS
#
\""
!
dest
!
"\"",200);
//System.out.print(reply);
if
(reply.endsWith("\r\n> ")){
reply
#
sendCmdNoCR(msg
!
(
char
)27,5000);
System.out.print(reply);
}
reply
#
sendCmd("AT
!
CGMI");
System.out.print(reply);
}
catch
(Exception e) {
reply
#
e.getMessage();
e.printStackTrace();
}
return
reply;
}
// Send a command to the modem with the predefined timeout
String sendCmd(String cmd){
return
sendCmd(cmd,2000); }
// Send a command to the modem
String sendCmd(String cmd,
long
timeout){
return
sendCmdNoCR(cmd
!
"\r",timeout);}
// Send a command to the modem, and wait for the given
// timeout.
synchronized String sendCmdNoCR(String cmd,
long
timeout){
String reply
#
"";
try
{
readAnswer
#
true
;
outputStream.write((cmd
!
"\r").getBytes());
this.wait(timeout);
reply
#
answer.toString();
if
(readAnswer){ // i.e. timed out
readAnswer
#
false
;
}
answer
#
new
StringBuffer();
}
catch
(Exception e){}
return
reply;
}
private
StringBuffer answer
#
new
StringBuffer();
private boolean
readAnswer
#
false
;
// Receive events from the serial port
public synchronized void
serialEvent
(SerialPortEvent event) {
switch
(event.getEventType()) {
case
SerialPortEvent.DATA_AVAILABLE:
try
{
char
ch
#
0;
// read the available data