use of gnu.io.UnsupportedCommOperationException in project openhab1-addons by openhab.
the class PrimareSerialConnector method connectSerial.
private void connectSerial() throws Exception {
logger.debug("Initializing serial port {}", serialPortName);
try {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(serialPortName);
CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
serialPort = (SerialPort) commPort;
try {
serialPort.setSerialPortParams(4800, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.enableReceiveThreshold(1);
serialPort.disableReceiveTimeout();
} catch (UnsupportedCommOperationException unimportant) {
// We might have a perfectly usable PTY even if above operations are unsupported
}
;
inStream = new DataInputStream(serialPort.getInputStream());
outStream = serialPort.getOutputStream();
outStream.flush();
if (inStream.markSupported()) {
inStream.reset();
}
logger.debug("Starting DataListener for {}", PrimareSerialConnector.this.toString());
dataListener = new DataListener();
dataListener.start();
logger.debug("Starting DataListener for {}", PrimareSerialConnector.this.toString());
sendInitMessages();
} catch (NoSuchPortException e) {
logger.error("No such port: {}", serialPortName);
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
if (portList.hasMoreElements()) {
StringBuilder sb = new StringBuilder();
while (portList.hasMoreElements()) {
CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
sb.append(String.format("%s ", portId.getName()));
}
logger.error("The following communications ports are available: {}", sb.toString().trim());
} else {
logger.error("There are no communications ports available");
}
logger.error("You may consider OpenHAB startup parameter [ -Dgnu.io.rxtx.SerialPorts={} ]", serialPortName);
throw e;
}
}
use of gnu.io.UnsupportedCommOperationException in project openhab1-addons by openhab.
the class CULSerialHandlerImpl method openHardware.
@Override
protected void openHardware() throws CULDeviceException {
String deviceName = config.getDeviceAddress();
logger.debug("Opening serial CUL connection for {}", deviceName);
try {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(deviceName);
if (portIdentifier.isCurrentlyOwned()) {
throw new CULDeviceException("The port " + deviceName + " is currenty used by " + portIdentifier.getCurrentOwner());
}
CommPort port = portIdentifier.open(this.getClass().getName(), 2000);
if (!(port instanceof SerialPort)) {
throw new CULDeviceException("The device " + deviceName + " is not a serial port");
}
serialPort = (SerialPort) port;
serialPort.setSerialPortParams(config.getBaudRate(), SerialPort.DATABITS_8, SerialPort.STOPBITS_1, config.getParityMode());
InputStream is = serialPort.getInputStream();
OutputStream os = serialPort.getOutputStream();
synchronized (serialPort) {
br = new BufferedReader(new InputStreamReader(is));
bw = new BufferedWriter(new OutputStreamWriter(os));
}
serialPort.notifyOnDataAvailable(true);
logger.debug("Adding serial port event listener");
serialPort.addEventListener(this);
} catch (NoSuchPortException e) {
throw new CULDeviceException(e);
} catch (PortInUseException e) {
throw new CULDeviceException(e);
} catch (UnsupportedCommOperationException e) {
throw new CULDeviceException(e);
} catch (IOException e) {
throw new CULDeviceException(e);
} catch (TooManyListenersException e) {
throw new CULDeviceException(e);
}
}
use of gnu.io.UnsupportedCommOperationException in project openhab1-addons by openhab.
the class ComfoAirConnector method open.
/**
* Open and initialize a serial port.
*
* @param portName
* e.g. /dev/ttyS0
* @param listener
* the listener which is informed after a successful response
* read
* @throws InitializationException
*/
public void open(String portName) throws InitializationException {
logger.debug("Open ComfoAir connection");
port = portName;
CommPortIdentifier portIdentifier;
try {
portIdentifier = CommPortIdentifier.getPortIdentifier(port);
try {
serialPort = portIdentifier.open("openhab", 3000);
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.enableReceiveThreshold(1);
serialPort.enableReceiveTimeout(1000);
// RXTX serial port library causes high CPU load
// Start event listener, which will just sleep and slow down event loop
serialPort.addEventListener(new CPUWorkaroundThread());
serialPort.notifyOnDataAvailable(true);
inputStream = new DataInputStream(new BufferedInputStream(serialPort.getInputStream()));
outputStream = serialPort.getOutputStream();
ComfoAirCommand command = ComfoAirCommandType.getChangeCommand(ComfoAirCommandType.ACTIVATE.key, new DecimalType(1));
sendCommand(command);
} catch (PortInUseException e) {
throw new InitializationException(e);
} catch (UnsupportedCommOperationException e) {
throw new InitializationException(e);
} catch (IOException e) {
throw new InitializationException(e);
} catch (TooManyListenersException e) {
throw new InitializationException(e);
}
} catch (NoSuchPortException e) {
StringBuilder sb = new StringBuilder();
@SuppressWarnings("rawtypes") 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.UnsupportedCommOperationException in project openhab1-addons by openhab.
the class AlarmDecoderBinding method connect.
private synchronized void connect() {
try {
// make sure we have disconnected
disconnect();
markAllItemsUnupdated();
if (m_tcpHostName != null && m_tcpPort > 0) {
m_socket = new Socket(m_tcpHostName, m_tcpPort);
m_reader = new BufferedReader(new InputStreamReader(m_socket.getInputStream()));
m_writer = new BufferedWriter(new OutputStreamWriter(m_socket.getOutputStream()));
logger.info("connected to {}:{}", m_tcpHostName, m_tcpPort);
startMsgReader();
} else if (this.m_serialDeviceName != null) {
/*
* by default, RXTX searches only devices /dev/ttyS* and
* /dev/ttyUSB*, and will so not find symlinks. The
* setProperty() call below helps
*/
updateSerialProperties(m_serialDeviceName);
CommPortIdentifier ci = CommPortIdentifier.getPortIdentifier(m_serialDeviceName);
CommPort cp = ci.open("openhabalarmdecoder", 10000);
if (cp == null) {
throw new IllegalStateException("cannot open serial port!");
}
if (cp instanceof SerialPort) {
m_port = (SerialPort) cp;
} else {
throw new IllegalStateException("unknown port type");
}
m_port.setSerialPortParams(m_portSpeed, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
m_port.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT);
m_port.disableReceiveFraming();
m_port.disableReceiveThreshold();
m_reader = new BufferedReader(new InputStreamReader(m_port.getInputStream()));
m_writer = new BufferedWriter(new OutputStreamWriter(m_port.getOutputStream()));
logger.info("connected to serial port: {}", m_serialDeviceName);
startMsgReader();
} else {
logger.warn("alarmdecoder hostname or port not configured!");
}
} catch (PortInUseException e) {
logger.error("cannot open serial port: {}, it is in use!", m_serialDeviceName);
} catch (UnsupportedCommOperationException e) {
logger.error("got unsupported operation {} on port {}", e.getMessage(), m_serialDeviceName);
} catch (NoSuchPortException e) {
logger.error("got no such port for {}", m_serialDeviceName);
} catch (IllegalStateException e) {
logger.error("got unknown port type for {}", m_serialDeviceName);
} catch (UnknownHostException e) {
logger.error("unknown host name :{}: ", m_tcpHostName, e);
} catch (IOException e) {
logger.error("cannot open connection to {}", m_connectString);
}
}
use of gnu.io.UnsupportedCommOperationException in project openhab1-addons by openhab.
the class SerialWR3223Connector method connect.
/**
* Connect to WR2332 over serial port.
*
* @param port
* @param baud
* @throws IOException
*/
public void connect(String port, int baud) throws IOException {
// parse ports and if the default port is found, initialized the reader
CommPortIdentifier portId = null;
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
CommPortIdentifier id = (CommPortIdentifier) portList.nextElement();
if (id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (id.getName().equals(port)) {
portId = id;
}
}
}
if (portId != null) {
// initialize serial port
try {
serialPort = portId.open("wr3223", 2000);
} catch (PortInUseException e) {
throw new IOException("Serial port '" + port + "' is already in use.", e);
}
try {
// set port parameters
serialPort.setSerialPortParams(baud, SerialPort.DATABITS_7, SerialPort.STOPBITS_1, SerialPort.PARITY_EVEN);
} catch (UnsupportedCommOperationException e) {
throw new IOException("Serial port '" + port + "' doesn't support the configuration 7 data bit, 1 stop bit and parity even.", e);
}
if (!serialPort.isReceiveTimeoutEnabled()) {
try {
if (logger.isDebugEnabled()) {
logger.debug("Add a receive timeout of 2000ms.");
}
serialPort.enableReceiveTimeout(2000);
} catch (UnsupportedCommOperationException e) {
logger.warn("Error by adding receive timeout.", e);
}
}
DataInputStream inputStream = new DataInputStream(serialPort.getInputStream());
DataOutputStream outputStream = new DataOutputStream(serialPort.getOutputStream());
connect(inputStream, outputStream);
} 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 IOException("Serial port '" + port + "' could not be found. Available ports are:\n" + sb.toString());
}
}
Aggregations