]> git.basschouten.com Git - openhab-addons.git/blob
f75e36f05f3a0014fb6ce3fa504950cb9ef4199d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.dsmr.internal.device.connector;
14
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;
18
19 /**
20  * Class for storing port settings
21  * This class does store 4 serial parameters (baudrate, databits, parity, stopbits)
22  * for use in {@link DSMRSerialConnector}.
23  *
24  * This class can also convert a string setting (<speed> <databits><parity><stopbits>)
25  * to a {@link DSMRSerialSettings} object (e.g. 115200 8N1)
26  *
27  * @author M. Volaart - Initial contribution
28  * @author Hilbrand Bouwkamp - Removed auto detecting state checking from this class.
29  */
30 @NonNullByDefault
31 public class DSMRSerialSettings {
32
33     /**
34      * Fixed settings for high speed communication (DSMR V4 and up)
35      */
36     public static final DSMRSerialSettings HIGH_SPEED_SETTINGS = new DSMRSerialSettings(115200, SerialPort.DATABITS_8,
37             SerialPort.PARITY_NONE, SerialPort.STOPBITS_1);
38
39     /**
40      * Fixed settings for low speed communication (DSMR V3 and down)
41      */
42     public static final DSMRSerialSettings LOW_SPEED_SETTINGS = new DSMRSerialSettings(9600, SerialPort.DATABITS_7,
43             SerialPort.PARITY_EVEN, SerialPort.STOPBITS_1);
44
45     /**
46      * Serial port baudrate
47      */
48     private final int baudrate;
49
50     /**
51      * Serial port databits
52      */
53     private final int databits;
54
55     /**
56      * Serial port parity
57      */
58     private final int parity;
59
60     /**
61      * Serial port stop bits
62      */
63     private final int stopbits;
64
65     /**
66      * Construct a new {@link DSMRSerialSettings} object.
67      *
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)
72      */
73     private DSMRSerialSettings(int baudrate, int databits, int parity, int stopbits) {
74         this.baudrate = baudrate;
75         this.databits = databits;
76         this.parity = parity;
77         this.stopbits = stopbits;
78     }
79
80     /**
81      * Returns the baudrate
82      *
83      * @return baudrate setting
84      */
85     public int getBaudrate() {
86         return baudrate;
87     }
88
89     /**
90      * Returns the number of data bits
91      *
92      * @return databits setting
93      */
94     public int getDataBits() {
95         return databits;
96     }
97
98     /**
99      * Returns the parity setting
100      *
101      * @return parity setting
102      */
103     public int getParity() {
104         return parity;
105     }
106
107     /**
108      * Returns the number of stop bits
109      *
110      * @return stop bits setting
111      */
112     public int getStopbits() {
113         return stopbits;
114     }
115
116     @Override
117     public String toString() {
118         String toString = "Baudrate:" + baudrate + ", databits:" + databits;
119
120         switch (parity) {
121             case SerialPort.PARITY_EVEN:
122                 toString += ", parity:even";
123                 break;
124             case SerialPort.PARITY_NONE:
125                 toString += ", parity:none";
126                 break;
127             case SerialPort.PARITY_ODD:
128                 toString += ", parity:odd";
129                 break;
130             default:
131                 toString += ", parity:<unknown>";
132                 break;
133         }
134         switch (stopbits) {
135             case SerialPort.STOPBITS_1:
136                 toString += ", stopbits:1";
137                 break;
138             case SerialPort.STOPBITS_1_5:
139                 toString += ", stopbits:1.5";
140                 break;
141             case SerialPort.STOPBITS_2:
142                 toString += ", stopbits:2";
143                 break;
144             default:
145                 toString += ", stopbits:<unknown>";
146                 break;
147         }
148         return toString;
149     }
150
151     /**
152      * Returns the manual entered port setting if all configuration fields have a value (not null).
153      *
154      * @param deviceConfiguration manual entered device configuration
155      * @return serial configuration.
156      */
157     public static DSMRSerialSettings getPortSettingsFromConfiguration(DSMRDeviceConfiguration deviceConfiguration) {
158         int baudrate = deviceConfiguration.baudrate;
159         int databits = deviceConfiguration.databits;
160
161         int parity;
162         switch (deviceConfiguration.parity) {
163             case "E":
164                 parity = SerialPort.PARITY_EVEN;
165                 break;
166             case "O":
167                 parity = SerialPort.PARITY_ODD;
168                 break;
169             case "N":
170                 parity = SerialPort.PARITY_NONE;
171                 break;
172             default:
173                 parity = -1;
174                 break;
175         }
176
177         int stopbits;
178         switch (deviceConfiguration.stopbits) {
179             case "1":
180                 stopbits = SerialPort.STOPBITS_1;
181                 break;
182             case "1.5":
183                 stopbits = SerialPort.STOPBITS_1_5;
184                 break;
185             case "2":
186                 stopbits = SerialPort.STOPBITS_2;
187                 break;
188             default:
189                 stopbits = -1;
190                 break;
191         }
192         return new DSMRSerialSettings(baudrate, databits, parity, stopbits);
193     }
194 }