Fazecast / jSerialComm

Platform-independent serial port access for Java
GNU Lesser General Public License v3.0
1.35k stars 287 forks source link

getListeningEvents() At the same time listening SerialPort.LISTENING_EVENT_DATA_AVAILABLE and SerialPort.LISTENING_EVENT_PORT_DISCONNECTED? #414

Closed SpringXL closed 2 years ago

SpringXL commented 2 years ago

Hello, your example only has public int getListeningEvents() { return SerialPort.LISTENING_EVENT_DATA_AVAILABLE; } A listener returns;Although the description can be multiple listening events, would like to ask how to achieve? Multiple attempts only trigger the first event condition ;I want to listen SerialPort.LISTENING_EVENT_DATA_AVAILABLE and SerialPort.LISTENING_EVENT_PORT_DISCONNECTED;

           @Override                                 //This returns the fixed listener, below serialEvent(SerialPortEvent event)Only this event is 
                                                             //triggered. Other event states are not,Consult the example scheme
            public int getListeningEvents() { return SerialPort.LISTENING_EVENT_DATA_AVAILABLE; }

            @Override
            public void serialEvent(SerialPortEvent event)
            {
                System.out.println(event.getEventType() != SerialPort.LISTENING_EVENT_DATA_AVAILABLE);

                if (event.getEventType() != SerialPort.LISTENING_EVENT_DATA_AVAILABLE) {
                    return;
                }
hedgecrw commented 2 years ago

To listen for multiple events, you simply OR them together. In your case:

return SerialPort.LISTENING_EVENT_DATA_AVAILABLE | SerialPort.LISTENING_EVENT_PORT_DISCONNECTED;