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.herzborg.internal;
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.core.io.transport.serial.PortInUseException;
22 import org.openhab.core.io.transport.serial.SerialPort;
23 import org.openhab.core.io.transport.serial.SerialPortIdentifier;
24 import org.openhab.core.io.transport.serial.SerialPortManager;
25 import org.openhab.core.io.transport.serial.UnsupportedCommOperationException;
26 import org.openhab.core.thing.ThingStatusDetail;
29 * The {@link SerialBus} implements specific handling for Herzborg serial bus,
30 * connected directly via a serial port.
32 * @author Pavel Fedin - Initial contribution
35 public class SerialBus extends Bus {
36 private SerialPortManager serialPortManager;
37 private @Nullable SerialPort serialPort;
39 public SerialBus(SerialPortManager manager) {
40 serialPortManager = manager;
43 public Result initialize(@Nullable String port) {
45 return new Result(ThingStatusDetail.CONFIGURATION_ERROR, "Port is not specified");
47 SerialPortIdentifier portIdentifier = serialPortManager.getIdentifier(port);
48 if (portIdentifier == null) {
49 return new Result(ThingStatusDetail.CONFIGURATION_ERROR, "No such port: " + port);
54 commPort = portIdentifier.open(this.getClass().getName(), 2000);
55 } catch (PortInUseException e1) {
56 return new Result(ThingStatusDetail.CONFIGURATION_ERROR, "Port " + port + " is in use");
60 // Herzborg serial bus operates with fixed parameters
61 commPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
62 commPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
63 } catch (UnsupportedCommOperationException e) {
64 return new Result(ThingStatusDetail.CONFIGURATION_ERROR, "Invalid port configuration");
68 commPort.enableReceiveThreshold(8);
69 commPort.enableReceiveTimeout(1000);
70 } catch (UnsupportedCommOperationException e) {
71 // OpenHAB's serial-over-IP doesn't support these, so let's ignore the exception
74 InputStream dataIn = null;
75 OutputStream dataOut = null;
79 dataIn = commPort.getInputStream();
80 dataOut = commPort.getOutputStream();
83 error = "No input stream available on the serial port";
84 } else if (dataOut == null) {
85 error = "No output stream available on the serial port";
88 if (dataIn.markSupported()) {
92 } catch (IOException e) {
93 error = e.getMessage();
97 return new Result(ThingStatusDetail.HANDLER_INITIALIZING_ERROR, error);
100 this.serialPort = commPort;
101 this.dataIn = dataIn;
102 this.dataOut = dataOut;
104 return new Result(ThingStatusDetail.NONE);
108 public void dispose() {
109 SerialPort port = serialPort;
112 return; // Nothing to do in this case
115 port.removeEventListener();