0 Replies Latest reply: Apr 25, 2008 9:03 AM by 843807 RSS

    Keep Message With AT Command

    843807
      Problem is,I don't understand how to keep message(from telephone inbox) with String and to see from console.Can anybody check,I think I have a mistake with how to use string properties.

      import java.io.InputStream;us
      import java.io.OutputStream;
      import java.util.Enumeration;
      
      import javax.comm.CommPortIdentifier;
      import javax.comm.SerialPort;
       
      public class CommTest
      {
           static CommPortIdentifier portId;
       
           static Enumeration portList;
       
           static int bauds[] = { 115200 };
           
          static String  defaultPort = "COM9";
       
           public static void main(String[] args)
           {
                
                portList = CommPortIdentifier.getPortIdentifiers();
                while (portList.hasMoreElements())
                {
                     portId = (CommPortIdentifier) portList.nextElement();
                     
                     if (portId.getName().equals(defaultPort))
                     {
                          System.out.println("Found port: " + portId.getName());
                          for (int i = 0; i < bauds.length; i++)
                          {
                               System.out.print("     Bound Rate is " + bauds[i] + ".");
                
           
                               try
                               {
                                    SerialPort serialPort;
                                    InputStream inStream;
                                    OutputStream outStream;
                                    int c;
                                    String response;
       
                                    serialPort = (SerialPort) portId.open("Home Automation System", 1971);
                                    serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN);
                                    serialPort.setSerialPortParams(bauds, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
                                    inStream = serialPort.getInputStream();
                                    outStream = serialPort.getOutputStream();
                                    serialPort.enableReceiveTimeout(1000);

                                    c = inStream.read();
                                    
                                    while (c != -1)
                                    c = inStream.read();
                                    outStream.write('A');
                                    outStream.write('T');
                                    outStream.write('+');
                                    outStream.write('C');
                                    outStream.write('M');
                                    outStream.write('G');
                                    outStream.write('F');
                                    outStream.write('=');
                                    outStream.write('1');
                                    outStream.write('\r');
                                    
                                    try {
                                    Thread.sleep(200);
                                    }
                                    
                                    catch (Exception e) {
                                         
                                    }
                                    
                                    response = "";
                                    c = inStream.read();
                                    
                                    while (c != -1)
                                    {
                                         response += (char) c;
                                         c = inStream.read();
                                    }
                                    if (response.indexOf("OK") >= 0)
                                    {
                                         try
                                         {
                                              
                          
                                              System.out.print(" \nGetting Info...");
                                              outStream.write('A');
                                              outStream.write('T');
                                              outStream.write('+');
                                              outStream.write('C');
                                              outStream.write('M');
                                              outStream.write('G');
                                              outStream.write('L');
                                              outStream.write('=');
                                              outStream.write('"');
                                              
                                              outStream.write('A');
                                              outStream.write('L');
                                              outStream.write('L');
                                              outStream.write('"');
                                              
                                              outStream.write('\r');
                                              response = "";
                                              c = inStream.read();
                                              
                                              while (c != -1)
                                              {
                                                   response += (char) c;
                                                   c = inStream.read();
                                                                                                
                                              }
                                              
                                              System.out.println("\n Found: " + response.replaceAll("\\s+\\s+", "OK").replaceAll("\n", ""). replaceAll(" \r", ""));
                                              
                                         }
                                                                            
                                         catch (Exception e)
                                         {
                                              System.out.println(" Nobody here!");
                                         }
                                         
                                         
                                    }
                                    
                                    
                                    else
                                    System.out.println(" Crush Problem(Commands)!");
                                    serialPort.close();
                                    
                                    
                               }
                               
      //----------------------------------------------------------------------------------------------                         

                                                   
                               catch (Exception e)
                               {
                                    System.out.println(" Nobody here!");
                               }
                               
                               
                     }
                }
                }
           }
      }