Fazecast / jSerialComm

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

[Re-opened] Random errors when opening ports. #560

Open Lyrei-cm opened 6 months ago

Lyrei-cm commented 6 months ago
          Closing as "no bug". Please re-open if the solution posted does not solve your issue.

Originally posted by @hedgecrw in https://github.com/Fazecast/jSerialComm/issues/538#issuecomment-2106239416


Thank you so much for your response, @hedgecrw!

Sorry, I got completely sidetracked by another issue and I haven't had the chance to update you on this.

The issue still occurs, but mostly for the first device that is connected (COM4 in this case).

Here's the detailed log:

2024-05-17 01:50:30,453 INFO [BGR1 (COM4)] m.LoggerUtils [LoggerUtils.java:21] Initializing serial connection on port COM4
2024-05-17 01:50:30,454 INFO [BGR1 (COM4)] m.LoggerUtils [LoggerUtils.java:21] Created BGR on port COM4 for lane 0.
2024-05-17 01:50:30,493 INFO [ConnectionThread (COM4)] m.LoggerUtils [LoggerUtils.java:21] Initializing connection to COM4
2024-05-17 01:50:30,564 ERROR [ConnectionThread (COM4)] m.LoggerUtils [LoggerUtils.java:33] Error opening port: COM4 | Is port busy? false | Last error: 5 | Last error location: 894
2024-05-17 01:50:30,564 INFO [ConnectionThread (COM4)] m.LoggerUtils [LoggerUtils.java:21] Failed to connect. Retrying in 5000 ms.
2024-05-17 01:50:30,844 INFO [CheckAndReconnectThread (COM4)] m.LoggerUtils [LoggerUtils.java:21] Reconnected to COM4
2024-05-17 01:50:31,367 INFO [EventThread] m.LoggerUtils [LoggerUtils.java:21] Connected to Gatewatch.
2024-05-17 01:50:32,424 INFO [BGR2 (COM5)] m.LoggerUtils [LoggerUtils.java:21] Initializing serial connection on port COM5
2024-05-17 01:50:32,426 INFO [BGR2 (COM5)] m.LoggerUtils [LoggerUtils.java:21] Created BGR on port COM5 for lane 1.
2024-05-17 01:50:32,428 INFO [ConnectionThread (COM5)] m.LoggerUtils [LoggerUtils.java:21] Initializing connection to COM5
2024-05-17 01:50:33,045 INFO [CheckAndReconnectThread (COM5)] m.LoggerUtils [LoggerUtils.java:21] Reconnected to COM5
2024-05-17 01:50:33,078 INFO [ConnectionThread (COM5)] m.LoggerUtils [LoggerUtils.java:21] Connected to COM5
2024-05-17 01:50:34,424 INFO [BGR3 (COM7)] m.LoggerUtils [LoggerUtils.java:21] Initializing serial connection on port COM7
2024-05-17 01:50:34,425 INFO [BGR3 (COM7)] m.LoggerUtils [LoggerUtils.java:21] Created BGR on port COM7 for lane 2.
2024-05-17 01:50:34,428 INFO [ConnectionThread (COM7)] m.LoggerUtils [LoggerUtils.java:21] Initializing connection to COM7
2024-05-17 01:50:34,888 INFO [ConnectionThread (COM7)] m.LoggerUtils [LoggerUtils.java:21] Connected to COM7
2024-05-17 01:50:35,093 DEBUG [Thread-24] m.LoggerUtils [LoggerUtils.java:25] 02 30 80 41 52 3B 58 53   55 32 4D 47 4F 4B 23 50      .0.AR;XSU2MGOK#P
FF 03 
2024-05-17 01:50:35,848 ERROR [CheckAndReconnectThread (COM4)] m.LoggerUtils [LoggerUtils.java:33] Device disconnected. Attempting to reconnect...
2024-05-17 01:50:35,849 ERROR [CheckAndReconnectThread (COM4)] m.LoggerUtils [LoggerUtils.java:33] Error opening port: COM4 | Is port busy? false | Last error: 2 | Last error location: 880
2024-05-17 01:50:35,850 ERROR [CheckAndReconnectThread (COM4)] m.LoggerUtils [LoggerUtils.java:33] Reconnection failed. Retrying in 5000 ms.
2024-05-17 01:50:36,939 DEBUG [Thread-27] m.LoggerUtils [LoggerUtils.java:25] 02 30 80 41 52 3B 43 55   54 4D 47 4F 4B 23 50 FF      .0.AR;CUTMGOK#P.
03 
2024-05-17 01:50:40,851 ERROR [CheckAndReconnectThread (COM4)] m.LoggerUtils [LoggerUtils.java:33] Error opening port: COM4 | Is port busy? false | Last error: 2 | Last error location: 880
2024-05-17 01:50:40,852 ERROR [CheckAndReconnectThread (COM4)] m.LoggerUtils [LoggerUtils.java:33] Reconnection failed. Retrying in 5000 ms.
2024-05-17 01:50:45,853 ERROR [CheckAndReconnectThread (COM4)] m.LoggerUtils [LoggerUtils.java:33] Error opening port: COM4 | Is port busy? false | Last error: 2 | Last error location: 880
2024-05-17 01:50:45,853 ERROR [CheckAndReconnectThread (COM4)] m.LoggerUtils [LoggerUtils.java:33] Reconnection failed. Retrying in 5000 ms.
2024-05-17 01:50:46,269 INFO [JavaFX Application Thread] m.LoggerUtils [LoggerUtils.java:21] Exit requested...
2024-05-17 01:50:46,272 INFO [JavaFX Application Thread] m.LoggerUtils [LoggerUtils.java:21] Closing BGRs...
2024-05-17 01:50:46,276 INFO [JavaFX Application Thread] m.LoggerUtils [LoggerUtils.java:21] Disconnecting from COM4...
2024-05-17 01:50:46,281 ERROR [CheckAndReconnectThread (COM4)] m.LoggerUtils [LoggerUtils.java:33] Check and reconnect thread interrupted: sleep interrupted
2024-05-17 01:50:46,282 DEBUG [JavaFX Application Thread] m.LoggerUtils [LoggerUtils.java:25] Removing data listener
2024-05-17 01:50:46,283 DEBUG [JavaFX Application Thread] m.LoggerUtils [LoggerUtils.java:25] Attempting to close port: true
2024-05-17 01:50:46,284 INFO [JavaFX Application Thread] m.LoggerUtils [LoggerUtils.java:21] Resources closed for COM4.
2024-05-17 01:50:46,286 INFO [JavaFX Application Thread] m.LoggerUtils [LoggerUtils.java:21] Disconnected from COM4
2024-05-17 01:50:46,288 INFO [JavaFX Application Thread] m.LoggerUtils [LoggerUtils.java:21] Disconnecting from COM5...
2024-05-17 01:50:46,288 ERROR [CheckAndReconnectThread (COM5)] m.LoggerUtils [LoggerUtils.java:33] Check and reconnect thread interrupted: sleep interrupted
2024-05-17 01:50:46,289 DEBUG [JavaFX Application Thread] m.LoggerUtils [LoggerUtils.java:25] Removing data listener
2024-05-17 01:50:46,474 DEBUG [JavaFX Application Thread] m.LoggerUtils [LoggerUtils.java:25] Attempting to close port: true
2024-05-17 01:50:46,474 INFO [JavaFX Application Thread] m.LoggerUtils [LoggerUtils.java:21] Resources closed for COM5.
2024-05-17 01:50:46,475 INFO [JavaFX Application Thread] m.LoggerUtils [LoggerUtils.java:21] Disconnected from COM5
2024-05-17 01:50:46,475 INFO [JavaFX Application Thread] m.LoggerUtils [LoggerUtils.java:21] Disconnecting from COM7...
2024-05-17 01:50:46,476 ERROR [CheckAndReconnectThread (COM7)] m.LoggerUtils [LoggerUtils.java:33] Check and reconnect thread interrupted: sleep interrupted
2024-05-17 01:50:46,477 DEBUG [JavaFX Application Thread] m.LoggerUtils [LoggerUtils.java:25] Removing data listener
2024-05-17 01:50:46,654 DEBUG [JavaFX Application Thread] m.LoggerUtils [LoggerUtils.java:25] Attempting to close port: true
2024-05-17 01:50:46,655 INFO [JavaFX Application Thread] m.LoggerUtils [LoggerUtils.java:21] Resources closed for COM7.
2024-05-17 01:50:46,655 INFO [JavaFX Application Thread] m.LoggerUtils [LoggerUtils.java:21] Disconnected from COM7
2024-05-17 01:50:46,656 INFO [JavaFX Application Thread] m.LoggerUtils [LoggerUtils.java:21] All BGRs disconnected.
2024-05-17 01:50:46,659 INFO [Thread-30] m.LoggerUtils [LoggerUtils.java:21] Exit successful.

In my connect() function I added the following (within the arrows):

public void connect() {
        try {
            --------- > SerialPort tempPort = SerialPort.getCommPort(_portName);
            if (tempPort.isOpen()) {
                tempPort.removeDataListener();
                tempPort.closePort();
            } < ---------------
            if (_serialPort == null)
                _serialPort = SerialPort.getCommPort(_portName);
            _serialPort.allowElevatedPermissionsRequest();
            if (!_serialPort.openPort())
            {
                LoggerUtils.error("Error opening port: "+_portName + " | Is port busy? " + _serialPort.isOpen() + " | Last error: " + _serialPort.getLastErrorCode() + " | Last error location: " + _serialPort.getLastErrorLocation());
                return;
            }
            _serialPort.setBaudRate(DATA_RATE);
            _serialPort.setNumDataBits(8);
            _serialPort.setNumStopBits(1);
            _serialPort.setParity(SerialPort.NO_PARITY);
            _serialPort.setFlowControl(SerialPort.FLOW_CONTROL_DISABLED);
            _serialPort.setDTR();
            _serialPort.setRTS();

            _serialPort.addDataListener(this);
            isConnected.set(true);
        } catch (SerialPortInvalidPortException e) {
            LoggerUtils.error("Invalid port: "+_portName);
        }
    }

I did not use closeResources() as the _serialPort object is always null before being assigned in the connect() function.

The devices are disconnected in a sequence using a CountDownLatch so as to ensure that every single one is properly shutdown before the app is closed.

Any ideas?