Uderc programming article aggregator sites

AT Commands , Hyper Terminal and java

Tags:
Answers: Have 7 answers
Advertisement
Hi,
I have following problem, i have PCMCIA Type -11 Wireless modem.its installed on my system COM4 port.
Now first problem is that when i try to connect with hyper terminal to COM4 there is no response from it .It become Idle.but when i connect with COM3 which is my Dialup modem port it works fine with all AT commands.
Now 2nd things is that same with below code i tried to send sms using AT command on COM4 its has same behaviour as it was in Hyper terminal mean no response. no Data Available event occour
Please help me in this regard and suggest me a solution.
package serialio;
import javax.comm.*;
import java.io.*;
import java.util.*;
public class SimpleWrite
    implements Runnable, SerialPortEventListener
  public void run()
  static Enumeration portList;
  static CommPortIdentifier portId;
  static String dest = "0517111930";
  static String messageString = "Hello";
  InputStream inputStream;
  static SerialPort serialPort;
  static OutputStream outputStream;
  public void serialEvent(SerialPortEvent event)
    switch (event.getEventType())
      case SerialPortEvent.BI:
      case SerialPortEvent.OE:
      case SerialPortEvent.FE:
      case SerialPortEvent.PE:
      case SerialPortEvent.CD:
      case SerialPortEvent.CTS:
      case SerialPortEvent.DSR:
      case SerialPortEvent.RI:
      case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
        break;
      case SerialPortEvent.DATA_AVAILABLE:
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        String line = "";
        try
          while ( (line = reader.readLine()) != null)
            System.out.println(line);
        catch (IOException e)
          System.err.println("Error while reading Port " + e);
        break;
    } //switch
  public SimpleWrite(SerialPort serial)
    try
      inputStream = serial.getInputStream();
      try
        serial.addEventListener(this);
      catch (TooManyListenersException e)
        System.out.println("Exception in Adding Listener" + e);
      serial.notifyOnDataAvailable(true);
    catch (Exception ex)
      System.out.println("Exception in getting InputStream" + ex);
  public static void main(String[] args)
//    String line1="AT+CMGF=1\r\n";
//    String line2="AT+CMGS=\""+ dest+"\"\r\n";
//    String line3=messageString+"\r\n";
//    String line4="\u001A";
    String line1 = "AT+CMGF=1\r\n";
    String line2 = "AT+CMGS=" + dest + "\r\n";
    String line3 = messageString + "^Z\r\n";
//    String line4 = "\u001A";
    portList = CommPortIdentifier.getPortIdentifiers();
    while (portList.hasMoreElements())
      portId = (CommPortIdentifier) portList.nextElement();
      if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
        System.out.println("SMS Sending........");
        if ( portId.getName().equals("COM4"))
          System.out.println("SMS Sending....Port Found");
          try
            serialPort = (SerialPort) portId.open("SimpleWriteApp", 2000);
            SimpleWrite wr = new SimpleWrite(serialPort);
          catch (PortInUseException e)
            System.out.println("Port In Use " + e);
          try
            outputStream = serialPort.getOutputStream();
          catch (IOException e)
            System.out.println("Error writing to output stream " + e);
          try
            serialPort.setSerialPortParams(
                2400,
                SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE);
          catch (UnsupportedCommOperationException e)
          try
            outputStream.write(line1.getBytes());
            outputStream.write(line2.getBytes());
            outputStream.write(line3.getBytes());
            outputStream.flush();
          catch (IOException e)
            System.out.println("Error writing message " + e);
  /** show text in the text window
   * @param Text text string to show on the display
  public static void showText(String Text)
    System.out.println(Text);
}
Advertisement
The best answer: * SerialWrite.java
* Created on September 10, 2008, 2:51 PM
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
package com.sms;
import javax.comm.*;
import java.io.*;
import java.util.*;
public class SerialWrite implements Runnable, SerialPortEventListener
static String output="";
public void run()
static Enumeration portList;
static CommPortIdentifier portId;
static String dest = "+91999999999";
static String messageString = "Hello Testing11";
InputStream inputStream;
static SerialPort serialPort;
static OutputStream outputStream;
public void serialEvent(SerialPortEvent event)
switch (event.getEventType())
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
          System.out.println("Error");
break;
case SerialPortEvent.DATA_AVAILABLE:
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
try
while ( (line = reader.readLine()) != null)
if(line.equalsIgnoreCase("OK") || (line.indexOf("+CMGS") != -1))
     output=line;
          Thread.sleep(10);
catch (Exception e)
System.err.println("Error while reading Port " + e);
break;
} //switch
public SerialWrite(SerialPort serial)
try
inputStream = serial.getInputStream();
try
serial.addEventListener(this);
catch (TooManyListenersException e)
System.out.println("Exception in Adding Listener" + e);
serial.notifyOnDataAvailable(true);
catch (Exception ex)
System.out.println("Exception in getting InputStream" + ex);
public static void main(String[] args) throws Exception
     int i=0;
String line0 = "ATZ\r\n";
String line1 = "AT+CMGF=1\r\n";
String line2 = "AT+CSCA=\"+919888888\"\r\n";
//String line3 = "AT+CMGS="+"\""+ dest + "\""+"\r\n";
String line3 = "AT+CMGS="+"\""+ args[0] + "\""+"\r\n";
//String line4 = messageString;
String line4 = args[1];
     portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements())
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
System.out.println("SMS Sending........");
if ( portId.getName().equals("/dev/ttyACM0"))
System.out.println("SMS Sending....Port Found");
try
serialPort = (SerialPort) portId.open("SerialTestApp", 2000);
SerialWrite wr = new SerialWrite(serialPort);
catch (PortInUseException e)
System.out.println("Port In Use " + e);
try
outputStream = serialPort.getOutputStream();
catch (IOException e)
System.out.println("Error writing to output stream " + e);
try
serialPort.setSerialPortParams(
115200,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
catch (UnsupportedCommOperationException e)
try
outputStream.write(line0.getBytes());
          Thread.sleep(10);
while(!output.equalsIgnoreCase("ok"))
          Thread.sleep(10);
          output="";
outputStream.write(line1.getBytes());
while(!output.equalsIgnoreCase("ok"))
               Thread.sleep(10);
          output="";
outputStream.write(line2.getBytes());
while(!output.equalsIgnoreCase("ok"))
               Thread.sleep(10);
          output="";
     for(i=0;i<3;i++)
outputStream.write(line3.getBytes());
Thread.sleep(10);
outputStream.write(line4.getBytes());
Thread.sleep(10);
outputStream.write(26);
outputStream.write("\r\n".getBytes());
outputStream.flush();
while(!output.equalsIgnoreCase("ok"))
               Thread.sleep(10);
          output="";
          System.out.println("Message Sent !!!!!!!!!1");
catch (IOException e)
System.out.println("Error writing message " + e);
/** show text in the text window
* @param Text text string to show on the display
public static void showText(String Text)
System.out.println("TEXT "+Text);
}