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: https://www.pi4j.com/ 012 * ********************************************************************** 013 * %% 014 * Copyright (C) 2012 - 2021 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.system.SystemInfo; 034 035import java.io.File; 036import java.io.IOException; 037 038public class SerialPort { 039 040 /** 041 * Get the default serial port for the detected platform/model/board revision. 042 * 043 * @return com port device path 044 * @throws IOException 045 * @throws InterruptedException 046 */ 047 public static String getDefaultPort() throws IOException, InterruptedException, UnsupportedBoardType { 048 return getDefaultPort(SystemInfo.getBoardType()); 049 } 050 051 /** 052 * Get the default serial port for the specified platform/model/board revision. 053 * 054 * @param board hardware board type 055 * @return com port device path 056 * 057 * REF: https://www.raspberrypi.org/documentation/configuration/uart.md 058 * 059 * By default, only UART0 is enabled. 060 * The following table summarises the assignment of the first two UARTs: 061 * 062 * ---------------------------------------------------------------- 063 * Model | first PL011 (UART0) | mini UART 064 * ---------------------------------------------------------------- 065 * Raspberry Pi Zero primary secondary 066 * Raspberry Pi Zero W secondary (Bluetooth) primary 067 * Raspberry Pi 1 primary secondary 068 * Raspberry Pi 2 primary secondary 069 * Raspberry Pi 3 secondary (Bluetooth) primary 070 * Raspberry Pi 4 secondary (Bluetooth) primary 071 * ---------------------------------------------------------------- 072 * Note: the mini UART is disabled by default, whether it is designated 073 * primary or secondary UART. 074 * 075 * Linux devices on Raspberry Pi OS: 076 * ---------------------------------------------------------- 077 * Linux device Description 078 * ---------------------------------------------------------- 079 * /dev/ttyS0 mini UART 080 * /dev/ttyAMA0 first PL011 (UART0) 081 * /dev/serial0 primary UART 082 * /dev/serial1 secondary UART 083 * ---------------------------------------------------------- 084 * Note: /dev/serial0 and /dev/serial1 are symbolic links which point 085 * to either /dev/ttyS0 or /dev/ttyAMA0. 086 */ 087 public static String getDefaultPort(SystemInfo.BoardType board) throws UnsupportedBoardType { 088 switch (board){ 089 // ------------------------------------------------------------- 090 // LEGACY RASPBERRY PI MODELS: 1A, 1B, 1A+, 1B+, CM1, 2B, Zero 091 // ------------------------------------------------------------- 092 case RaspberryPi_A: 093 case RaspberryPi_B_Rev1: 094 case RaspberryPi_B_Rev2: 095 case RaspberryPi_A_Plus: 096 case RaspberryPi_B_Plus: 097 case RaspberryPi_ComputeModule: 098 case RaspberryPi_2B: 099 case RaspberryPi_Zero: 100 case RaspberryPi_ComputeModule3: 101 case RaspberryPi_Alpha: 102 case RaspberryPi_Unknown: { 103 return RaspberryPiSerial.DEFAULT_COM_PORT; 104 } 105 106 // -------------------------------------------------------- 107 // NEWER RASPBERRY PI MODELS: ZeroW, 3B, 3B+, 4B, 400, CM4 108 // -------------------------------------------------------- 109 case RaspberryPi_ZeroW: 110 case RaspberryPi_3B: 111 case RaspberryPi_3B_Plus: 112 case RaspberryPi_3A_Plus: 113 case RaspberryPi_4B: 114 case RaspberryPi_400: 115 case RaspberryPi_ComputeModule4:{ 116 // if the /dev/ttyS0 port exists, then use it as the default serial port 117 File s0ComPort = new File(RaspberryPiSerial.S0_COM_PORT); 118 if((s0ComPort.exists())){ 119 return RaspberryPiSerial.S0_COM_PORT; 120 } 121 return RaspberryPiSerial.DEFAULT_COM_PORT; 122 } 123 124 // ------------------------ 125 // BANANAPI and BANANAPRO 126 // ------------------------ 127 case BananaPi: { 128 return BananaPiSerial.DEFAULT_COM_PORT; 129 } 130 case BananaPro: { 131 return BananaProSerial.DEFAULT_COM_PORT; 132 } 133 134 // ------------------------ 135 // BPI 136 // ------------------------ 137 // TODO : Implement serial for BPI boards 138 case Bpi_M1: 139 break; 140 case Bpi_M1P: 141 break; 142 case Bpi_M2: 143 break; 144 case Bpi_M2M: 145 break; 146 case Bpi_M2P: 147 break; 148 case Bpi_M2P_H2_Plus: 149 break; 150 case Bpi_M2P_H5: 151 break; 152 case Bpi_M2U: 153 break; 154 case Bpi_M2U_V40: 155 break; 156 case Bpi_M3: 157 break; 158 case Bpi_M64: 159 break; 160 case Bpi_R1: 161 break; 162 163 // ------------------------ 164 // NANOPI 165 // ------------------------ 166 // TODO : Implement serial for NanoPi boards 167 case NanoPi_A64: 168 break; 169 case NanoPi_K2: 170 break; 171 case NanoPi_M1: 172 break; 173 case NanoPi_M1_Plus: 174 break; 175 case NanoPi_M3: 176 break; 177 case NanoPi_NEO: 178 break; 179 case NanoPi_NEO2: 180 break; 181 case NanoPi_NEO2_Plus: 182 break; 183 case NanoPi_NEO_Air: 184 break; 185 case NanoPi_S2: 186 break; 187 188 // ------------------------ 189 // ODROID 190 // ------------------------ 191 // TODO : Implement serial for Odroid boards 192 193 case Odroid: 194 break; 195 196 // ------------------------ 197 // ORANGEPI 198 // ------------------------ 199 // TODO : Implement serial for OrangePi boards 200 case OrangePi: 201 break; 202 203 // ------------------------ 204 // UNKNOWN 205 // ------------------------ 206 case UNKNOWN: 207 break; 208 default: 209 break; 210 } 211 212 // unknown board type, return null 213 throw new UnsupportedBoardType(); 214 } 215} 216