2 * Copyright (c) 2010-2022 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.rotel.internal.communication;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.OutputStream;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.rotel.internal.RotelException;
23 import org.openhab.binding.rotel.internal.RotelModel;
24 import org.openhab.core.io.transport.serial.PortInUseException;
25 import org.openhab.core.io.transport.serial.SerialPort;
26 import org.openhab.core.io.transport.serial.SerialPortIdentifier;
27 import org.openhab.core.io.transport.serial.SerialPortManager;
28 import org.openhab.core.io.transport.serial.UnsupportedCommOperationException;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Class for communicating with the Rotel device through a serial connection
35 * @author Laurent Garnier - Initial contribution
38 public class RotelSerialConnector extends RotelConnector {
40 private final Logger logger = LoggerFactory.getLogger(RotelSerialConnector.class);
42 private String serialPortName;
43 private SerialPortManager serialPortManager;
45 private @Nullable SerialPort serialPort;
50 * @param serialPortManager the serial port manager
51 * @param serialPortName the serial port name to be used
52 * @param model the projector model in use
53 * @param protocol the protocol to be used
54 * @param readerThreadName the name of thread to be created
56 public RotelSerialConnector(SerialPortManager serialPortManager, String serialPortName, RotelModel model,
57 RotelProtocol protocol, Map<RotelSource, String> sourcesLabels, String readerThreadName) {
58 super(model, protocol, sourcesLabels, false, readerThreadName);
60 this.serialPortManager = serialPortManager;
61 this.serialPortName = serialPortName;
65 public synchronized void open() throws RotelException {
66 logger.debug("Opening serial connection on port {}", serialPortName);
68 SerialPortIdentifier portIdentifier = serialPortManager.getIdentifier(serialPortName);
69 if (portIdentifier == null) {
71 throw new RotelException("Opening serial connection failed: no port " + serialPortName);
74 SerialPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
76 commPort.setSerialPortParams(getModel().getBaudRate(), SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
77 SerialPort.PARITY_NONE);
78 commPort.enableReceiveThreshold(1);
79 commPort.enableReceiveTimeout(100);
80 commPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
82 InputStream dataIn = commPort.getInputStream();
83 OutputStream dataOut = commPort.getOutputStream();
85 if (dataOut != null) {
88 if (dataIn != null && dataIn.markSupported()) {
91 } catch (IOException e) {
95 Thread thread = new RotelReaderThread(this, readerThreadName);
96 setReaderThread(thread);
99 this.serialPort = commPort;
100 this.dataIn = dataIn;
101 this.dataOut = dataOut;
105 logger.debug("Serial connection opened");
106 } catch (PortInUseException | UnsupportedCommOperationException | IOException e) {
108 throw new RotelException("Opening serial connection failed", e);
113 public synchronized void close() {
114 logger.debug("Closing serial connection");
115 SerialPort serialPort = this.serialPort;
116 if (serialPort != null) {
117 serialPort.removeEventListener();
120 if (serialPort != null) {
122 this.serialPort = null;
125 logger.debug("Serial connection closed");