use of gnu.io.CommPort 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.CommPort 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.CommPort in project openhab1-addons by openhab.
the class PowerMaxSerialConnector method open.
/**
* {@inheritDoc}
**/
@Override
public void open() {
logger.debug("open(): Opening Serial Connection");
try {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(serialPortName);
CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(baudRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.enableReceiveThreshold(1);
serialPort.disableReceiveTimeout();
setInput(serialPort.getInputStream());
setOutput(serialPort.getOutputStream());
getOutput().flush();
if (getInput().markSupported()) {
getInput().reset();
}
setReaderThread(new SerialReaderThread(getInput(), this));
getReaderThread().start();
setConnected(true);
} catch (NoSuchPortException noSuchPortException) {
logger.debug("open(): No Such Port Exception: {}", noSuchPortException.getMessage());
setConnected(false);
} catch (PortInUseException portInUseException) {
logger.debug("open(): Port in Use Exception: {}", portInUseException.getMessage());
setConnected(false);
} catch (UnsupportedCommOperationException unsupportedCommOperationException) {
logger.debug("open(): Unsupported Comm Operation Exception: {}", unsupportedCommOperationException.getMessage());
setConnected(false);
} catch (UnsupportedEncodingException unsupportedEncodingException) {
logger.debug("open(): Unsupported Encoding Exception: {}", unsupportedEncodingException.getMessage());
setConnected(false);
} catch (IOException ioException) {
logger.debug("open(): IO Exception: ", ioException.getMessage());
setConnected(false);
}
}
use of gnu.io.CommPort in project openhab1-addons by openhab.
the class SwegonVentilationSerialConnector method connect.
@Override
public void connect() throws SwegonVentilationException {
try {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(BAUDRATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
in = serialPort.getInputStream();
logger.debug("Swegon ventilation Serial Port message listener started");
} catch (Exception e) {
throw new SwegonVentilationException(e);
}
}
use of gnu.io.CommPort 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;
}
Aggregations