6 Replies Latest reply: Dec 27, 2011 10:11 PM by 854612 RSS

    Program not exited when GSM modem is not connected on specific port

    854612
      Hi all, i have write a java program to check is GSM Modem connected on specific comm port or not via AT command. If Modem is connected it return OK and program exited, there is no problem but when Modem is not connected on this port, the program unit is going running and not exited, then i manual terminate the program. I wants that the program should be exited when Modem is not connected on this port, return status or not is not problem. My Code is following:

      package oracle.forms.sms;
      import javax.comm.*;
      import java.util.*;
      import java.io.*;

      public class CheckCommCon{
      private BufferedReader is;
      private PrintStream os;
      public String CheckPort(String pComm){
      System.setSecurityManager(null);
      String OutMsg = "";

      Enumeration portIdentifiers = CommPortIdentifier.getPortIdentifiers();
      CommPortIdentifier portId = null; // will be set if port found
      SerialPort port = null;

      while (portIdentifiers.hasMoreElements()){
      CommPortIdentifier pid = (CommPortIdentifier) portIdentifiers.nextElement();

      if(pid.getPortType() == CommPortIdentifier.PORT_SERIAL &&
      pid.getName().equals(pComm)){
      portId = pid;
      break;
      }
      }
      if(portId == null){
      System.err.println("Could not find serial port " + pComm);
      System.exit(1);
      }

      try {
      port = (SerialPort) portId.open(
      "Comm", // Name of the application asking for the port
      10000 // Wait max. 10 sec. to acquire port
      );
      } catch(PortInUseException e) {
      System.err.println("Port already in use: " + e);
      System.exit(1);
      }

      try {
      port.setSerialPortParams(
      115200,
      SerialPort.DATABITS_8,
      SerialPort.STOPBITS_1,
      SerialPort.PARITY_NONE);


      try {
      is = new BufferedReader(new InputStreamReader(port.getInputStream()),1);
      System.out.println(is);
      } catch (IOException e) {
      System.err.println("Can't open input stream: write-only");
      is = null;
      }
      try{
      os = new PrintStream(port.getOutputStream(), true);
      System.out.println(os);;
      } catch (IOException e) {
      System.err.println(e.getMessage());
      }

      os.print("AT");
      os.print("\r\n");
      try{
      is.readLine();
      is.readLine();
      //System.out.println(is.readLine());
      OutMsg = is.readLine();
      } catch (IOException e) {
      System.err.println(e.getMessage());
      System.exit(0);
      }
      } catch(UnsupportedCommOperationException e) {
      System.err.println(e.getMessage());
      System.exit(1);
      }
      return OutMsg;
      }
      public static void main (String args[])
      {
      CheckCommCon ccc = new CheckCommCon();
      System.out.println(ccc.CheckPort("COM17"));
      }
      }