How to get the output of AT command into a string in java?

青春壹個敷衍的年華 提交于 2019-11-27 03:41:46

问题


I am trying to read the result of an AT command (command executed to do various operation to a GSM modem from console). I have seen and successfully tested using the Java OuputStream class to get the result of an AT command as output stream but what I need to do is to get the result not as outputstream but into a variable (String for now) in my class.

If it is possible to do like

outStream.write(("Some At command").getBytes());

which works fine, how can it be possibe to do something like this

Strign resultOfCommmand=.....result of some AT command;

I am trying it in this way

 InputStream is = new ByteArrayInputStream(("some at commamd").getBytes());

 String result = getStringFromInputStream(is);

 System.out.println("66666666666666666666-----"+result);

/////////////////////

 private static String getStringFromInputStream(InputStream is) {

        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();

        String line;
        try {

            br = new BufferedReader(new InputStreamReader(is));
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return sb.toString();

    }

but insted of getting the result iam getting the at command itself as a string like this.... output:

66666666666666666666-----AT+CMGR=20

回答1:


You must restructure the flow in your program so that it starts by sending the AT command line, and then read back and parse the every single response line given back from the modem until you get a final result code. See this answer for the general outline.

Properly parsing AT command responses is not that complicated, have a look at the source code for atinout for an example.



来源:https://stackoverflow.com/questions/20215374/how-to-get-the-output-of-at-command-into-a-string-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!