Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
309 views
in Technique[技术] by (71.8m points)

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

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...