steveohara / j2mod

Enhanced Modbus library implemented in the Java programming language
Apache License 2.0
276 stars 110 forks source link

Serial port is left hanging in OS if it gets physically disconnected #144

Closed MartynasRim closed 11 months ago

MartynasRim commented 1 year ago

Expected Behavior

If serial port is disconnected - lib should listen to LISTENING_EVENT_PORT_DISCONNECTED event and call [closePort](https://fazecast.github.io/jSerialComm/javadoc/com/fazecast/jSerialComm/SerialPort.html#closePort()) to prevent hanging serial port reference. More info at - https://fazecast.github.io/jSerialComm/javadoc/com/fazecast/jSerialComm/SerialPort.html#getCommPorts()

Note that the array will also include any serial ports that your application currently has open, even if the devices attached to those ports become disconnected. As such, it is important that you always call [closePort()](https://fazecast.github.io/jSerialComm/javadoc/com/fazecast/jSerialComm/SerialPort.html#closePort()) on a SerialPort object if it becomes disconnected, which is detectable by inspecting the return values from the various read calls or by registering a SerialPortDataListener for the LISTENING_EVENT_PORT_DISCONNECTED event.

Actual Behavior

If serial port gets disconnected (physically) & connected again while we do have a connection established we will have to ports available in OS. For example /dev/ttyUSB0 & /dev/ttyUSB1.

Steps to Reproduce the Problem

  1. Check how many USB ports are available ls dev | grep ttyUSB
  2. Connect serial port
  3. Connect to port using ModbusSerialMaster
  4. Disconnect port physically and reattach it
  5. Check how many USB ports are available ls dev | grep ttyUSB - there should be 2 more ports allocated

Specifications

steveohara commented 11 months ago

Merged change into latest snapshot