如何将AT命令的输出转换为java中的字符串? [英] How to get the output of AT command into a string in java?

查看:599
本文介绍了如何将AT命令的输出转换为java中的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取AT命令的结果(执行从控制台对GSM调制解调器执行各种操作的命令)。我已经看到并成功测试了使用Java OuputStream类来获取AT命令的结果作为输出流,但我需要做的是将结果不是作为输出流而是在我的类中的变量(现在为String)。 / p>

如果可以这样做

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

工作正常,怎么可能做这样的事情

  Strign resultOfCommmand = .....某些AT命令的结果; 

我正在尝试这种方式

  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();

字符串行;
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();
}
}
}

返回sb.toString();

}

但是得到了结果我得到了at命令本身作为字符串
像这样....
输出:

  66666666666666666666 ----- AT + CMGR = 20 


解决方案

您必须重新调整流量程序,以便它通过发送AT命令行开始,然后读回并解析从调制解调器返回的每一条响应行,直到您得到最终结果代码。有关概述,请参见此答案



正确解析AT命令响应并不复杂,请查看 atinout 的例子。


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

解决方案

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.

这篇关于如何将AT命令的输出转换为java中的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆