Question

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
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top