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