use of gnu.io.PortInUseException in project openhab1-addons by openhab.
the class DavisBinding method openPort.
public void openPort() throws InitializationException {
CommPortIdentifier portIdentifier;
try {
portIdentifier = CommPortIdentifier.getPortIdentifier(port);
try {
serialPort = portIdentifier.open("openhab", 3000);
serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
// serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT);
serialPort.enableReceiveTimeout(100);
serialPort.enableReceiveThreshold(1);
inputStream = new DataInputStream(new BufferedInputStream(serialPort.getInputStream()));
outputStream = serialPort.getOutputStream();
logger.debug("port opened: " + port);
} catch (PortInUseException e) {
throw new InitializationException(e);
} catch (UnsupportedCommOperationException e) {
throw new InitializationException(e);
} catch (IOException e) {
throw new InitializationException(e);
}
} catch (NoSuchPortException e) {
StringBuilder sb = new StringBuilder();
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
CommPortIdentifier id = (CommPortIdentifier) portList.nextElement();
if (id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
sb.append(id.getName() + "\n");
}
}
throw new InitializationException("Serial port '" + port + "' could not be found. Available ports are:\n" + sb.toString());
}
}
use of gnu.io.PortInUseException in project openhab1-addons by openhab.
the class DSMRPort method open.
/**
* Opens the Operation System Serial Port
* <p>
* This method opens the port and set Serial Port parameters according to
* the DSMR specification. Since the specification is clear about these
* parameters there are not configurable.
* <p>
* If there are problem while opening the port, it is the responsibility of
* the calling method to handle this situation (and for example close the
* port again).
* <p>
* Opening an already open port is harmless. The method will return
* immediately
*
* @return true if opening was successful (or port was already open), false
* otherwise
*/
private boolean open() {
synchronized (portLock) {
// Sanity check
if (portState != PortState.CLOSED) {
return true;
}
try {
// Add non standard port names if not exists (fixes part of #4175)
if (!portExists(portName)) {
logger.warn("Port {} does not exists according to the system, we will still try to open it", portName);
}
// Opening Operating System Serial Port
logger.debug("Creating CommPortIdentifier");
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
logger.debug("Opening CommPortIdentifier");
CommPort commPort = portIdentifier.open("org.openhab.binding.dsmr", readTimeoutMSec);
logger.debug("Configure serial port");
serialPort = (SerialPort) commPort;
serialPort.enableReceiveThreshold(1);
serialPort.enableReceiveTimeout(readTimeoutMSec);
// Configure Serial Port based on specified port speed
logger.debug("Configure serial port parameters: {}", portSettings);
if (portSettings != null) {
serialPort.setSerialPortParams(portSettings.getBaudrate(), portSettings.getDataBits(), portSettings.getStopbits(), portSettings.getParity());
/* special settings for low speed port (checking reference here) */
if (portSettings == DSMRPortSettings.LOW_SPEED_SETTINGS) {
serialPort.setDTR(false);
serialPort.setRTS(true);
}
} else {
logger.error("Invalid port parameters, closing port:{}", portSettings);
return false;
}
} catch (NoSuchPortException nspe) {
logger.error("Could not open port: {}", portName, nspe);
return false;
} catch (PortInUseException piue) {
logger.error("Port already in use: {}", portName, piue);
return false;
} catch (UnsupportedCommOperationException ucoe) {
logger.error("Port does not support requested port settings " + "(invalid dsmr:portsettings parameter?): {}", portName, ucoe);
return false;
}
// SerialPort is ready, open the reader
logger.info("SerialPort opened successful");
try {
bis = new BufferedInputStream(serialPort.getInputStream());
} catch (IOException ioe) {
logger.error("Failed to get inputstream for serialPort. Closing port", ioe);
return false;
}
logger.info("DSMR Port opened successful");
return true;
}
}
use of gnu.io.PortInUseException in project openhab1-addons by openhab.
the class Connection method open.
/**
* Opens the serial port associated with this connection.
*
* @throws IOException
* if any kind of error occurs opening the serial port.
*/
public void open() throws IOException {
CommPortIdentifier portIdentifier;
try {
portIdentifier = CommPortIdentifier.getPortIdentifier(serialPortName);
} catch (NoSuchPortException e) {
throw new IOException("Serial port with given name does not exist", e);
}
if (portIdentifier.isCurrentlyOwned()) {
throw new IOException("Serial port is currently in use.");
}
// fixed issue as rxtx library originally used in j62056 does use
// different version of rxtx
// com port in their version is using gnu.io.CommPort
RXTXPort commPort;
try {
commPort = portIdentifier.open(this.getClass().getName(), 2000);
} catch (PortInUseException e) {
throw new IOException("Serial port is currently in use.", e);
}
if (!(commPort instanceof SerialPort)) {
commPort.close();
throw new IOException("The specified CommPort is not a serial port");
}
serialPort = commPort;
try {
os = new DataOutputStream(serialPort.getOutputStream());
is = new DataInputStream(serialPort.getInputStream());
} catch (IOException e) {
serialPort.close();
serialPort = null;
throw new IOException("Error getting input or output or input stream from serial port", e);
}
}
use of gnu.io.PortInUseException in project openhab1-addons by openhab.
the class SerialIOStream method open.
@Override
public boolean open() {
try {
updateSerialProperties(m_devName);
CommPortIdentifier ci = CommPortIdentifier.getPortIdentifier(m_devName);
CommPort cp = ci.open(m_appName, 1000);
if (cp instanceof SerialPort) {
m_port = (SerialPort) cp;
} else {
throw new IllegalStateException("unknown port type");
}
m_port.setSerialPortParams(m_speed, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
m_port.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
logger.debug("setting port speed to {}", m_speed);
m_port.disableReceiveFraming();
m_port.enableReceiveThreshold(1);
// m_port.disableReceiveTimeout();
m_port.enableReceiveTimeout(1000);
m_in = m_port.getInputStream();
m_out = m_port.getOutputStream();
logger.info("successfully opened port {}", m_devName);
return true;
} catch (IOException e) {
logger.error("cannot open port: {}, got IOException ", m_devName, e);
} catch (PortInUseException e) {
logger.error("cannot open port: {}, it is in use!", m_devName);
} catch (UnsupportedCommOperationException e) {
logger.error("got unsupported operation {} on port {}", e.getMessage(), m_devName);
} catch (NoSuchPortException e) {
logger.error("got no such port for {}", m_devName);
} catch (IllegalStateException e) {
logger.error("got unknown port type for {}", m_devName);
}
return false;
}
use of gnu.io.PortInUseException in project openhab1-addons by openhab.
the class SerialDevice method initialize.
/**
* Initialize this device and open the serial port
*
* @throws InitializationException
* if port can not be opened
*/
public void initialize() throws InitializationException {
// parse ports and if the default port is found, initialized the reader
Enumeration<?> portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
CommPortIdentifier id = (CommPortIdentifier) portList.nextElement();
if (id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (id.getName().equals(port)) {
logger.debug("Serial port '{}' has been found.", port);
portId = id;
}
}
}
if (portId != null) {
// initialize serial port
try {
serialPort = portId.open("openHAB-Smarthomatic", DEFAULT_PORT);
} catch (PortInUseException e) {
throw new InitializationException(e);
}
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {
throw new InitializationException(e);
}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {
throw new InitializationException(e);
}
// activate the DATA_AVAILABLE notifier
serialPort.notifyOnDataAvailable(true);
try {
// set port parameters
serialPort.setSerialPortParams(baud, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
throw new InitializationException(e);
}
try {
// get the output stream
outputStream = serialPort.getOutputStream();
} catch (IOException e) {
throw new InitializationException(e);
}
} else {
StringBuilder sb = new StringBuilder();
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
CommPortIdentifier id = (CommPortIdentifier) portList.nextElement();
if (id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
sb.append(id.getName() + "\n");
}
}
throw new InitializationException("Serial port '" + port + "' could not be found. Available ports are:\n" + sb.toString());
}
}
Aggregations