Send sms with modem 3g

Asked

Viewed 407 times

-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?

1 answer

1


RESOLUTION

  • I use the RXTX library where we can download HERE and take out windows-x64 on the part of Downloads. Copy to project folder Rxcommtx.jar jar, rxtxParallel.dll and rxtxSerial.dll.
  • I use broadband with a SIM card.
  • Attention to the port where the modem is connected (Comx), in this case it is connected on the COM9 port. View in Device Manager (device manager) on the modem part.

CODE

public class ErsteSchritte {

private static CommPortIdentifier portId;
private static SerialPort serialPort;
private static OutputStream outputStream;

public static void main(String args[]) throws Exception{

    portId = CommPortIdentifier.getPortIdentifier("COM9");
    serialPort = (SerialPort) portId.open("Teste", 1000);
    serialPort.notifyOnDataAvailable(true);
    serialPort.setSerialPortParams(9600,
            SerialPort.DATABITS_8,
            SerialPort.STOPBITS_1,
            SerialPort.PARITY_NONE);
    outputStream = serialPort.getOutputStream();
    serialPort.addEventListener(new Listener(serialPort));


    //AT+CMGF=1\n
    String x1 = "AT+CMGF=1\r\n";
    outputStream.write(x1.getBytes());
    System.out.println("Enviou a primeira.");
    Thread.sleep(1000);


    //"AT+CMGS=\"+"+numero+"\"\n"
    String numero = "+351xxxxxxxxx";
    String x2 = "AT+CMGS=\""+numero+"\",145\r\n";
    outputStream.write(x2.getBytes());
    System.out.println("Envia a segunda.");
    Thread.sleep(1000);


    //"Teste de envio de mensagem"+(char)26
    String mensagem = "MENSAGEM A ENVIAR";
    String x3 = mensagem +"\u001a";
    outputStream.write(x3.getBytes());
    System.out.println("Envia a terceira.");

    Thread.sleep(20000);
    outputStream.close();
    serialPort.close();

    System.out.println("TERMINOU!");

}} 

class Listener extends Thread implements SerialPortEventListener{
private final SerialPort serialPort;

public Listener(SerialPort serialPort){
    this.serialPort = serialPort;
}

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");
            System.out.println("NADA");
            break;
        case SerialPortEvent.DATA_AVAILABLE:
            byte[] readBuffer = new byte[200];
            try {
                while (inputStream.available() > 0) {
                    inputStream.read(readBuffer);
                    System.out.println(new String(readBuffer));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }catch(Exception ex){
        ex.printStackTrace();
    }
}}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.