Skip to content

Commit

Permalink
Improved Javadoc for SerialPort.isOpen() to reduce confusion when use…
Browse files Browse the repository at this point in the history
…d with USB serial adapters and Arduino/esp32 boards.
  • Loading branch information
jskubick committed Feb 21, 2024
1 parent d2f580f commit e8dc58a
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/main/java/com/fazecast/jSerialComm/SerialPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,31 @@ public final boolean closePort()

/**
* Returns whether the port is currently open and available for communication.
* <p></p><b>Warning!</b> This method alone <i>might not</i> reliably indicate the unplugging of a USB serial port,
* including USB ports on popular microcontrollers like Arduino and ESP32.
* (ie, it'll return <i>true</i> after you open the port, then continue to return <i>true</i> even after the interface
* has been unplugged from the USB port).
* In order to detect the port's unplugging, add the following code to your program after opening the SerialPort:
* <pre>
* port.addDataListener(new SerialPortDataListener() {
* // (atOverride annotations omitted to avoid confusing Javadoc generator, but necessary in actual use)
* public int getListeningEvents() {
* return SerialPort.LISTENING_EVENT_PORT_DISCONNECTED;
* }
* public void serialEvent(SerialPortEvent serialPortEvent) {
* port.closePort();
* }
* });
* </pre>
* Once the port has been closed by the event handler, isOpen() will return false, and you can periodically
* attempt to reopen it within your event loop without creating a new SerialPort object:
* <pre>
* if (port.isOpen() == false)
* port.open();
* </pre>
* Note that once a USB serial port has been unplugged and reconnected, it's unlikely (certain?) to
* not work again until the SerialPort object's close() and open() methods have both been called to
* re-establish the connection.
*
* @return Whether the port is opened.
*/
Expand Down

0 comments on commit e8dc58a

Please sign in to comment.