Results 1 to 16 of 16
Thread: Sending AT command for sms
- 11-15-2010, 06:02 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 19
- Rep Power
- 0
Sending AT command for sms
Dear All,
Below is my codes. I have already managed to open the port sucessfully. I could send the AT+CMGF command, but in order to read the response as "OK" I had to do 4 is.readLine();. So my next problem is how to send the AT+CMGS which also require to send the ctrl+z command at the end? Below is my codes.
Java Code:import java.io.*; import javax.comm.*; import java.util.*; public class PortWriter { static Enumeration ports; static CommPortIdentifier pID; static OutputStream outStream; static SerialPort serPort; static BufferedReader is; static PrintStream os; public PortWriter() throws Exception{ try { serPort = (SerialPort)pID.open("/dev/ttyUSB0",2000); System.out.println(); System.out.println(); serPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE); try { is = new BufferedReader(new InputStreamReader(serPort.getInputStream())); } catch (IOException e) { System.err.println("Can't open input stream: write-only"); is = null; } os = new PrintStream(serPort.getOutputStream(),true, "ISO-8859-1"); } catch (PortInUseException e) { System.out.println("PortInUseException : "+e); } catch (Exception e) { System.out.println("PortInUseException : "+e); } } public static void main(String[] args) throws Exception{ ports = CommPortIdentifier.getPortIdentifiers(); while(ports.hasMoreElements()) { pID = (CommPortIdentifier)ports.nextElement(); System.out.println("Port " + pID.getName()); if (pID.getPortType() == CommPortIdentifier.PORT_SERIAL) { if (pID.getName().equals("/dev/ttyUSB0")) { PortWriter pWriter = new PortWriter(); System.out.println("USB found"); try { os.print("AT+CMGF=1"); os.print("\r\n"); is.readLine(); is.readLine(); is.readLine(); String cmgfresponse = is.readLine(); System.out.println("CMGF Response is :"+cmgfresponse); os.print("AT+CMGS=\"+60165522334\""); os.print("\r\n"); is.readLine(); is.readLine(); is.readLine(); String cmgsresponse = is.readLine(); System.out.println("CMGS Response is :"+cmgsresponse); if (is != null) is.close(); if (os != null) os.close(); if (serPort != null) serPort.close(); } catch (IOException e) { System.out.println("could not write to outputstream:"); System.out.println(e.toString()); } } } } } }
- 11-16-2010, 10:24 AM #2
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 364
- Rep Power
- 4
In most examples i see, people use
java.io.OutputStream getOutputStream() on Serial port.
They create method that receive String as argument(some AT commands),
and encode this String into a sequence of bytes using the platform's default charset :
Java Code:public void send(String cmd) { try { outputStream.write(cmd.getBytes()); } catch (IOException e) {
For ctrl+z they simply send:
'\032'
hope this can help
- 11-16-2010, 01:58 PM #3
Member
- Join Date
- Feb 2010
- Posts
- 19
- Rep Power
- 0
Dear Fon,
Thank you first for your reply. So ok just say I modify now. So if I send like this will this be ok or not.
String cmd="AT+CMGS=\"+60165522334\"\r\n";
String cmd1="hi testing\032"
Do you think first I send the cmd then I send cmd1 will this work correctly. Just to inform you I am using linux environment. Thank you.
- 11-16-2010, 02:18 PM #4
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 364
- Rep Power
- 4
You have to experiment with that, but let me give you this advice from WikiBooks on topic 'Serial Programming/Serial Java':
I'm sure that will help youSimple Writing of Data
Writing to a serial port is as simple as basic Java IO. However there are a couple of caveats to look out for if you are using the AT Hayes protocol:
1. Don't use println (or other methods that automatically append "\n") on the OutputStream. The AT Hayes protocol for modems expects a "\r\n" as the delimiter (regardless of underlying operating system).
2. After writing to the OutputStream, the InputStream buffer will contain a repeat of the command that was sent to it (with line feed), if the modem is set to echoing the command line, and another line feed (the answer to the "AT" command). So as part of the write operation make sure to clean the InputStream of this information (which can actually be used for error detection).
3. When using a Reader/Writer (not a really good idea), at least set the character encoding to US-ASCII instead of using the platform's default encoding, which might or might not work.
4. Since the main operation when using a modem is to transfer data unaltered, the communication with the modem should be handled via InputStream/OutputStream, and not a Reader/Writer
- 11-17-2010, 01:54 AM #5
Member
- Join Date
- Feb 2010
- Posts
- 19
- Rep Power
- 0
Dear Fon,
Actually I also referred the Wikibooks before this and my codes is based on their example.Ok how to know whether the modem is set to echoeing command line? Is there any special command for that or is it product to product basis. How do I go about cleaning the inputstream I am not to sure about it too?
- 11-17-2010, 09:38 AM #6
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 364
- Rep Power
- 4
I'm not expert on AT commands;
there are AT commands related to echo :
E0 : Inhibits the command echo.
E1 : Enables the command echo.
But you can simply check if echo is on:
by reading and printing to console result of reading, immediately after sending command, something like:
hope this can help.Java Code:... os.print("AT+CMGF=1"); os.print("\r\n"); response = is.readLine( ); System.out.print(response); ...
TIP:
Please focus on 'crtrl+z' issue, and if you have solved it please share it with rest of us on forum pls
- initial question was about it, then move on to all other questions.
Thank you!
- 11-17-2010, 06:27 PM #7
Member
- Join Date
- Feb 2010
- Posts
- 19
- Rep Power
- 0
Dear Fon,
Ok great it works below is my codes. The problem is that if I uncomment this part then the message send it AT+CMGF=1 a line break then AT+CMGS="+60165522334 and not the actual message. Why this happens ya? Ok so if I would like to read the input stream what is the best way becasue according to the wiki books is some kind of blocking method I am not too sure either too.
String sCMGF= "AT+CMGF=1\r\n";
send(sCMGF);
Thread.sleep(1000);
Java Code:public static void main(String[] args) throws Exception{ ports = CommPortIdentifier.getPortIdentifiers(); while(ports.hasMoreElements()) { pID = (CommPortIdentifier)ports.nextElement(); System.out.println("Port " + pID.getName()); if (pID.getPortType() == CommPortIdentifier.PORT_SERIAL) { if (pID.getName().equals("/dev/ttyUSB0")) { PortWriter pWriter = new PortWriter(); System.out.println("USB found"); try { //String sCMGF= "AT+CMGF=1\r\n"; //send(sCMGF); //Thread.sleep(1000); String sCMGS="AT+CMGS=\"+60165522334\"\r\n"; send(sCMGS); Thread.sleep(1000); String smsMessage="hi testing done 1\032\r\n"; send(smsMessage); if (is != null) is.close(); if (os != null) os.close(); if (serPort != null) serPort.close(); } catch (IOException e) { System.out.println("could not write to outputstream:"); System.out.println(e.toString()); } } } } } public static void send(String cmd) { try { os.write(cmd.getBytes()); } catch (IOException e) { System.out.println("IO Exception : "+e); } }
- 11-17-2010, 06:33 PM #8
Member
- Join Date
- Feb 2010
- Posts
- 19
- Rep Power
- 0
Dear Fon,
Just for testing after the line String smsMessage="hi testing done 1\032\r\n"; I have added three readline statement. Only the first give value that is the echo of "hi testing done 1" the rest is all empty. How can I read the message ID from the gsm modem which it will generate after send the message. Thank you.
Java Code:send(smsMessage); String response = is.readLine( ); System.out.println(response); String response2 = is.readLine( ); System.out.println(response2); String response3 = is.readLine( ); System.out.println(response3);
- 11-18-2010, 11:25 AM #9
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 364
- Rep Power
- 4
For testing you can install some 'Free Serial Port' monitor tool.
and use it to help you debug.
tips:
- don't put your code in main()
- create separate methods for: port indentifying, opening, closing, getting and closing steams, sending msg
-use debugger to track port issues
I'm not sure i understand yours questions from last 2 posts so please make a corrections to code and try to rephrase those questions.
- 11-19-2010, 07:22 PM #10
Member
- Join Date
- Feb 2010
- Posts
- 19
- Rep Power
- 0
Dear Fon,
Attached below is my new codes I have now two new function openingPort and closingPort. What type of debbuger to use to track port issues? My question is that how to correctly read the input stream once my sms is sent sucessfully.
import java.io.*;
import javax.comm.*;
import java.util.*;
public class PortWriter
{
static Enumeration ports;
static CommPortIdentifier pID;
static OutputStream outStream;
static SerialPort serPort;
static InputStream is;
static PrintStream os;
static int i=0;
public PortWriter() throws Exception{
try
{
serPort = (SerialPort)pID.open("/dev/ttyUSB0",2000);
System.out.println();
System.out.println();
//System.out.println("\ngetDataBits"+serPort.getData Bits());
//System.out.println("\ngetStopBits"+serPort.getStop Bits());
//System.out.println("\ngetParity"+serPort.getParity ());
//System.out.println("\ngetBaudRate"+serPort.getBaud Rate());
serPort.setSerialPortParams(9600,SerialPort.DATABI TS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE) ;
try {
is = serPort.getInputStream();
} catch (IOException e) {
System.err.println("Can't open input stream: write-only");
is = null;
}
os = new PrintStream(serPort.getOutputStream(),true, "US-ASCII");
}
catch (PortInUseException e)
{
System.out.println("PortInUseException : "+e);
}
catch (Exception e)
{
System.out.println("PortInUseException : "+e);
}
}
public static void main(String[] args) {
openingPort();
}
public static void openingPort()
{
ports = CommPortIdentifier.getPortIdentifiers();
while(ports.hasMoreElements())
{
pID = (CommPortIdentifier)ports.nextElement();
System.out.println("Port " + pID.getName());
if (pID.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
if (pID.getName().equals("/dev/ttyUSB0"))
{
try
{
PortWriter pWriter = new PortWriter();
}
catch (Exception e)
{
System.out.println("error in calling PortWriter");
System.out.println(e.toString());
}
System.out.println("USB found");
try
{
//String sCMGF= "AT+CMGF=1\r\n";
//send(sCMGF);
//Thread.sleep(1000);
String sCMGS="AT+CMGS=\"+60175533443\"\r\n";
send(sCMGS);
Thread.sleep(1000);
String smsMessage="hi testing done\032\r\n";
send(smsMessage);
//Thread.sleep(1000);
//String response = is.readLine( );
//System.out.println(response);
//Thread.sleep(1000);
//String response2 = is.readLine( );
//System.out.println(response2);
//Thread.sleep(1000);
//String response3 = is.readLine( );
//System.out.println(response3);
try
{
closingPort();
}
catch (Exception e)
{
System.out.println("error in calling closingPort()");
System.out.println(e.toString());
}
}
catch (Exception e)
{
System.out.println("could not write to outputstream:");
System.out.println(e.toString());
}
}
}
}
}
public static void closingPort() throws Exception
{
try
{
if (is != null)
is.close();
}
catch (IOException e)
{
System.out.println("could not close is");
System.out.println(e.toString());
}
try
{
if (os != null)
os.close();
}
catch (Exception e)
{
System.out.println("could not close os");
System.out.println(e.toString());
}
try
{
if (serPort != null)
serPort.close();
}
catch (Exception e)
{
System.out.println("could not close serPort");
System.out.println(e.toString());
}
}
public static void send(String cmd)
{
try
{
os.write(cmd.getBytes());
} catch (IOException e)
{
System.out.println("IO Exception : "+e);
}
}
}
- 11-23-2010, 01:22 AM #11
Member
- Join Date
- Feb 2010
- Posts
- 19
- Rep Power
- 0
Dear Fon,
Any comments from you side. Thank you.
- 11-23-2010, 09:28 AM #12
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 364
- Rep Power
- 4
This is much better but still needs to be improved - use 1 method to perform one function - don't put all in opening,
create separate methods for that, and call it from main one by one - ...open(), send(), read(), close().
I see 2 send() and no readline().
Can you do 1 send() and one readline(),
and again another send() and readline()?
use idents when you paste code here so it's easier to read pls.
Before you send() add \\comment to explain what send() performs. Do it for all methods() and inside methods on importat places. Remove block comments when posting here - to much info...
Use standard debugger in your IDE - Eclipse? Netbeans?
Also you can use external tool as i mentioned - utility application that you can install and run separately.
- 11-25-2010, 08:15 PM #13
Member
- Join Date
- Feb 2010
- Posts
- 19
- Rep Power
- 0
Dear Fon,
Attached below is my latest codes. I have done as per your sugesstions. The problem now out of the 3 readLine call only one of it shows the value as "Response is :testing done". This is for the first call the second and third is empty. Is this correct output or something is wrong?
import java.io.*;
import javax.comm.*;
import java.util.*;
public class PortWriter
{
static Enumeration ports;
static CommPortIdentifier pID;
static OutputStream outStream;
static SerialPort serPort;
static BufferedReader is = null;
static PrintStream os;
static int i=0;
public PortWriter() throws Exception{
try
{
serPort = (SerialPort)pID.open("/dev/ttyUSB0",2000);
System.out.println();
System.out.println();
serPort.setSerialPortParams(9600,SerialPort.DATABI TS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE) ;
try
{
is = new BufferedReader(new InputStreamReader(serPort.getInputStream()));
}
catch (IOException e)
{
System.err.println("Can't open input stream: write-only");
is = null;
}
os = new PrintStream(serPort.getOutputStream(),true, "US-ASCII");
}
catch (PortInUseException e)
{
System.out.println("PortInUseException : "+e);
}
catch (Exception e)
{
System.out.println("PortInUseException : "+e);
}
}
public static void main(String[] args) throws Exception{
ports = CommPortIdentifier.getPortIdentifiers();
while(ports.hasMoreElements())
{
pID = (CommPortIdentifier)ports.nextElement();
System.out.println("Port " + pID.getName());
if (pID.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
if (pID.getName().equals("/dev/ttyUSB0"))
{
PortWriter pWriter = new PortWriter();
System.out.println("USB found");
sendData();
try
{
closingPort();
}
catch (Exception e)
{
System.out.println("error in calling closingPort()");
System.out.println(e.toString());
}
}
}
}
}
public static void sendData()
{
try
{
String sCMGF="AT+CMGF=1\r\n";
send(sCMGF);
Thread.sleep(1000);
readLine();
String sCMGS="AT+CMGS=\"+60124455332\"\r\n";
send(sCMGS);
Thread.sleep(1000);
readLine();
String smsMessage="hi testing done\032\r\n";
send(smsMessage);
readLine();
}
catch (Exception e)
{
System.out.println("could not write to outputstream:");
System.out.println(e.toString());
}
}
//called finally to close the port
public static void closingPort() throws Exception
{
try
{
if (is != null)
is.close();
}
catch (IOException e)
{
System.out.println("could not close is");
System.out.println(e.toString());
}
try
{
if (os != null)
os.close();
}
catch (Exception e)
{
System.out.println("could not close os");
System.out.println(e.toString());
}
try
{
if (serPort != null)
serPort.close();
}
catch (Exception e)
{
System.out.println("could not close serPort");
System.out.println(e.toString());
}
}
//send the string to the gsm modem
public static void send(String cmd)
{
try
{
os.write(cmd.getBytes());
}
catch (IOException e)
{
System.out.println("IO Exception : "+e);
}
}
// to read line after each send of string
public static void readLine()
{
try
{
// Read the response
String response = is.readLine();
System.out.println("Response is :"+response);
} catch (IOException e)
{
System.out.println("IO Exception : "+e);
}
}
}Last edited by newbie14; 11-25-2010 at 08:16 PM. Reason: I am trying to indent my codes it does not work.
- 11-25-2010, 08:17 PM #14
Member
- Join Date
- Feb 2010
- Posts
- 19
- Rep Power
- 0
Dear Fon,
I am sorry I already indent my codes but when I see the output it does not indent. I do not know what is the problem. Hope you can help with my codes.
- 11-27-2010, 01:30 AM #15
Member
- Join Date
- Feb 2010
- Posts
- 19
- Rep Power
- 0
Dear Fon,
I think the sending of sms looks quite stable only problem now is reading the input stream. I would need your help to receive the input stream properly for example I send a sms surely there will be the sms Id which I would like to capture and store into database. Thank you.
- 11-29-2010, 03:33 AM #16
Member
- Join Date
- Feb 2010
- Posts
- 19
- Rep Power
- 0
Similar Threads
-
sending email
By kkk in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 08-21-2009, 10:20 AM -
sending to client
By rob in forum New To JavaReplies: 1Last Post: 02-22-2009, 11:07 AM -
Sending a Mail
By haiforhussain in forum Advanced JavaReplies: 7Last Post: 07-30-2008, 12:28 AM -
Unable to execute command line command in java
By LordSM in forum New To JavaReplies: 1Last Post: 08-08-2007, 12:23 AM


1Likes
LinkBack URL
About LinkBacks

Bookmarks