001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020 021package org.apache.mina.transport.serial; 022 023import gnu.io.SerialPort; 024 025import java.net.SocketAddress; 026import java.security.InvalidParameterException; 027 028/** 029 * An address for a serial port communication. 030 * 031 * @author <a href="http://mina.apache.org">Apache MINA Project</a> 032 */ 033public class SerialAddress extends SocketAddress { 034 035 private static final long serialVersionUID = 1735370510442384505L; 036 037 public enum DataBits { 038 DATABITS_5, DATABITS_6, DATABITS_7, DATABITS_8 039 } 040 041 public enum Parity { 042 NONE, ODD, EVEN, MARK, SPACE 043 } 044 045 public enum StopBits { 046 BITS_1, BITS_2, BITS_1_5 047 } 048 049 public enum FlowControl { 050 NONE, RTSCTS_IN, RTSCTS_OUT, RTSCTS_IN_OUT, XONXOFF_IN, XONXOFF_OUT, XONXOFF_IN_OUT 051 } 052 053 private final String name; 054 055 private final int bauds; 056 057 private final DataBits dataBits; 058 059 private final StopBits stopBits; 060 061 private final Parity parity; 062 063 private final FlowControl flowControl; 064 065 /** 066 * Create an address for a serial communication, associating a serial interface and 067 * various serial signal carcteristics. 068 * @param name name of the device, COM1 COM2 for Windows, /dev/ttyS0 for Unix 069 * @param bauds baud rate for the communication 070 * @param dataBits number of data bits per bytes 071 * @param stopBits number of stop bits 072 * @param parity parity used 073 * @param flowControl flow control used 074 */ 075 public SerialAddress(String name, int bauds, DataBits dataBits, StopBits stopBits, Parity parity, 076 FlowControl flowControl) { 077 if (name == null) { 078 throw new IllegalArgumentException("name"); 079 } 080 name = name.trim(); 081 if (name.length() == 0) { 082 throw new IllegalArgumentException("Empty name."); 083 } 084 if (bauds <= 0) { 085 throw new IllegalArgumentException("bauds: " + bauds); 086 } 087 if (dataBits == null) { 088 throw new IllegalArgumentException("dataBits"); 089 } 090 if (stopBits == null) { 091 throw new IllegalArgumentException("stopBits"); 092 } 093 if (parity == null) { 094 throw new IllegalArgumentException("parity"); 095 } 096 if (flowControl == null) { 097 throw new IllegalArgumentException("flowControl"); 098 } 099 100 this.name = name; 101 this.bauds = bauds; 102 this.dataBits = dataBits; 103 this.stopBits = stopBits; 104 this.parity = parity; 105 this.flowControl = flowControl; 106 } 107 108 /** 109 * Bauds rate for the communication. 110 * @return the bauds (bits per seconds) for this serial link 111 */ 112 public int getBauds() { 113 return bauds; 114 } 115 116 /** 117 * Number of data bits for each communicated bytes. 118 * @return the data bits 119 */ 120 public DataBits getDataBits() { 121 return dataBits; 122 } 123 124 /** 125 * The flow control policie used for this communication. 126 * @return the flow control 127 */ 128 public FlowControl getFlowControl() { 129 return flowControl; 130 } 131 132 /** 133 * The name of the device. Can be COM1, COM2, /dev/ttyS0, /dev/ttyUSB1, etc.. 134 * @return name 135 */ 136 public String getName() { 137 return name; 138 } 139 140 /** 141 * The parity check for this communication. 142 * @return parity type 143 */ 144 public Parity getParity() { 145 return parity; 146 } 147 148 /** 149 * Number of stop bits used. 150 * @return stop bits number 151 */ 152 public StopBits getStopBits() { 153 return stopBits; 154 } 155 156 /** 157 * Convert this serial address to a human readable string. 158 */ 159 @Override 160 public String toString() { 161 return name + " (bauds: " + bauds + ", dataBits: " + dataBits + ", stopBits: " + stopBits + ", parity: " 162 + parity + ", flowControl: " + flowControl + ")"; 163 } 164 165 int getDataBitsForRXTX() { 166 switch (dataBits) { 167 case DATABITS_5: 168 return SerialPort.DATABITS_5; 169 case DATABITS_6: 170 return SerialPort.DATABITS_6; 171 case DATABITS_7: 172 return SerialPort.DATABITS_7; 173 case DATABITS_8: 174 return SerialPort.DATABITS_8; 175 } 176 throw new InvalidParameterException("broken databits"); 177 } 178 179 int getStopBitsForRXTX() { 180 switch (stopBits) { 181 case BITS_1: 182 return SerialPort.STOPBITS_1; 183 case BITS_1_5: 184 return SerialPort.STOPBITS_1_5; 185 case BITS_2: 186 return SerialPort.STOPBITS_2; 187 } 188 throw new InvalidParameterException("broken stopbits"); 189 } 190 191 int getParityForRXTX() { 192 switch (parity) { 193 case EVEN: 194 return SerialPort.PARITY_EVEN; 195 case MARK: 196 return SerialPort.PARITY_MARK; 197 case NONE: 198 return SerialPort.PARITY_NONE; 199 case ODD: 200 return SerialPort.PARITY_ODD; 201 case SPACE: 202 return SerialPort.PARITY_SPACE; 203 } 204 throw new InvalidParameterException("broken parity"); 205 } 206 207 int getFLowControlForRXTX() { 208 switch (flowControl) { 209 case NONE: 210 return SerialPort.FLOWCONTROL_NONE; 211 case RTSCTS_IN: 212 return SerialPort.FLOWCONTROL_RTSCTS_IN; 213 case RTSCTS_OUT: 214 return SerialPort.FLOWCONTROL_RTSCTS_OUT; 215 case RTSCTS_IN_OUT: 216 return SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT; 217 case XONXOFF_IN: 218 return SerialPort.FLOWCONTROL_XONXOFF_IN; 219 case XONXOFF_OUT: 220 return SerialPort.FLOWCONTROL_XONXOFF_OUT; 221 case XONXOFF_IN_OUT: 222 return SerialPort.FLOWCONTROL_XONXOFF_IN | SerialPort.FLOWCONTROL_XONXOFF_OUT; 223 } 224 throw new InvalidParameterException("broken flow control"); 225 } 226}