-3
I am developing a java application that sends sms via a modem using RXTX.
So far I have this code:
public static void main(String args[]) throws Exception{
BlockingQueue<String> queue = new LinkedBlockingQueue<String>();
Escreve escreve = new Escreve(queue);
escreve.out("AT\r\n");
String saida = queue.take().toString();
System.out.println("OUTPUT: " + saida);
if(saida.contains("OK")) {
//AT+CMGF=1\n
escreve.out("AT+CMGF=1\r\n");
saida = queue.take().toString();
System.out.println("OUTPUT: " + saida);
if(saida.contains("OK")) {
//"AT+CMGS=\"+"+numero+"\"\n"
String numero = "xxxxxxxxx";
escreve.out("AT+CMGS=\"+"+numero+"\"\r\n");
saida = queue.take().toString();
System.out.println("OUTPUT: " + saida);
if(saida.contains(">")) {
//"Teste de envio de mensagem"+(char)26
escreve.out("Teste de envio de mensagem\u001a");
saida = queue.take().toString();
System.out.println("OUTPUT: " + saida);
}else {
System.out.println("Erro");
}
}else {
System.out.println("Erro");
}
}else {
System.out.println("Erro");
}
//AT+CMGS="+PPAAxxxxxxxx",145\r\n
//<mensagem>\u001a
}
class Escreve{
private final BlockingQueue<String> queue;
public Escreve(BlockingQueue<String> queue){this.queue = queue;}
public void out(String command) throws Exception{
CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier("COM9");
SerialPort serialPort = (SerialPort) portId.open(this.getClass().getName(), 1000);
serialPort.notifyOnDataAvailable(true);
serialPort.setSerialPortParams(460800,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
serialPort.addEventListener(new Listener(serialPort,queue));
OutputStream outputStream = serialPort.getOutputStream();
outputStream.write(command.getBytes());
System.out.println("Já enviou!");
Thread.sleep(2000);
outputStream.close();
serialPort.close();
}
class Listener extends Thread implements SerialPortEventListener{
private final SerialPort serialPort;
private final BlockingQueue<String> queue;
public Listener(SerialPort serialPort, BlockingQueue<String> queue){
this.serialPort = serialPort;
this.queue = queue;
}
public void serialEvent(SerialPortEvent event) {
try{
InputStream inputStream = serialPort.getInputStream();
switch (event.getEventType()) {
case SerialPortEvent.BI:
System.out.println("1");
break;
case SerialPortEvent.OE:
System.out.println("2");
break;
case SerialPortEvent.FE:
System.out.println("3");
break;
case SerialPortEvent.PE:
System.out.println("4");
break;
case SerialPortEvent.CD:
System.out.println("5");
break;
case SerialPortEvent.CTS:
System.out.println("6");
break;
case SerialPortEvent.DSR:
System.out.println("7");
break;
case SerialPortEvent.RI:
System.out.println("8");
break;
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
System.out.println("9");
queue.put("NADA");
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[100];
try {
while (inputStream.available() > 0) {
inputStream.read(readBuffer);
queue.put(new String(readBuffer));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}catch(Exception ex){
ex.printStackTrace();
}
}
What’s the matter?
It runs the whole program and sends the message (at least there is no error). However, the message never reaches the number we have entered.
After texting, he doesn’t return anything.
NOTE: The number I enter is without the country code (+351), if I enter country code, the modem returns error.
Some help?