I finaly could solve this problem only with port ownership in the java class. No way with socat, I think because of the framing problems.
I created an ownership handler like in
Managing Serial Port Ownership (in rxtx)
in jruby it looks like
class SerialOwnershipHandler
include CommPortOwnershipListener
def initialize serial_port
@serial_port = serial_port
@id = serial_port.id
@port_name = serial_port.port_name
end
def ownershipChange type
case type
when CommPortOwnershipListener::PORT_OWNED
p @id.to_s + " OwnershipListener: got the port for " + @port_name.to_s
when CommPortOwnershipListener::PORT_UNOWNED
p @id.to_s + " OwnershipListener: just lost it's port ownership for " + @port_name.to_s
when CommPortOwnershipListener::PORT_OWNERSHIP_REQUESTED
p @id.to_s + " OwnershipListener: someone is asking port ownership for " + @port_name.to_s
@serial_port.close
end
end...
In my serial port class initialize method I call
@serial_ownership_handler = SerialOwnershipHandler.new self ## implements CommPortOwnershipListener
@port_id.add_port_ownership_listener @serial_ownership_handler
Only disadvantage. You cannot have more than one open line. You have to always close one connection before opening anohter one (the one who requests the port)