]> git.basschouten.com Git - openhab-addons.git/blob
bc51e0fed945d2dfb10d9143ba1f9125bd351edc
[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.smartmeter.internal.helper;
14
15 import org.apache.commons.lang3.StringUtils;
16 import org.eclipse.jdt.annotation.NonNullByDefault;
17 import org.openhab.core.io.transport.serial.SerialPort;
18
19 /**
20  *
21  * @author Matthias Steigenberger - Initial contribution
22  *
23  */
24 @NonNullByDefault
25 public enum SerialParameter {
26
27     _8N1(SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE),
28     _7N1(SerialPort.DATABITS_7, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE),
29     _7O1(SerialPort.DATABITS_7, SerialPort.STOPBITS_1, SerialPort.PARITY_ODD),
30     _7E1(SerialPort.DATABITS_7, SerialPort.STOPBITS_1, SerialPort.PARITY_EVEN);
31
32     private int databits;
33     private int stopbits;
34     private int parity;
35
36     private SerialParameter(int databits, int stopbits, int parity) {
37         this.databits = databits;
38         this.stopbits = stopbits;
39         this.parity = parity;
40     }
41
42     public int getDatabits() {
43         return this.databits;
44     }
45
46     public int getStopbits() {
47         return stopbits;
48     }
49
50     public int getParity() {
51         return parity;
52     }
53
54     @Override
55     public String toString() {
56         return name().substring(1);
57     }
58
59     /**
60      * Returns the enum constant for the serial parameter string.
61      * The parameters must be in format 'StartbitsParityStopbits'
62      * e.g. '7N1', '8N1'
63      *
64      * @param params
65      * @return The found {@link SerialParameter} or {@link SerialParameter#_8N1} if not found
66      */
67     public static SerialParameter fromString(String params) {
68         try {
69             return valueOf("_" + StringUtils.upperCase(params));
70         } catch (IllegalArgumentException e) {
71             return SerialParameter._8N1;
72         }
73     }
74 }