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
25 * ({@code <speed> <databits><parity><stopbits>})
26 * to a {@link DSMRSerialSettings} object (e.g. 115200 8N1)
28 * @author M. Volaart - Initial contribution
30 * @author Hilbrand Bouwkamp - Removed auto detecting state checking from this class.
33 public class DSMRSerialSettings {
36 * Fixed settings for high speed communication (DSMR V4 and up)
38 public static final DSMRSerialSettings HIGH_SPEED_SETTINGS = new DSMRSerialSettings(115200, SerialPort.DATABITS_8,
39 SerialPort.PARITY_NONE, SerialPort.STOPBITS_1);
42 * Fixed settings for low speed communication (DSMR V3 and down)
44 public static final DSMRSerialSettings LOW_SPEED_SETTINGS = new DSMRSerialSettings(9600, SerialPort.DATABITS_7,
45 SerialPort.PARITY_EVEN, SerialPort.STOPBITS_1);
48 * Serial port baudrate
50 private final int baudrate;
53 * Serial port databits
55 private final int databits;
60 private final int parity;
63 * Serial port stop bits
65 private final int stopbits;
68 * Construct a new {@link DSMRSerialSettings} object.
70 * @param baudrate baudrate of the port
71 * @param databits no data bits to use (use SerialPort.DATABITS_* constant)
72 * @param parity parity to use (use SerialPort.PARITY_* constant)
73 * @param stopbits no stopbits to use (use SerialPort.STOPBITS_* constant)
75 private DSMRSerialSettings(int baudrate, int databits, int parity, int stopbits) {
76 this.baudrate = baudrate;
77 this.databits = databits;
79 this.stopbits = stopbits;
83 * Returns the baudrate
85 * @return baudrate setting
87 public int getBaudrate() {
92 * Returns the number of data bits
94 * @return databits setting
96 public int getDataBits() {
101 * Returns the parity setting
103 * @return parity setting
105 public int getParity() {
110 * Returns the number of stop bits
112 * @return stop bits setting
114 public int getStopbits() {
119 public String toString() {
120 String toString = "Baudrate:" + baudrate + ", databits:" + databits;
123 case SerialPort.PARITY_EVEN:
124 toString += ", parity:even";
126 case SerialPort.PARITY_NONE:
127 toString += ", parity:none";
129 case SerialPort.PARITY_ODD:
130 toString += ", parity:odd";
133 toString += ", parity:<unknown>";
137 case SerialPort.STOPBITS_1:
138 toString += ", stopbits:1";
140 case SerialPort.STOPBITS_1_5:
141 toString += ", stopbits:1.5";
143 case SerialPort.STOPBITS_2:
144 toString += ", stopbits:2";
147 toString += ", stopbits:<unknown>";
154 * Returns the manual entered port setting if all configuration fields have a value (not null).
156 * @param deviceConfiguration manual entered device configuration
157 * @return serial configuration.
159 public static DSMRSerialSettings getPortSettingsFromConfiguration(DSMRDeviceConfiguration deviceConfiguration) {
160 int baudrate = deviceConfiguration.baudrate;
161 int databits = deviceConfiguration.databits;
164 switch (deviceConfiguration.parity) {
166 parity = SerialPort.PARITY_EVEN;
169 parity = SerialPort.PARITY_ODD;
172 parity = SerialPort.PARITY_NONE;
180 switch (deviceConfiguration.stopbits) {
182 stopbits = SerialPort.STOPBITS_1;
185 stopbits = SerialPort.STOPBITS_1_5;
188 stopbits = SerialPort.STOPBITS_2;
194 return new DSMRSerialSettings(baudrate, databits, parity, stopbits);