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      /**
38       * The number of data bits per byte
39       */
40      public enum DataBits {
41          /** 5 bits per bytes */
42          DATABITS_5, 
43          
44          /** 6 bits per bytes */
45          DATABITS_6, 
46          
47          /** 7 bits per bytes */
48          DATABITS_7,
49          
50          /** 8 bits per bytes */
51          DATABITS_8
52      }
53  
54       /**
55        * The error detection parity in use
56        *
57        */
58      public enum Parity {
59          /** No parity bit sent */
60          NONE, 
61          
62          /** Odd parity */
63          ODD, 
64          
65          /** Even parity */
66          EVEN, 
67          
68          /** Mark signal condition */
69          MARK, 
70          
71          /**Space signal condition  */
72          SPACE
73      }
74  
75      /**
76       * Stop bits in use
77       */
78      public enum StopBits {
79          /** One bit */
80          BITS_1, 
81          
82          /** Two bits */
83          BITS_2,
84          
85          /** one and half bits */
86          BITS_1_5
87      }
88  
89      /**
90       * The Flow control flags
91       */
92      public enum FlowControl {
93          /** No flow control */
94          NONE, 
95          
96          /** RTS/CTS IN flow control */
97          RTSCTS_IN, 
98  
99          /** RTS/CTS OUT flow control */
100         RTSCTS_OUT, 
101         
102         /** RTS/CTS IN/OUT flow control */
103         RTSCTS_IN_OUT, 
104         
105         /** XON/XOFF IN flow control */
106         XONXOFF_IN, 
107         
108         /** XON/XOFF OUT flow control */
109         XONXOFF_OUT, 
110         
111         /** XON/XOFF IN/OUT flow control */
112         XONXOFF_IN_OUT
113     }
114 
115     private final String name;
116 
117     private final int bauds;
118 
119     private final DataBits dataBits;
120 
121     private final StopBits stopBits;
122 
123     private final Parity parity;
124 
125     private final FlowControl flowControl;
126 
127     /**
128      * Create an address for a serial communication, associating a serial interface and
129      * various serial signal carcteristics.
130      * @param name name of the device, COM1 COM2 for Windows, /dev/ttyS0 for Unix
131      * @param bauds baud rate for the communication
132      * @param dataBits number of data bits per bytes
133      * @param stopBits number of stop bits
134      * @param parity parity used
135      * @param flowControl flow control used
136      */
137     public SerialAddress(String name, int bauds, DataBits dataBits, StopBits stopBits, Parity parity,
138             FlowControl flowControl) {
139         if (name == null) {
140             throw new IllegalArgumentException("name");
141         }
142         name = name.trim();
143         if (name.length() == 0) {
144             throw new IllegalArgumentException("Empty name.");
145         }
146         if (bauds <= 0) {
147             throw new IllegalArgumentException("bauds: " + bauds);
148         }
149         if (dataBits == null) {
150             throw new IllegalArgumentException("dataBits");
151         }
152         if (stopBits == null) {
153             throw new IllegalArgumentException("stopBits");
154         }
155         if (parity == null) {
156             throw new IllegalArgumentException("parity");
157         }
158         if (flowControl == null) {
159             throw new IllegalArgumentException("flowControl");
160         }
161 
162         this.name = name;
163         this.bauds = bauds;
164         this.dataBits = dataBits;
165         this.stopBits = stopBits;
166         this.parity = parity;
167         this.flowControl = flowControl;
168     }
169 
170     /**
171      * Bauds rate for the communication.
172      * @return the bauds (bits per seconds) for this serial link
173      */
174     public int getBauds() {
175         return bauds;
176     }
177 
178     /**
179      * Number of data bits for each communicated bytes.
180      * @return the data bits
181      */
182     public DataBits getDataBits() {
183         return dataBits;
184     }
185 
186     /**
187      * The flow control policie used for this communication.
188      * @return the flow control
189      */
190     public FlowControl getFlowControl() {
191         return flowControl;
192     }
193 
194     /**
195      * The name of the device. Can be COM1, COM2, /dev/ttyS0, /dev/ttyUSB1, etc..
196      * @return name
197      */
198     public String getName() {
199         return name;
200     }
201 
202     /**
203      * The parity check for this communication.
204      * @return parity type
205      */
206     public Parity getParity() {
207         return parity;
208     }
209 
210     /**
211      * Number of stop bits used.
212      * @return stop bits number
213      */
214     public StopBits getStopBits() {
215         return stopBits;
216     }
217 
218     /**
219      * Convert this serial address to a human readable string.
220      */
221     @Override
222     public String toString() {
223         return name + " (bauds: " + bauds + ", dataBits: " + dataBits + ", stopBits: " + stopBits + ", parity: "
224                 + parity + ", flowControl: " + flowControl + ")";
225     }
226 
227     int getDataBitsForRXTX() {
228         switch (dataBits) {
229         case DATABITS_5:
230             return SerialPort.DATABITS_5;
231         case DATABITS_6:
232             return SerialPort.DATABITS_6;
233         case DATABITS_7:
234             return SerialPort.DATABITS_7;
235         case DATABITS_8:
236             return SerialPort.DATABITS_8;
237         }
238         throw new InvalidParameterException("broken databits");
239     }
240 
241     int getStopBitsForRXTX() {
242         switch (stopBits) {
243         case BITS_1:
244             return SerialPort.STOPBITS_1;
245         case BITS_1_5:
246             return SerialPort.STOPBITS_1_5;
247         case BITS_2:
248             return SerialPort.STOPBITS_2;
249         }
250         throw new InvalidParameterException("broken stopbits");
251     }
252 
253     int getParityForRXTX() {
254         switch (parity) {
255         case EVEN:
256             return SerialPort.PARITY_EVEN;
257         case MARK:
258             return SerialPort.PARITY_MARK;
259         case NONE:
260             return SerialPort.PARITY_NONE;
261         case ODD:
262             return SerialPort.PARITY_ODD;
263         case SPACE:
264             return SerialPort.PARITY_SPACE;
265         }
266         throw new InvalidParameterException("broken parity");
267     }
268 
269     int getFLowControlForRXTX() {
270         switch (flowControl) {
271         case NONE:
272             return SerialPort.FLOWCONTROL_NONE;
273         case RTSCTS_IN:
274             return SerialPort.FLOWCONTROL_RTSCTS_IN;
275         case RTSCTS_OUT:
276             return SerialPort.FLOWCONTROL_RTSCTS_OUT;
277         case RTSCTS_IN_OUT:
278             return SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT;
279         case XONXOFF_IN:
280             return SerialPort.FLOWCONTROL_XONXOFF_IN;
281         case XONXOFF_OUT:
282             return SerialPort.FLOWCONTROL_XONXOFF_OUT;
283         case XONXOFF_IN_OUT:
284             return SerialPort.FLOWCONTROL_XONXOFF_IN | SerialPort.FLOWCONTROL_XONXOFF_OUT;
285         }
286         throw new InvalidParameterException("broken flow control");
287     }
288 }