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