Fazecast / jSerialComm

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

Not able to reopen com port (orig issue#60) #240

Closed m2oswald closed 5 years ago

m2oswald commented 5 years ago

Hi there. I'm having a similar problem to the one described in issue#60. I'm using Java 8 and jSerialComm 2.5.1 I have a satellite communication device plugged into a serial port (COM10). I send a command every 30 seconds, and the device responds with an 'ok'. I know if the device gets unplugged because there are no more responses. Then I follow this sequence: 1) call serialPort.closePort (which returns true) 2) plug device back in 3) call SerialPort.getComPorts to get a new listing of available ports 4) call serialPort = SerialPort.getCommPort(portName). That seems to be successful 5) call serialPort.openPort I get a false.

If I restart the program, everything connects immediately. In your response to the OP in issue#60 you said that he would need to 're-enumerate the list of SerialPort objects'. Did you mean the getComPorts method? Or a different way to initialize the ports? Any help would be greatly appreciated. Thanks.

hedgecrw commented 5 years ago

Hey there, the method you describe should be working correctly, so there may be some underlying bug. Which OS are you using this on? And could you also try again with the most recent version 2.5.2? One thing to note, your methods 3 and 4 are both enumerating the same port, so no need to do both of them. If you know your port name, you can simply do line 4 which should work. Alternately, you can do line 3 and use whichever SerialPort object from the returned array corresponds to your device. As a sanity check, could you actually try to just use line 3 and make sure that your device is actually being returned/enumerated in that array once you've plugged it back in after a disconnect?

m2oswald commented 5 years ago

Thanks so much for your help. In the end, it was my problem - I had multiple threads trying to access the port, and one was blocking while the other was trying to reconnect. I had synchronized in the wrong place, but once I fixed that it was working fine again.