Fazecast / jSerialComm

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

sometimes closing port takes time and subsequent open fails #517

Closed VaibhavDhopate closed 8 months ago

VaibhavDhopate commented 1 year ago

USB device randomly close connection takse time approx 5 min to get out from commPort.closePort() -- method and after that subsequent port open request fails after reboot conencted device it again works proeprly, As usb communication is critical piece of funcationality and rebooting device is not feasible in every situation.

jSerialComm verison : 2.9.2 Platform : windows

hedgecrw commented 1 year ago

Please use the most recent release version (v2.10.3) and report back if this is still an issue.

VaibhavDhopate commented 1 year ago

thank you will check with latest version and will report. we are suspeting frequently open close serial port conenction causing issue did you came accrss any such scenario ? and what is your remondation in such implemntation

VaibhavDhopate commented 1 year ago

@hedgecrw updated jserailcomm version 2.10.3 still it is happing jserailcomm : 2.10.3 platfomr : windows (to resolve this need to reboot/unplug the usb connted device

hedgecrw commented 1 year ago

Circling back to this. Are you using any data listeners in your project? If so, could you post the code for your serialEvent() callback function?

karborator commented 11 months ago

I'm not sure if its the same case but I will explain it here and you will decide if I have to open a separate issue:

When I switch to Windows 10 it's working each time.

Same with the latest version of jSerialComm 2.10.4

` public class PaymentDoneListenerImpl implements PaymentDoneListener {

private static final Logger LOGGER = LoggerFactory.getLogger(PaymentDoneListenerImpl.class);
private static final int PAYMENT_DONE = 67;
private static final int BUSY = 64;

private final SerialPort serialComPort;
private boolean paymentDone = false;

public PaymentDoneListenerImpl(SerialPort serialComPort) {
    this.serialComPort = serialComPort;
}

@Override
public int getListeningEvents() {
    return SerialPort.LISTENING_EVENT_DATA_AVAILABLE;
}

@Override
public void serialEvent(SerialPortEvent event) {
    newDataFromEventIfPossible(event).ifPresent(newData -> {
        flagIfBusy(newData);
        flagIfPaymentDone(newData);
    });
}

public boolean isPaymentDone() {
    return paymentDone;
}

public void finishPayment(){
    paymentDone = false;
}

private Optional<byte[]> newDataFromEventIfPossible(SerialPortEvent event) {
    if (event.getEventType() != SerialPort.LISTENING_EVENT_DATA_AVAILABLE) {
        return Optional.empty();
    }
    return Optional.of(newData());
}

private byte[] newData() {
    byte[] newData = new byte[serialComPort.bytesAvailable()];
    LOGGER.info("Read {}, bytes: {}", serialComPort.readBytes(newData, newData.length), Arrays.toString(newData));
    return newData;
}

private void flagIfPaymentDone(byte[] newData) {
    if (newData[1] == PAYMENT_DONE) {
        paymentDone = true;
        LOGGER.info("Payment done");
    }
}

private void flagIfBusy(byte[] newData) {
    if (newData[1] == BUSY) {
        paymentDone = false;
        LOGGER.info("Busy ...");
    }
}

}

`

AwesomeYotta commented 9 months ago

For those who meet this issue for ch340 chip on Windows 11, it's the built in driver problem of OS, close port just doesn't work, the OS will return error code 31 afterwards until re-plug the port.

Installing official driver provided by chip manufacture finally solved this issue. Keywords: CH341SER.EXE

hedgecrw commented 8 months ago

Great, thanks for the update. This will definitely help others in the future!