After a very long time, I finally have time to implement this idea: AT command parser. My first thought about AT command was how to separate the data from the OK/ERROR, and how to differentiate the URC from AT command.
So, here’s my idea (to be specific, the interface for the AT parser):
/** * Send AT command to the serial port * * @param atCmd The AT command (ended with r or ) */ void sendAtCmd(String atCmd) throws IOException; /** * Get the parser status * * @return true if parsing is done, false otherwise */ boolean isParsingDone(); /** * Get the AT command's data * * @return Array of string consist of the AT command data */ String[] getAtCmdData(); /** * Get the status of the executed AT command * * @return AtCmdStatus */ AtCmdStatus getAtCmdStatus();
The usage of the AT command parser will be something like this:
try {
atParser.sendAtCmd("AT+CMGL=4r");
// Wait until the parsing is done
while (!atParser.isParsingDone()) {
// Sleep for 1-s
Thread.sleep(10);
}
if (atParser.getAtCmdStatus() == AtCmdStatus.OK) {
for (String atCmdData : atParser.getAtCmdData()) {
System.out.println("Data :" + atCmdData);
}
} else {
System.out.println("AT Command Error");
}
} catch (InterruptedException ex) {
} catch (IOException ex) {
System.err.println(ex.getMessage());
System.exit(1);
}
