From e637ed0e4152f341a47ba2599283e72e9c975b84 Mon Sep 17 00:00:00 2001 From: Denis Denisov Date: Tue, 16 Mar 2021 23:05:52 +0300 Subject: [PATCH] Added request for full list of CommPortIdentifiers if we're unable to get identifer by name. Signed-off-by: Denis Denisov --- .../java/net/wimpi/modbus/net/SerialConnection.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/wimpi/modbus/net/SerialConnection.java b/src/main/java/net/wimpi/modbus/net/SerialConnection.java index 87cd62e..eedf4f0 100755 --- a/src/main/java/net/wimpi/modbus/net/SerialConnection.java +++ b/src/main/java/net/wimpi/modbus/net/SerialConnection.java @@ -56,6 +56,7 @@ public class SerialConnection implements SerialPortEventListener, ModbusSlaveCon private SerialPort m_SerialPort; private boolean m_Open; private InputStream m_SerialIn; + private boolean m_PortsRequested; /** * Creates a SerialConnection object and initializes variables passed in @@ -66,6 +67,7 @@ public class SerialConnection implements SerialPortEventListener, ModbusSlaveCon public SerialConnection(SerialParameters parameters) { m_Parameters = parameters; m_Open = false; + m_PortsRequested = false; }// constructor /** @@ -111,7 +113,15 @@ public void open() throws Exception { // 1. obtain a CommPortIdentifier instance try { - m_PortIdentifyer = CommPortIdentifier.getPortIdentifier(m_Parameters.getPortName()); + try { + m_PortIdentifyer = CommPortIdentifier.getPortIdentifier(m_Parameters.getPortName()); + } catch (NoSuchPortException e) { + if (!m_PortsRequested) { + CommPortIdentifier.getPortIdentifiers(); + } else { + throw e; + } + } } catch (NoSuchPortException e) { final String errMsg = "Could not get port identifier, maybe insufficient permissions. " + e.getMessage(); logger.debug("Could not get port identifier, maybe insufficient permissions. {}: {}",