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 <a href="http://mina.apache.org">Apache MINA Project</a>
32   */
33  public class SerialAddress extends SocketAddress {
34  
35      private static final long serialVersionUID = 1735370510442384505L;
36  
37      public enum DataBits {
38          DATABITS_5, DATABITS_6, DATABITS_7, DATABITS_8
39      }
40  
41      public enum Parity {
42          NONE, ODD, EVEN, MARK, SPACE
43      }
44  
45      public enum StopBits {
46          BITS_1, BITS_2, BITS_1_5
47      }
48  
49      public enum FlowControl {
50          NONE, RTSCTS_IN, RTSCTS_OUT, RTSCTS_IN_OUT, XONXOFF_IN, XONXOFF_OUT, XONXOFF_IN_OUT
51      }
52  
53      private final String name;
54  
55      private final int bauds;
56  
57      private final DataBits dataBits;
58  
59      private final StopBits stopBits;
60  
61      private final Parity parity;
62  
63      private final FlowControl flowControl;
64  
65      /**
66       * Create an address for a serial communication, associating a serial interface and
67       * various serial signal carcteristics.
68       * @param name name of the device, COM1 COM2 for Windows, /dev/ttyS0 for Unix
69       * @param bauds baud rate for the communication
70       * @param dataBits number of data bits per bytes
71       * @param stopBits number of stop bits
72       * @param parity parity used
73       * @param flowControl flow control used
74       */
75      public SerialAddress(String name, int bauds, DataBits dataBits, StopBits stopBits, Parity parity,
76              FlowControl flowControl) {
77          if (name == null) {
78              throw new IllegalArgumentException("name");
79          }
80          name = name.trim();
81          if (name.length() == 0) {
82              throw new IllegalArgumentException("Empty name.");
83          }
84          if (bauds <= 0) {
85              throw new IllegalArgumentException("bauds: " + bauds);
86          }
87          if (dataBits == null) {
88              throw new IllegalArgumentException("dataBits");
89          }
90          if (stopBits == null) {
91              throw new IllegalArgumentException("stopBits");
92          }
93          if (parity == null) {
94              throw new IllegalArgumentException("parity");
95          }
96          if (flowControl == null) {
97              throw new IllegalArgumentException("flowControl");
98          }
99  
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 }