2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.rfxcom.internal.connector;
15 import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.MAX_RFXCOM_MESSAGE_LEN;
17 import java.io.IOException;
18 import java.io.OutputStream;
19 import java.util.TooManyListenersException;
21 import org.openhab.binding.rfxcom.internal.config.RFXComBridgeConfiguration;
22 import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
23 import org.openhab.core.io.transport.serial.PortInUseException;
24 import org.openhab.core.io.transport.serial.SerialPort;
25 import org.openhab.core.io.transport.serial.SerialPortEvent;
26 import org.openhab.core.io.transport.serial.SerialPortEventListener;
27 import org.openhab.core.io.transport.serial.SerialPortIdentifier;
28 import org.openhab.core.io.transport.serial.SerialPortManager;
29 import org.openhab.core.io.transport.serial.UnsupportedCommOperationException;
30 import org.openhab.core.util.HexUtils;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * RFXCOM connector for serial port communication.
37 * @author Pauli Anttila - Initial contribution
39 public class RFXComSerialConnector extends RFXComBaseConnector implements SerialPortEventListener {
40 private final Logger logger = LoggerFactory.getLogger(RFXComSerialConnector.class);
42 private OutputStream out;
43 private SerialPort serialPort;
44 private final SerialPortManager serialPortManager;
46 private final String readerThreadName;
47 private Thread readerThread;
49 public RFXComSerialConnector(SerialPortManager serialPortManager, String readerThreadName) {
51 this.serialPortManager = serialPortManager;
52 this.readerThreadName = readerThreadName;
56 public void connect(RFXComBridgeConfiguration device)
57 throws RFXComException, PortInUseException, UnsupportedCommOperationException, IOException {
58 SerialPortIdentifier portIdentifier = serialPortManager.getIdentifier(device.serialPort);
59 if (portIdentifier == null) {
60 logger.debug("No serial port {}", device.serialPort);
61 throw new RFXComException("No serial port " + device.serialPort);
64 SerialPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
66 serialPort = commPort;
67 serialPort.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
68 serialPort.enableReceiveThreshold(MAX_RFXCOM_MESSAGE_LEN);
69 serialPort.enableReceiveTimeout(100); // In ms. Small values mean faster shutdown but more cpu usage.
71 in = serialPort.getInputStream();
72 out = serialPort.getOutputStream();
75 if (in.markSupported()) {
79 // RXTX serial port library causes high CPU load
80 // Start event listener, which will just sleep and slow down event loop
82 serialPort.addEventListener(this);
83 serialPort.notifyOnDataAvailable(true);
84 logger.debug("Serial port event listener started");
85 } catch (TooManyListenersException e) {
88 readerThread = new RFXComStreamReader(this, readerThreadName);
93 public void disconnect() {
94 logger.debug("Disconnecting");
96 if (serialPort != null) {
97 serialPort.removeEventListener();
98 logger.debug("Serial port event listener stopped");
101 if (readerThread != null) {
102 logger.debug("Interrupt serial listener");
103 readerThread.interrupt();
106 } catch (InterruptedException e) {
111 logger.debug("Close serial out stream");
114 } catch (IOException e) {
115 logger.debug("Error while closing the out stream: {}", e.getMessage());
119 logger.debug("Close serial in stream");
122 } catch (IOException e) {
123 logger.debug("Error while closing the in stream: {}", e.getMessage());
127 if (serialPort != null) {
128 logger.debug("Close serial port");
137 logger.debug("Closed");
141 public void sendMessage(byte[] data) throws IOException {
143 throw new IOException("Not connected sending messages is not possible");
146 if (logger.isTraceEnabled()) {
147 logger.trace("Send data (len={}): {}", data.length, HexUtils.bytesToHex(data));
155 public void serialEvent(SerialPortEvent arg0) {
158 * See more details from
159 * https://github.com/NeuronRobotics/nrjavaserial/issues/22
161 logger.trace("RXTX library CPU load workaround, sleep forever");
162 Thread.sleep(Long.MAX_VALUE);
163 } catch (InterruptedException ignore) {