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.regoheatpump.internal.protocol;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.OutputStream;
19 import org.openhab.core.io.transport.serial.PortInUseException;
20 import org.openhab.core.io.transport.serial.SerialPort;
21 import org.openhab.core.io.transport.serial.SerialPortIdentifier;
22 import org.openhab.core.io.transport.serial.UnsupportedCommOperationException;
25 * The {@link SerialRegoConnection} is responsible for creating serial connections to clients.
27 * @author Boris Krivonog - Initial contribution
29 public class SerialRegoConnection implements RegoConnection {
30 private final int baudRate;
31 private final String portName;
32 private SerialPort serialPort;
33 private final SerialPortIdentifier serialPortIdentifier;
35 public SerialRegoConnection(SerialPortIdentifier serialPortIdentifier, int baudRate) {
36 this.serialPortIdentifier = serialPortIdentifier;
37 this.portName = serialPortIdentifier.getName();
38 this.baudRate = baudRate;
42 public void connect() throws IOException {
44 serialPort = serialPortIdentifier.open(SerialRegoConnection.class.getCanonicalName(), 2000);
45 serialPort.enableReceiveTimeout(100);
46 serialPort.setSerialPortParams(baudRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
47 SerialPort.PARITY_NONE);
48 } catch (PortInUseException e) {
49 throw new IOException("Serial port already used: " + portName, e);
50 } catch (UnsupportedCommOperationException e) {
51 throw new IOException("Unsupported operation on '" + portName + "': " + e.getMessage(), e);
56 public boolean isConnected() {
57 return serialPort != null;
62 if (serialPort != null) {
69 public OutputStream outputStream() throws IOException {
70 return serialPort.getOutputStream();
74 public InputStream inputStream() throws IOException {
75 return serialPort.getInputStream();