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.rotel.internal.communication;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.OutputStream;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.rotel.internal.RotelException;
22 import org.openhab.binding.rotel.internal.protocol.RotelAbstractProtocolHandler;
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.SerialPortIdentifier;
26 import org.openhab.core.io.transport.serial.SerialPortManager;
27 import org.openhab.core.io.transport.serial.UnsupportedCommOperationException;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * Class for communicating with the Rotel device through a serial connection
34 * @author Laurent Garnier - Initial contribution
37 public class RotelSerialConnector extends RotelConnector {
39 private final Logger logger = LoggerFactory.getLogger(RotelSerialConnector.class);
41 private String serialPortName;
42 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 baudRate the baud rate to be used
54 * @param protocolHandler the protocol handler
55 * @param readerThreadName the name of thread to be created
57 public RotelSerialConnector(SerialPortManager serialPortManager, String serialPortName, int baudRate,
58 RotelAbstractProtocolHandler protocolHandler, String readerThreadName) {
59 super(protocolHandler, false, readerThreadName);
61 this.baudRate = baudRate;
62 this.serialPortManager = serialPortManager;
63 this.serialPortName = serialPortName;
67 public synchronized void open() throws RotelException {
68 logger.debug("Opening serial connection on port {}", serialPortName);
70 SerialPortIdentifier portIdentifier = serialPortManager.getIdentifier(serialPortName);
71 if (portIdentifier == null) {
73 throw new RotelException("Opening serial connection failed: no port " + serialPortName);
76 SerialPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
78 commPort.setSerialPortParams(baudRate, 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) {
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");