001package com.pi4j.io.serial;
002
003/*
004 * #%L
005 * **********************************************************************
006 * ORGANIZATION  :  Pi4J
007 * PROJECT       :  Pi4J :: Java Library (Core)
008 * FILENAME      :  SerialConfig.java
009 *
010 * This file is part of the Pi4J project. More information about
011 * this project can be found here:  https://www.pi4j.com/
012 * **********************************************************************
013 * %%
014 * Copyright (C) 2012 - 2019 Pi4J
015 * %%
016 * This program is free software: you can redistribute it and/or modify
017 * it under the terms of the GNU Lesser General Public License as
018 * published by the Free Software Foundation, either version 3 of the
019 * License, or (at your option) any later version.
020 *
021 * This program is distributed in the hope that it will be useful,
022 * but WITHOUT ANY WARRANTY; without even the implied warranty of
023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
024 * GNU General Lesser Public License for more details.
025 *
026 * You should have received a copy of the GNU General Lesser Public
027 * License along with this program.  If not, see
028 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
029 * #L%
030 */
031
032public class SerialConfig {
033
034    private String device = Serial.DEFAULT_COM_PORT;
035    private Baud baud = Baud._9600;
036    private DataBits dataBits = DataBits._8;
037    private Parity parity = Parity.NONE;
038    private StopBits stopBits = StopBits._1;
039    private FlowControl flowControl = FlowControl.NONE;
040
041    public SerialConfig(){}
042
043    /*
044     *  The device address of the serial port to access. You can use constant 'Serial.DEFAULT_COM_PORT'
045     *   if you wish to access the default serial port provided via the GPIO header.
046     */
047    public String device() { return device; }
048
049    /*
050     *  The device address of the serial port to access. You can use constant 'Serial.DEFAULT_COM_PORT'
051     *   if you wish to access the default serial port provided via the GPIO header.
052     */
053    public SerialConfig device(String device) { this.device = device; return this; }
054
055    /*
056     * The baud rate to use with the serial port.
057     */
058    public Baud baud() { return baud; }
059
060    /*
061     * The baud rate to use with the serial port.
062     */
063    public SerialConfig baud(Baud baud) { this.baud = baud; return this; }
064
065    /*
066     * The data bits to use for serial communication. (5,6,7,8)
067     */
068    public DataBits dataBits() { return dataBits; }
069
070    /*
071     * The data bits to use for serial communication. (5,6,7,8)
072     */
073    public SerialConfig dataBits(DataBits dataBits) { this.dataBits = dataBits; return this; }
074
075    /*
076     * The parity setting to use for serial communication. (None, Event, Odd, Mark, Space)
077     */
078    public Parity parity() { return parity; }
079
080    /*
081     * The parity setting to use for serial communication. (None, Event, Odd, Mark, Space)
082     */
083    public SerialConfig parity(Parity parity) { this.parity = parity; return this; }
084
085    /*
086     * The stop bits to use for serial communication. (1,2)
087     */
088    public StopBits stopBits() { return stopBits; }
089
090    /*
091     * The stop bits to use for serial communication. (1,2)
092     */
093    public SerialConfig stopBits(StopBits stopBits) { this.stopBits = stopBits; return this; }
094
095    /*
096     * The flow control option to use for serial communication. (none, hardware, software)
097     */
098    public FlowControl flowControl() { return flowControl; }
099
100    /*
101     * The flow control option to use for serial communication. (none, hardware, software)
102     */
103    public SerialConfig flowControl(FlowControl flowControl) { this.flowControl = flowControl; return this; }
104
105    @Override
106    public String toString(){
107        // /dev/ttyAMA0 (38400, 8N1) [FC=NONE]
108        return device() + " (" +
109                baud().getValue() + "," +
110                dataBits().getValue() +
111                parity().toString().substring(0, 1) +
112                stopBits().getValue() + ") {" +
113                "FC:" + flowControl().toString() + "}";
114    }
115
116}