001package com.pi4j.io.serial;
002
003/*
004 * #%L
005 * **********************************************************************
006 * ORGANIZATION  :  Pi4J
007 * PROJECT       :  Pi4J :: Java Library (Core)
008 * FILENAME      :  SerialPort.java
009 *
010 * This file is part of the Pi4J project. More information about
011 * this project can be found here:  http://www.pi4j.com/
012 * **********************************************************************
013 * %%
014 * Copyright (C) 2012 - 2016 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
032import com.pi4j.io.gpio.exception.UnsupportedBoardType;
033import com.pi4j.io.gpio.exception.UnsupportedPinEventsException;
034import com.pi4j.system.SystemInfo;
035
036import java.io.File;
037import java.io.IOException;
038
039public class SerialPort {
040
041    /**
042     * Get the default serial port for the detected platform/model/board revision.
043     *
044     * @return com port device path
045     * @throws IOException
046     * @throws InterruptedException
047     */
048    public static String getDefaultPort() throws IOException, InterruptedException, UnsupportedBoardType {
049        return getDefaultPort(SystemInfo.getBoardType());
050    }
051
052    /**
053     * Get the default serial port for the specified platform/model/board revision.
054     *
055     * @param board hardware board type
056     * @return com port device path
057     */
058    public static String getDefaultPort(SystemInfo.BoardType board) throws UnsupportedBoardType {
059        switch (board){
060            // ------------------------
061            // ALL RASPBERRY PI MODELS
062            // (except Model 3B)
063            // ------------------------
064            case RaspberryPi_A:
065            case RaspberryPi_B_Rev1:
066            case RaspberryPi_B_Rev2:
067            case RaspberryPi_A_Plus:
068            case RaspberryPi_B_Plus:
069            case RaspberryPi_ComputeModule:
070            case RaspberryPi_2B:
071            case RaspberryPi_Zero:
072            case RaspberryPi_Alpha:
073            case RaspberryPi_Unknown: {
074                return RaspberryPiSerial.DEFAULT_COM_PORT;
075            }
076
077            // ------------------------
078            // RASPBERRY PI MODEL 3B
079            // ------------------------
080            case RaspberryPi_3B: {
081                // if the /dev/ttyS0 port exists, then use it as the default serial port
082                File s0ComPort = new File(RaspberryPiSerial.S0_COM_PORT);
083                if((s0ComPort.exists())){
084                    return RaspberryPiSerial.S0_COM_PORT;
085                }
086                return RaspberryPiSerial.DEFAULT_COM_PORT;
087            }
088
089            // ------------------------
090            // BANANAPI and BANANAPRO
091            // ------------------------
092            case BananaPi: {
093                return BananaPiSerial.DEFAULT_COM_PORT;
094            }
095            case BananaPro: {
096                return BananaProSerial.DEFAULT_COM_PORT;
097            }
098
099            // ------------------------
100            // ODROID
101            // ------------------------
102            // TODO: Implement serial for Odroid boards
103        }
104
105        // unknown board type, return null
106        throw new UnsupportedBoardType();
107        }
108}
109