Search code examples
javatimerglobal-variablesbufferedreaderrxtx

Java recognize AT commands with RXTX library


I'm going to read COM port in java with RXTX library. I want to recognize the flow of char in AT commands.

My reading function cycle a buffer and save all on a String var.

InputStream in; //var sent to function

byte[] buffer = new byte[1024];
int len = -1;

try{
    while ((len = in.read(buffer)>-1){
        String s = new String(buffer,0,len);

        // I've think to save all string into a global var and ciclically
        // read after delay che global string and analyze it for bring the 
        // AT commands
        Global.__GLstring += s;

        System.out.print(s); 
        /* Example sout:

           AT

           OK

           BLABLABLA
           ERROR
        */

    }
} catch (IOException e){
        syserr("Exception"+e);
}

How can I create the timer in the background? There are better solutions or functions already made?

I hope to be able to explain myself. Thanks to all!


Solution

  • Use java.util.Timer. This is exactly what you need.