I have the same problem: I tried to find out when the device was disconnected from the USB port. Then I found out that every time I disconnect the device from the USB port, it gets a java.io.IOException from serialEvent. Take a look at my serialEvent code below:
@Override public void serialEvent(SerialPortEvent evt) { if(this.getConectado()){ if (evt.getEventType() == SerialPortEvent.DATA_AVAILABLE) { try { byte singleData = (byte)input.read(); if (singleData == START_DELIMITER) { singleData = (byte)input.read(); if(singleData == (byte)0x00) { singleData = (byte)input.read();
Even if I do not receive data at this moment, I still get java.io.IOException; there is an exception trace here:
Jun 21, 2014 10:57:19 AM inovale.serial.Comunicador serialEvent SEVERE: null java.io.IOException: No error in readByte at gnu.io.RXTXPort.readByte(Native Method) at gnu.io.RXTXPort$SerialInputStream.read(RXTXPort.java:1250) at inovale.serial.Comunicador.serialEvent(Comunicador.java:336) at gnu.io.RXTXPort.sendEvent(RXTXPort.java:732) at gnu.io.RXTXPort.eventLoop(Native Method) at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575)
The only events I use are:
serialPort.addEventListener(this); serialPort.notifyOnDataAvailable(true);
So I see to detect a disconnect event (physically).
source share