View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  
21  package org.apache.mina.transport.serial;
22  
23  import gnu.io.SerialPort;
24  
25  import java.net.SocketAddress;
26  import java.security.InvalidParameterException;
27  
28  /**
29   * An address for a serial port communication.
30   *
31   * @author The Apache MINA Project (dev@mina.apache.org)
32   * @version $Rev: 529576 $, $Date: 2007-04-17 14:25:07 +0200 (mar., 17 avr. 2007) $
33   */
34  public class SerialAddress extends SocketAddress {
35  
36      private static final long serialVersionUID = 1735370510442384505L;
37  
38      public enum DataBits {
39          DATABITS_5, DATABITS_6, DATABITS_7, DATABITS_8
40      }
41  
42      public enum Parity {
43          NONE, ODD, EVEN, MARK, SPACE
44      }
45  
46      public enum StopBits {
47          BITS_1, BITS_2, BITS_1_5
48      }
49  
50      public enum FlowControl {
51          NONE, RTSCTS_IN, RTSCTS_OUT, XONXOFF_IN, XONXOFF_OUT
52      }
53  
54      private final String name;
55      private final int bauds;
56      private final DataBits dataBits;
57      private final StopBits stopBits;
58      private final Parity parity;
59      private final FlowControl flowControl;
60  
61      /**
62       * Create an address for a serial communication, associating a serial interface and
63       * various serial signal carcteristics.
64       * @param name name of the device, COM1 COM2 for Windows, /dev/ttyS0 for Unix
65       * @param bauds baud rate for the communication
66       * @param dataBits number of data bits per bytes
67       * @param stopBits number of stop bits
68       * @param parity parity used
69       * @param flowControl flow control used
70       */
71      public SerialAddress(String name, int bauds, DataBits dataBits,
72              StopBits stopBits, Parity parity, FlowControl flowControl) {
73          if (name == null) {
74              throw new NullPointerException("name");
75          }
76          name = name.trim();
77          if (name.length() == 0) {
78              throw new IllegalArgumentException("Empty name.");
79          }
80          if (bauds <= 0) {
81              throw new IllegalArgumentException("bauds: " + bauds);
82          }
83          if (dataBits == null) {
84              throw new NullPointerException("dataBits");
85          }
86          if (stopBits == null) {
87              throw new NullPointerException("stopBits");
88          }
89          if (parity == null) {
90              throw new NullPointerException("parity");
91          }
92          if (flowControl == null) {
93              throw new NullPointerException("flowControl");
94          }
95          
96          this.name = name;
97          this.bauds = bauds;
98          this.dataBits = dataBits;
99          this.stopBits = stopBits;
100         this.parity = parity;
101         this.flowControl = flowControl;
102     }
103 
104     /**
105      * Bauds rate for the communication.
106      * @return the bauds (bits per seconds) for this serial link
107      */
108     public int getBauds() {
109         return bauds;
110     }
111 
112     /**
113      * Number of data bits for each communicated bytes.
114      * @return the data bits
115      */
116     public DataBits getDataBits() {
117         return dataBits;
118     }
119 
120     /**
121      * The flow control policie used for this communication.
122      * @return the flow control
123      */
124     public FlowControl getFlowControl() {
125         return flowControl;
126     }
127 
128     /**
129      * The name of the device. Can be COM1, COM2, /dev/ttyS0, /dev/ttyUSB1, etc..
130      * @return name
131      */
132     public String getName() {
133         return name;
134     }
135 
136     /**
137      * The parity check for this communication.
138      * @return parity type
139      */
140     public Parity getParity() {
141         return parity;
142     }
143 
144     /**
145      * Number of stop bits used.
146      * @return stop bits number
147      */
148     public StopBits getStopBits() {
149         return stopBits;
150     }
151 
152     /**
153      * Convert this serial address to a human readable string.
154      */
155     @Override
156     public String toString() {
157         return name + " (bauds: " + bauds + ", dataBits: " + dataBits
158                 + ", stopBits: " + stopBits + ", parity: " + parity
159                 + ", flowControl: " + flowControl + ")";
160     }
161 
162     int getDataBitsForRXTX() {
163         switch (dataBits) {
164         case DATABITS_5:
165             return SerialPort.DATABITS_5;
166         case DATABITS_6:
167             return SerialPort.DATABITS_6;
168         case DATABITS_7:
169             return SerialPort.DATABITS_7;
170         case DATABITS_8:
171             return SerialPort.DATABITS_8;
172         }
173         throw new InvalidParameterException("broken databits");
174     }
175 
176     int getStopBitsForRXTX() {
177         switch (stopBits) {
178         case BITS_1:
179             return SerialPort.STOPBITS_1;
180         case BITS_1_5:
181             return SerialPort.STOPBITS_1_5;
182         case BITS_2:
183             return SerialPort.STOPBITS_2;
184         }
185         throw new InvalidParameterException("broken stopbits");
186     }
187 
188     int getParityForRXTX() {
189         switch (parity) {
190         case EVEN:
191             return SerialPort.PARITY_EVEN;
192         case MARK:
193             return SerialPort.PARITY_MARK;
194         case NONE:
195             return SerialPort.PARITY_NONE;
196         case ODD:
197             return SerialPort.PARITY_ODD;
198         case SPACE:
199             return SerialPort.PARITY_SPACE;
200         }
201         throw new InvalidParameterException("broken parity");
202     }
203 
204     int getFLowControlForRXTX() {
205         switch (flowControl) {
206         case NONE:
207             return SerialPort.FLOWCONTROL_NONE;
208         case RTSCTS_IN:
209             return SerialPort.FLOWCONTROL_RTSCTS_IN;
210         case RTSCTS_OUT:
211             return SerialPort.FLOWCONTROL_RTSCTS_OUT;
212         case XONXOFF_IN:
213             return SerialPort.FLOWCONTROL_XONXOFF_IN;
214         case XONXOFF_OUT:
215             return SerialPort.FLOWCONTROL_XONXOFF_OUT;
216         }
217         throw new InvalidParameterException("broken stopbits");
218     }
219 }