use of gnu.io.UnsupportedCommOperationException 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
*/
@SuppressWarnings("rawtypes")
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", 2000);
} 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());
}
}
use of gnu.io.UnsupportedCommOperationException in project openhab1-addons by openhab.
the class IntRSModule method connect.
@Override
protected CommunicationChannel connect() {
logger.info("Connecting to INT-RS module at {}", this.port);
try {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(this.port);
SerialPort serialPort = portIdentifier.open("org.openhab.binding.satel", 2000);
serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.enableReceiveTimeout(this.getTimeout());
// RXTX serial port library causes high CPU load
// Start event listener, which will just sleep and slow down event
// loop
serialPort.addEventListener(new SerialPortEventListener() {
@Override
public void serialEvent(SerialPortEvent ev) {
try {
logger.trace("RXTX library CPU load workaround, sleep forever");
Thread.sleep(Long.MAX_VALUE);
} catch (InterruptedException e) {
}
}
});
serialPort.notifyOnDataAvailable(true);
logger.info("INT-RS module connected successfuly");
return new SerialCommunicationChannel(serialPort);
} catch (NoSuchPortException e) {
logger.error("Port {} does not exist", this.port);
} catch (PortInUseException e) {
logger.error("Port {} in use.", this.port);
} catch (UnsupportedCommOperationException e) {
logger.error("Unsupported comm operation on port {}.", this.port);
} catch (TooManyListenersException e) {
logger.error("Too many listeners on port {}.", this.port);
}
return null;
}
use of gnu.io.UnsupportedCommOperationException in project openhab1-addons by openhab.
the class EBusSerialConnector method connect.
/*
* (non-Javadoc)
*
* @see org.openhab.binding.ebus.connection.AbstractEBusConnector#connect()
*/
@Override
protected boolean connect() throws IOException {
try {
final CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(port);
if (portIdentifier != null) {
serialPort = portIdentifier.open("org.openhab.binding.ebus", 2000);
serialPort.setSerialPortParams(2400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
// set timeout 10 sec.
serialPort.disableReceiveThreshold();
serialPort.enableReceiveTimeout(10000);
// use event to let readByte wait until data is available, optimize cpu usage
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
outputStream = serialPort.getOutputStream();
inputStream = serialPort.getInputStream();
return super.connect();
}
} catch (NoSuchPortException e) {
logger.error("Unable to connect to serial port {}", port);
} catch (PortInUseException e) {
logger.error("Serial port {} is already in use", port);
} catch (UnsupportedCommOperationException e) {
logger.error(e.toString(), e);
} catch (TooManyListenersException e) {
logger.error("Too many listeners error!", e);
}
serialPort = null;
return false;
}
use of gnu.io.UnsupportedCommOperationException in project openhab1-addons by openhab.
the class SerialConnector method connect.
public void connect() throws EHealthException {
logger.debug("Going to open Serial connection on port '{}'", portName);
// 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(portName)) {
logger.debug("Serial port '{}' has been found.", portName);
portId = id;
}
}
}
if (portId != null) {
// initialize serial port
try {
serialPort = portId.open("openHAB", 2000);
inputStream = serialPort.getInputStream();
outputStream = serialPort.getOutputStream();
bReader = new BufferedReader(new InputStreamReader(inputStream));
bWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
serialPort.setSerialPortParams(BAUD, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
} catch (IOException e) {
throw new EHealthException(e);
} catch (PortInUseException e) {
throw new EHealthException(e);
} catch (UnsupportedCommOperationException e) {
throw new EHealthException(e);
} catch (TooManyListenersException e) {
throw new EHealthException(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 EHealthException("Serial port '" + portName + "' could not be found. Available ports are:\n" + sb.toString());
}
}
use of gnu.io.UnsupportedCommOperationException in project openhab1-addons by openhab.
the class ZWaveController method connect.
// Controller methods
/**
* Connects to the comm port and starts send and receive threads.
*
* @param serialPortName the port name to open
* @throws SerialInterfaceException when a connection error occurs.
*/
public void connect(final String serialPortName) throws SerialInterfaceException {
logger.info("Connecting to serial port {}", serialPortName);
try {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(serialPortName);
CommPort commPort = portIdentifier.open("org.openhab.binding.zwave", 2000);
this.serialPort = (SerialPort) commPort;
this.serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
this.serialPort.enableReceiveThreshold(1);
this.serialPort.enableReceiveTimeout(ZWAVE_RECEIVE_TIMEOUT);
this.receiveThread = new ZWaveReceiveThread();
this.receiveThread.start();
this.sendThread = new ZWaveSendThread();
this.sendThread.start();
this.inputThread = new ZWaveInputThread();
this.inputThread.start();
// RXTX serial port library causes high CPU load
// Start event listener, which will just sleep and slow down event loop
serialPort.addEventListener(this.receiveThread);
serialPort.notifyOnDataAvailable(true);
logger.info("Serial port is initialized");
} catch (NoSuchPortException e) {
logger.error("Serial Error: Port {} does not exist", serialPortName);
throw new SerialInterfaceException(String.format("Port %s does not exist", serialPortName), e);
} catch (PortInUseException e) {
logger.error("Serial Error: Port {} in use.", serialPortName);
throw new SerialInterfaceException(String.format("Port %s in use.", serialPortName), e);
} catch (UnsupportedCommOperationException e) {
logger.error("Serial Error: Unsupported comm operation on Port {}.", serialPortName);
throw new SerialInterfaceException(String.format("Unsupported comm operation on Port %s.", serialPortName), e);
} catch (TooManyListenersException e) {
logger.error("Serial Error: Too many listeners on Port {}.", serialPortName);
e.printStackTrace();
}
}
Aggregations