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.rfxcom.internal.connector;
15 import java.io.IOException;
17 import org.openhab.binding.rfxcom.internal.config.RFXComBridgeConfiguration;
18 import org.openhab.core.util.HexUtils;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 import jd2xx.JD2XXInputStream;
24 import jd2xx.JD2XXOutputStream;
27 * RFXCOM connector for direct access via D2XX driver.
29 * @author Pauli Anttila - Initial contribution
31 public class RFXComJD2XXConnector extends RFXComBaseConnector {
32 private final Logger logger = LoggerFactory.getLogger(RFXComJD2XXConnector.class);
34 private JD2XX serialPort;
35 private JD2XXOutputStream out;
37 private final String readerThreadName;
38 private Thread readerThread;
40 public RFXComJD2XXConnector(String readerThreadName) {
42 this.readerThreadName = readerThreadName;
46 public void connect(RFXComBridgeConfiguration device) throws IOException {
47 logger.info("Connecting to RFXCOM device '{}' using JD2XX.", device.bridgeId);
49 if (serialPort == null) {
50 serialPort = new JD2XX();
52 serialPort.openBySerialNumber(device.bridgeId);
53 serialPort.setBaudRate(38400);
54 serialPort.setDataCharacteristics(8, JD2XX.STOP_BITS_1, JD2XX.PARITY_NONE);
55 serialPort.setFlowControl(JD2XX.FLOW_NONE, 0, 0);
56 serialPort.setTimeouts(100, 100);
58 in = new JD2XXInputStream(serialPort);
59 out = new JD2XXOutputStream(serialPort);
62 if (in.markSupported()) {
66 readerThread = new RFXComStreamReader(this, readerThreadName);
71 public void disconnect() {
72 logger.debug("Disconnecting");
74 if (readerThread != null) {
75 logger.debug("Interrupt serial listener");
76 readerThread.interrupt();
79 } catch (InterruptedException e) {
84 logger.debug("Close serial out stream");
87 } catch (IOException e) {
88 logger.debug("Error while closing the out stream: {}", e.getMessage());
92 logger.debug("Close serial in stream");
95 } catch (IOException e) {
96 logger.debug("Error while closing the in stream: {}", e.getMessage());
100 if (serialPort != null) {
101 logger.debug("Close serial port");
104 } catch (IOException e) {
105 logger.debug("Serial port closing error: {}", e.getMessage());
114 logger.debug("Closed");
118 public void sendMessage(byte[] data) throws IOException {
119 if (logger.isTraceEnabled()) {
120 logger.trace("Send data (len={}): {}", data.length, HexUtils.bytesToHex(data));