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.dsmr.internal.device.connector;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.dsmr.internal.device.DSMRDeviceConfiguration;
17 import org.openhab.core.io.transport.serial.SerialPort;
20 * Class for storing port settings
21 * This class does store 4 serial parameters (baudrate, databits, parity, stopbits)
22 * for use in {@link DSMRSerialConnector}.
24 * This class can also convert a string setting (<speed> <databits><parity><stopbits>)
25 * to a {@link DSMRSerialSettings} object (e.g. 115200 8N1)
27 * @author M. Volaart - Initial contribution
28 * @author Hilbrand Bouwkamp - Removed auto detecting state checking from this class.
31 public class DSMRSerialSettings {
34 * Fixed settings for high speed communication (DSMR V4 and up)
36 public static final DSMRSerialSettings HIGH_SPEED_SETTINGS = new DSMRSerialSettings(115200, SerialPort.DATABITS_8,
37 SerialPort.PARITY_NONE, SerialPort.STOPBITS_1);
40 * Fixed settings for low speed communication (DSMR V3 and down)
42 public static final DSMRSerialSettings LOW_SPEED_SETTINGS = new DSMRSerialSettings(9600, SerialPort.DATABITS_7,
43 SerialPort.PARITY_EVEN, SerialPort.STOPBITS_1);
46 * Serial port baudrate
48 private final int baudrate;
51 * Serial port databits
53 private final int databits;
58 private final int parity;
61 * Serial port stop bits
63 private final int stopbits;
66 * Construct a new {@link DSMRSerialSettings} object.
68 * @param baudrate baudrate of the port
69 * @param databits no data bits to use (use SerialPort.DATABITS_* constant)
70 * @param parity parity to use (use SerialPort.PARITY_* constant)
71 * @param stopbits no stopbits to use (use SerialPort.STOPBITS_* constant)
73 private DSMRSerialSettings(int baudrate, int databits, int parity, int stopbits) {
74 this.baudrate = baudrate;
75 this.databits = databits;
77 this.stopbits = stopbits;
81 * Returns the baudrate
83 * @return baudrate setting
85 public int getBaudrate() {
90 * Returns the number of data bits
92 * @return databits setting
94 public int getDataBits() {
99 * Returns the parity setting
101 * @return parity setting
103 public int getParity() {
108 * Returns the number of stop bits
110 * @return stop bits setting
112 public int getStopbits() {
117 public String toString() {
118 String toString = "Baudrate:" + baudrate + ", databits:" + databits;
121 case SerialPort.PARITY_EVEN:
122 toString += ", parity:even";
124 case SerialPort.PARITY_NONE:
125 toString += ", parity:none";
127 case SerialPort.PARITY_ODD:
128 toString += ", parity:odd";
131 toString += ", parity:<unknown>";
135 case SerialPort.STOPBITS_1:
136 toString += ", stopbits:1";
138 case SerialPort.STOPBITS_1_5:
139 toString += ", stopbits:1.5";
141 case SerialPort.STOPBITS_2:
142 toString += ", stopbits:2";
145 toString += ", stopbits:<unknown>";
152 * Returns the manual entered port setting if all configuration fields have a value (not null).
154 * @param deviceConfiguration manual entered device configuration
155 * @return serial configuration.
157 public static DSMRSerialSettings getPortSettingsFromConfiguration(DSMRDeviceConfiguration deviceConfiguration) {
158 int baudrate = deviceConfiguration.baudrate;
159 int databits = deviceConfiguration.databits;
162 switch (deviceConfiguration.parity) {
164 parity = SerialPort.PARITY_EVEN;
167 parity = SerialPort.PARITY_ODD;
170 parity = SerialPort.PARITY_NONE;
178 switch (deviceConfiguration.stopbits) {
180 stopbits = SerialPort.STOPBITS_1;
183 stopbits = SerialPort.STOPBITS_1_5;
186 stopbits = SerialPort.STOPBITS_2;
192 return new DSMRSerialSettings(baudrate, databits, parity, stopbits);