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.knx.internal.client;
15 import java.util.Enumeration;
16 import java.util.concurrent.ScheduledExecutorService;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.core.thing.ThingUID;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 import gnu.io.CommPortIdentifier;
24 import gnu.io.RXTXVersion;
25 import tuwien.auto.calimero.KNXException;
26 import tuwien.auto.calimero.link.KNXNetworkLink;
27 import tuwien.auto.calimero.link.KNXNetworkLinkFT12;
28 import tuwien.auto.calimero.link.medium.TPSettings;
31 * Serial specific {@link AbstractKNXClient} implementation.
33 * @author Simon Kaufmann - initial contribution and API.
37 public class SerialClient extends AbstractKNXClient {
39 private final Logger logger = LoggerFactory.getLogger(SerialClient.class);
41 private final String serialPort;
43 public SerialClient(int autoReconnectPeriod, ThingUID thingUID, int responseTimeout, int readingPause,
44 int readRetriesLimit, ScheduledExecutorService knxScheduler, String serialPort,
45 StatusUpdateCallback statusUpdateCallback) {
46 super(autoReconnectPeriod, thingUID, responseTimeout, readingPause, readRetriesLimit, knxScheduler,
47 statusUpdateCallback);
48 this.serialPort = serialPort;
52 protected KNXNetworkLink establishConnection() throws KNXException, InterruptedException {
54 RXTXVersion.getVersion();
55 logger.debug("Establishing connection to KNX bus through FT1.2 on serial port {}.", serialPort);
56 return new KNXNetworkLinkFT12(serialPort, new TPSettings());
58 } catch (NoClassDefFoundError e) {
59 throw new KNXException(
60 "The serial FT1.2 KNX connection requires the RXTX libraries to be available, but they could not be found!",
62 } catch (KNXException e) {
63 final String msg = e.getMessage();
64 if ((msg != null) && (msg.startsWith("can not open serial port"))) {
65 StringBuilder sb = new StringBuilder("Available ports are:\n");
66 Enumeration<?> portList = CommPortIdentifier.getPortIdentifiers();
67 while (portList.hasMoreElements()) {
68 CommPortIdentifier id = (CommPortIdentifier) portList.nextElement();
69 if ((id != null) && (id.getPortType() == CommPortIdentifier.PORT_SERIAL)) {
70 sb.append(id.getName());
74 sb.deleteCharAt(sb.length() - 1);
75 throw new KNXException("Serial port '" + serialPort + "' could not be opened. " + sb.toString());