Fazecast / jSerialComm

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

serialPort.openPort() has trouble opening the serial port #407

Closed dev-walter closed 2 years ago

dev-walter commented 2 years ago

Hello, I am working on an existing project that uses the jSerialComm library to establish a serial communication through bluetooth to a bluetooth module of a sensor system. Once the connection is up it works and is stable. The problem lies in the openPort() function to open the serial port. This function often returns false and I have to try again. Sometimes it is able to open the port after one try, sometimes it takes more than 10 tries. Now I know that there are a lot of variables that could cause this e.g. the bluetooth module on my sensorsystem. I just wanted to ask if anyone else has experienced this behaviour of the openPort() function when using the library for serial connection over bluetooth or if the problem is caused by other parts such as my bluetooth dongle or my bluetooth module.

NorbertSandor commented 2 years ago

I have seen similar problems as well when opening a connection to an Arduino device :(

hedgecrw commented 2 years ago

I haven't personally experienced this or had any other bugs reported about this issue. Could you have your code print out the results of getLastErrorLocation() and getLastErrorCode() immediately after your call to openPort() so I can see what native failure code is being returned?

dev-walter commented 2 years ago

open = this.sensor.openPort(); errorCode = sensor.getLastErrorCode(); errorLocation = sensor.getLastErrorLocation();

With this code I get an error code of 0 and an error location of -1. The documentation says that both functions only work while the port is open. Since the port can't get opened in the first place the return values make sense... Unless I implemented the functions wrong.

hedgecrw commented 2 years ago

Good catch on the documentation - the need for the port to be open is no longer necessary...I will update the documentation to avoid future confusion.

Regarding getting an error code of 0 and location of -1, are you sure that in this case, the openPort() call returned false? You'll only get an error code and location when openPort() fails.

Also, which OS are you using? I'm assuming Windows.

dev-walter commented 2 years ago

I tried it again in an minimal example and with my bluetooth dongle in two different USB-Ports. If the openPort() function returns true, both getLastErrorCode() and getLastErrorLocation() return 0 most of the time. Weirdly enough when I tested the openPort() in a loop to see how many times it failed both error functions returned an errorLocation of 390 and an errorCode of 121 when the openPort() function returned true... I only got errorCode and errorLocation of 0 when I ran the program with a single successfull attempt at opening the port.

If the openPort() function returns false I always get an errorCode of 0 and an errorLocation of -1.

Yes I use Windows 10.

hedgecrw commented 2 years ago

Ah okay, a couple things:

  1. Based on your results, you are using an out-of-date version of the library before the getLastErrorXXX() methods were able to return information about non-open ports (most likely <= 2.8.2). Please update to 2.9.0 and try again to get the correct error code.
  2. The error code you did receive in your looped retry version (code 121) is a Windows-based error for "semaphore timeout expired" which occurs for Bluetooth connections when an underlying driver times out trying to connect to the device. (Which from a quick web search looks to be a decently common problem when trying to access a Bluetooth device over serial, and has to do with the fact that Windows is only emulating a serial device in this case and not using the Bluetooth drivers directly.) If you retest using the latest library version, and this error code is still what is appearing on an openPort() failure, then it is a Windows/Bluetooth issue and not something this library can address other than to just retry until success as you are already doing.

Thanks!

dev-walter commented 2 years ago

I updated to the newest version and now get the error code and location when the opening of the port failed like it should! Sadly it's still the error code 121. I will look into the Windows error you mentioned. Thank you for the quick help!

hedgecrw commented 2 years ago

Okay, sorry about that! I'll add a line to the documentation for the openPort() function as well mentioning that this as a known issue and can be resolved by retrying the call for serial-over-BT.