简体   繁体   中英

polling serial port in java to get current state of device

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 (ie 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){}
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM