cayofontana
Posts: 5
Joined: Fri Jun 22, 2018 12:10 am
Location: Brasil

Send SMS with GSM GPRS A6 Module and Java

Sun Jul 15, 2018 5:48 pm

Hi there,

I work on academic project where, in some moment, the solution need send SMS for some clients.
Searching on web, I found the example below in Python and it works fine. See it:

Code: Select all

import serial
import RPi.GPIO as GPIO      
import os, time

GPIO.setmode(GPIO.BOARD)    

# Enable Serial Communication
port = serial.Serial("/dev/ttyS0", baudrate=9600, timeout=1)

# Transmitting AT Commands to the Modem
# '\r\n' indicates the Enter key

port.write('AT'+'\r\n')
rcv = port.read(10)
print rcv
time.sleep(1)

port.write('ATE0'+'\r\n')      # Disable the Echo
rcv = port.read(10)
print rcv
time.sleep(1)

port.write('AT+CMGF=1'+'\r\n')  # Select Message format as Text mode 
rcv = port.read(10)
print rcv
time.sleep(1)

# Sending a message to a particular Number

port.write('AT+CMGS="+5527999XXXXXX"'+'\r\n')
rcv = port.read(10)
print rcv
time.sleep(1)

port.write('Test'+'\r')  # Message
rcv = port.read(10)

But this academic project was implemented in Java. Then I created a Java code for send SMS but its not working. See it below:

Code: Select all

import java.io.IOException;
import java.io.OutputStream;

import java.util.Enumeration;
import java.util.List;
import java.util.ArrayList;

import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;

class ComunicacaoMovel
{
	private static final char enter = 13;
	private static final char ctrlz = 26;

	public ComunicacaoMovel()
	{
		
	}

	private CommPortIdentifier obterPortaCommSerial()
	{
		Enumeration portasComm = CommPortIdentifier.getPortIdentifiers();

		while (portasComm.hasMoreElements())
		{
			CommPortIdentifier portaComm = (CommPortIdentifier) portasComm.nextElement();

			if (portaComm.getPortType() == CommPortIdentifier.PORT_SERIAL && portaComm.getName().equals("/dev/ttyS0"))
				return (portaComm);
		}

		return (null);
	}

	public void enviarMensagemSMS(String numeroCelular, String mensagem)
	{
		CommPortIdentifier portaComm = obterPortaCommSerial();
		List<String> mensagensSIM = new ArrayList<String>();
		mensagensSIM.add("AT");
		mensagensSIM.add("ATE0");
		mensagensSIM.add("AT+CMGF=1");
		mensagensSIM.add("AT+CMGS=\"+" + numeroCelular + "\"");
		mensagensSIM.add(mensagem);

		try
		{
			SerialPort portaSerial = (SerialPort) portaComm.open("/dev/ttyS0", 2000);
			portaSerial.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); 

			OutputStream streamSaida = portaSerial.getOutputStream();

			for (int i = 0; i < mensagensSIM.size(); ++i)
			{
				if (i < (mensagensSIM.size() - 1))
					streamSaida.write((mensagensSIM.get(i) + enter).getBytes());
				else
					streamSaida.write((mensagensSIM.get(i) + ctrlz).getBytes());
				Thread.sleep(1000);
				streamSaida.flush();
			}

			portaSerial.close();
		}
		catch (PortInUseException excecao)
		{
			excecao.printStackTrace();
		}
		catch (IOException excecao)
		{
			excecao.printStackTrace();
		}
		catch (UnsupportedCommOperationException excecao)
		{
			excecao.printStackTrace();
		}
		catch (InterruptedException excecao)
		{
			excecao.printStackTrace();
		}
	}

	public static void main(String[] args)
	{
		ComunicacaoMovel comunicacaoMovel = new ComunicacaoMovel();
		comunicacaoMovel.enviarMensagemSMS("+5527999XXXXXX", "Test");
	}
}

I suspect that the RXTX library not works correctly, because the SIM900 instructions is the same on both codes (Python and Java).

Anyone can help me?

Thank you!

User avatar
The Traveler
Posts: 360
Joined: Sat Oct 21, 2017 3:48 pm

Re: Send SMS with GSM GPRS A6 Module and Java

Sun Jul 15, 2018 5:58 pm

error log?
Retired IT professional, C programmer and "beardie weirdie".
RPi interests: Developing an Infinite Improbability Drive
“Thinking outside of the box allows you to get rewards outside of your reach.” Matshona Dhliwayo

cayofontana
Posts: 5
Joined: Fri Jun 22, 2018 12:10 am
Location: Brasil

Re: Send SMS with GSM GPRS A6 Module and Java

Mon Jul 16, 2018 3:52 pm

Hi The Traveler,

I change the code and now becomes:

Code: Select all

import java.io.IOException;
import java.io.OutputStream;

import java.util.Enumeration;
import java.util.List;
import java.util.ArrayList;

import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;

class ComunicacaoMovel
{
	private boolean fim;

	public ComunicacaoMovel()
	{
		fim = false;
	}

	private CommPortIdentifier obterPortaCommSerial()
	{
		Enumeration portasComm = CommPortIdentifier.getPortIdentifiers();

		while (portasComm.hasMoreElements())
		{
			CommPortIdentifier portaComm = (CommPortIdentifier) portasComm.nextElement();

			if (portaComm.getPortType() == CommPortIdentifier.PORT_SERIAL && portaComm.getName().equals("/dev/ttyS0"))
				return (portaComm);
		}

		return (null);
	}

	public void enviarMensagemSMS(String numeroCelular, String mensagem)
	{
		CommPortIdentifier portaComm = obterPortaCommSerial();
		List<String> mensagensSIM = new ArrayList<String>();

		mensagensSIM.add("AT" + "\r\n");
		mensagensSIM.add("ATE0" + "\r\n");
		mensagensSIM.add("AT+CMGF=1" + "\r\n");
		mensagensSIM.add("AT+CMGS=\"" + numeroCelular + "\"" + "\r\n");
		mensagensSIM.add(mensagem + "\r");

		try
		{
			SerialPort portaSerial = (SerialPort) portaComm.open("/dev/ttyS0", 2000);
			portaSerial.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); 

			OutputStream streamSaida = portaSerial.getOutputStream();

			final Thread threadLeituraStream = new Thread()
			{
				@Override
				public void run()
				{
					try
					{
						final BufferedReader leitor = new BufferedReader(new InputStreamReader(portaSerial.getInputStream()));
						String linha = null;
						while ((linha = leitor.readLine()) != null && !fim)
							System.out.println(linha);
						leitor.close();
					}
					catch (final Exception excecao)
					{
						excecao.printStackTrace();
					}
				}
			};
			threadLeituraStream.start();

			for (int i = 0; i < mensagensSIM.size(); ++i)
			{
				streamSaida.write((mensagensSIM.get(i)).getBytes());
				streamSaida.flush();
				Thread.sleep(1000);
			}
			fim= !fim;

			streamSaida.close();
			portaSerial.close();
		}
		catch (PortInUseException excecao)
		{
			excecao.printStackTrace();
		}
		catch (IOException excecao)
		{
			excecao.printStackTrace();
		}
		catch (UnsupportedCommOperationException excecao)
		{
			excecao.printStackTrace();
		}
		catch (InterruptedException excecao)
		{
			excecao.printStackTrace();
		}
	}

	public static void main(String[] args)
	{
		ComunicacaoMovel comunicacaoMovel = new ComunicacaoMovel();
		comunicacaoMovel.enviarMensagemSMS("+5527999XXXXXX", "Test");
	}
}

Here the output (without errors):

Code: Select all

OK

OK

OK

>

... and the console stay lock with this program. It isn't return to console command prompt and I don't receive the SMS.

But curiously, the Python code send SMS correctly.

What do you think?

Thank you!

Return to “Java”

Who is online

Users browsing this forum: No registered users and 2 guests