2 * Copyright (c) 2010-2021 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;
18 import java.io.UnsupportedEncodingException;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.rotel.internal.RotelException;
24 import org.openhab.binding.rotel.internal.RotelModel;
25 import org.openhab.core.io.transport.serial.PortInUseException;
26 import org.openhab.core.io.transport.serial.SerialPort;
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.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * Class for communicating with the Rotel device through a serial connection
36 * @author Laurent Garnier - Initial contribution
39 public class RotelSerialConnector extends RotelConnector {
41 private final Logger logger = LoggerFactory.getLogger(RotelSerialConnector.class);
43 private String serialPortName;
44 private SerialPortManager serialPortManager;
46 private @Nullable SerialPort serialPort;
51 * @param serialPortManager the serial port manager
52 * @param serialPortName the serial port name to be used
53 * @param model the projector model in use
54 * @param protocol the protocol to be used
55 * @param readerThreadName the name of thread to be created
57 public RotelSerialConnector(SerialPortManager serialPortManager, String serialPortName, RotelModel model,
58 RotelProtocol protocol, Map<RotelSource, String> sourcesLabels, String readerThreadName) {
59 super(model, protocol, sourcesLabels, false, readerThreadName);
61 this.serialPortManager = serialPortManager;
62 this.serialPortName = serialPortName;
66 public synchronized void open() throws RotelException {
67 logger.debug("Opening serial connection on port {}", serialPortName);
69 SerialPortIdentifier portIdentifier = serialPortManager.getIdentifier(serialPortName);
70 if (portIdentifier == null) {
72 logger.debug("Opening serial connection failed: No Such Port: {}", serialPortName);
73 throw new RotelException("Opening serial connection failed: No Such Port");
76 SerialPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
78 commPort.setSerialPortParams(getModel().getBaudRate(), SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
79 SerialPort.PARITY_NONE);
80 commPort.enableReceiveThreshold(1);
81 commPort.enableReceiveTimeout(100);
82 commPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
84 InputStream dataIn = commPort.getInputStream();
85 OutputStream dataOut = commPort.getOutputStream();
87 if (dataOut != null) {
90 if (dataIn != null && dataIn.markSupported()) {
93 } catch (IOException e) {
97 Thread thread = new RotelReaderThread(this, readerThreadName);
98 setReaderThread(thread);
101 this.serialPort = commPort;
102 this.dataIn = dataIn;
103 this.dataOut = dataOut;
107 logger.debug("Serial connection opened");
108 } catch (PortInUseException e) {
110 logger.debug("Opening serial connection failed: Port in Use Exception: {}", e.getMessage(), e);
111 throw new RotelException("Opening serial connection failed: Port in Use Exception");
112 } catch (UnsupportedCommOperationException e) {
114 logger.debug("Opening serial connection failed: Unsupported Comm Operation Exception: {}", e.getMessage(),
116 throw new RotelException("Opening serial connection failed: Unsupported Comm Operation Exception");
117 } catch (UnsupportedEncodingException e) {
119 logger.debug("Opening serial connection failed: Unsupported Encoding Exception: {}", e.getMessage(), e);
120 throw new RotelException("Opening serial connection failed: Unsupported Encoding Exception");
121 } catch (IOException e) {
123 logger.debug("Opening serial connection failed: IO Exception: {}", e.getMessage(), e);
124 throw new RotelException("Opening serial connection failed: IO Exception");
129 public synchronized void close() {
130 logger.debug("Closing serial connection");
131 SerialPort serialPort = this.serialPort;
132 if (serialPort != null) {
133 serialPort.removeEventListener();
136 if (serialPort != null) {
138 this.serialPort = null;
141 logger.debug("Serial connection closed");