0

I am using javax.comm to communicate over a serial port and the problem I am having is I am unsure how to poll the port to get the current state of my device (a 4x8 Matrix Switch) on startup. I am able to get the current state beautifully when the state changes using the serial port event listener (i.e I write something to the port, listener captures data bits as they are changed).

Here is my code for the captured events.. How can I modify this to poll for the current state without actually changing the state?

public void serialEvent(SerialPortEvent event) {
    waitingForResponse = true;
    System.out.println("Serial Event Detected: " + event.getEventType() + " Going to read buffer..");

    switch(event.getEventType()) {
    case SerialPortEvent.BI:
        System.out.println("Data Event: BI");
    case SerialPortEvent.OE:
        System.out.println("Data Event: OE");
    case SerialPortEvent.FE:
        System.out.println("Data Event: FE");
    case SerialPortEvent.PE:
        System.out.println("Data Event: PE");
    case SerialPortEvent.CD:
        System.out.println("Data Event: CD");
    case SerialPortEvent.CTS:
        System.out.println("Data Event: CTS");
    case SerialPortEvent.DSR:
        System.out.println("Data Event: DSR");
    case SerialPortEvent.RI:
        System.out.println("Data Event: RI");
    case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
        System.out.println("Data Event: OUTPUT_BUGGER_EMPTY");
        break;
    case SerialPortEvent.DATA_AVAILABLE:
        System.out.println("Data available in buffer..");
        //byte[] readBuffer = new byte[11];

        try {              
            StringBuilder readBuffer = new StringBuilder();

            try{
                int value;     
                int byteLengthCounter = 0;
                // reading just one at a time
                while (byteLengthCounter < 8) {

                    value = inputStream.read();
                    readBuffer.append((char) value);
                    currentState = new String(readBuffer);
                    byteLengthCounter++;
                }
                System.out.println("Successful read. Current state: " + getCurrentState());
                byteLengthCounter = 0;

                waitingForResponse = false;

            }catch(Exception e){System.out.println("Did not read buffer: " + e);}

        } catch (Exception e) {System.out.println(e);}
        break;
    } 

    try{
        System.out.println("Going to close steam..");
        if (inputStream != null)    inputStream.close();
        //System.out.println("Going to close port..");
        //if (serialPort != null) serialPort.close();

        }catch (Exception e){}
}
gecko25
  • 71
  • 2
  • 11
  • You're missing a bunch of `break;` statements in your switch cases. – Jim Garrison Jan 02 '14 at 21:28
  • okay yes thanks.. still not sure how to read from a port without an event change :-\ – gecko25 Jan 02 '14 at 21:45
  • Have you looked at all the `isXxxx()` methods in `SerialPort`? – Jim Garrison Jan 02 '14 at 22:17
  • hmm I have but I don't understand them very well. I am definitely a newbie here. Also, all these methods return booleans so it seems as that would just be a first step in actually reading bytes from the stream. It seems weird to me there isn't a simple way to poll the port, but like I said, not an expert. Maybe there a way to create an event without changing the state of the device, but to prompt the event listener to read the input stream? (to me, this seems like the definition of polling..) Thanks for help in advance.. – gecko25 Jan 02 '14 at 22:50
  • I think you need to clarify in your own mind the distinction between your device's state changing and the comm port's state changing. They are very different things and it sounds like you are confusing them. As it stands now your question is not answerable. It might help you to draw a state diagram for the device (independent of the comm port) and then figure out how each state change affects the comm port. Also, "polling" your device should involve sending it a query string and receiving a reply, which would fall under the normal operation of the serial port. – Jim Garrison Jan 02 '14 at 22:58

1 Answers1

0

Thanks Jim-- you are absolutely right about just sending a query string and receiving a reply. Very simple! The answer for my question depends on the manufacturer and what string I needed to send to my device. Somehow I had overlooked this in the manual. Now I am able to "poll" the device to have its current state returned. Simple answer! Thanks for all the help.

gecko25
  • 71
  • 2
  • 11