2

Sort of a follow up to this question, which I have now had a measure of success with (the LED turns on and off, hooray!), I now have another question.

Besides writing to the device, I also have a need to read from the device. I would prefer NOT to have a thread sit there and query the device every 50 MS (considering the setting in which this device will be employed that's probably the only way to get this to work without whatever the Java equivalent of .Net Events are).

Is there a way to set up a Java "event" (term used loosely) that will fire when there are bytes waiting to be read from the device?

What I have now to write to the device is a terminal interface that accepts strings and responds to them by writing a byte array (or terminating):

    BufferedReader BR = new BufferedReader(new InputStreamReader(System.in));
    byte[] bBuffer = new byte[6];
    String cmd = BR.readLine();
    while (true){
        switch(cmd){
            case "ON":
                dev.write(ON);
                Thread.sleep(READ_UPDATE_DELAY_MS);
                dev.read(bBuffer);
                break;
            case "OFF":
                dev.write(OFF);
                Thread.sleep(READ_UPDATE_DELAY_MS);
                dev.read(bBuffer);
                break;
            case "EXIT":
                System.exit(0);
        }           
        cmd = BR.readLine();
    }

What I was hoping for was something like:

dev.InputReceived = <some input handling function>;

Is this possible?

Community
  • 1
  • 1
Will
  • 3,413
  • 7
  • 50
  • 107
  • You can see at Reader's ready method or InputStream's available method. There's no such events, you can just check is there something to read, or not. – Anatoly May 22 '14 at 16:19
  • no, no, no that isn't what... that's just the command line reader for checking the users input to see what the device should send. – Will May 22 '14 at 17:08

0 Answers0