简体   繁体   English

在Java中获得AT COMMAND的意外结果

[英]Getting unexpected result of AT COMMAND in java

import gnu.io.*;
import java.io.*;
import java.util.*;
import java.lang.*;

public class SerialWrite implements Runnable, SerialPortEventListener{

    static String output="";

    public void run(){
    }

    static Enumeration portList;
    static CommPortIdentifier portId;
    static String dest = "+923216159133";
    static String messageString = "Hello Testing";
    static 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 line1 = "AT+CMGF=1\r\n";
    String line2 = "AT+CMGS=" + "\"" + dest + "\""+"\r\n";
    System.out.println("This " + line2);
    String line3 = messageString;
    String line4 = "<ctrl+z>";
    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("COM3")){
                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(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
                }
                catch (UnsupportedCommOperationException e){
                    System.out.println("Error");
                }
                try{
                    System.out.println("It seems OK now");
                    outputStream.write(line1.getBytes());
                    byte buffer[] = new byte[10000];
                    // read the response from mobile phone
                    inputStream.read(buffer);
                    System.out.println("AT Comand response: "+buffer.toString());
                    /*System.out.println ("done");
                    outputStream.write(line2.getBytes());
                    System.out.println("It seems OK now");
                    outputStream.write(line3.getBytes());
                    outputStream.write(line4.getBytes());
                    System.out.println("This one is output "+output);
                    outputStream.flush();
                    System.out.println("Message Sent!");*/
                }
                catch (IOException e){
                    System.out.println("Error writing message " + e);
                }
            }
        }
    }
}


public static void showText(String Text){
    System.out.println("TEXT "+Text);
    }
}

I want to send a message using GSM PHONE (NOKIA 110) first I tried to send "AT" command but the response I am getting is unexpected it is shown below: 我想先使用GSM PHONE(诺基亚110)发送消息,我尝试发送“ AT”命令,但收到的响应是意外的,如下所示:

SMS Sending........
SMS Sending....Port Found
It seems OK now
**AT Comand response: [B@3820e**
SMS Sending........
SMS Sending........
SMS Sending........
SMS Sending........

Secondly when I run the whole program without COMMENTS my mobile restarts at the end. 其次,当我在没有COMMENTS的情况下运行整个程序时,我的手机最终会重启。 Can anyone help me out with this. 谁能帮我这个忙。

You need to change how you print the contents of the received byte array: 您需要更改打印接收到的字节数组内容的方式:

String str = new String(buffer);
System.out.println(str);

instead of: buffer.toString() , which will print the hashcode of the buffer array. 代替: buffer.toString() ,它将打印缓冲区数组的哈希码。 Which is what you are seeing as the response from the device. 这是您从设备看到的响应。

As for your second part, in the commented out code, try uncommenting: outputStream.flush(); 至于第二部分,在注释掉的代码中,尝试取消注释: outputStream.flush(); , maybe the device still is waiting for the AT command and the command has not been flushed to its input stream. ,则设备可能仍在等待AT命令,并且该命令尚未刷新到其输入流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM