简体   繁体   English

如何在Java中将AT命令的输出转换为字符串?

[英]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). 我正在尝试读取AT命令的结果(执行该命令以从控制台对GSM调制解调器执行各种操作)。 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. 我已经看到并使用Java OuputStream类成功进行了测试,以将AT命令的结果作为输出流获取,但是我需要做的不是将结果作为输出流获取,而是获取类中的变量(目前为String)。

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: 但是为了获得结果,我想要像这样的字符串来获取at命令本身。...输出:

66666666666666666666-----AT+CMGR=20

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. 您必须重组程序中的流,以便从发送AT命令行开始,然后回读并解析从调制解调器返回的每个响应行,直到获得最终结果代码为止。 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. 正确地解析AT命令响应并不那么复杂,以atinout的源代码为例。

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

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