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   */
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, XONXOFF_IN, XONXOFF_OUT
51      }
52  
53      private final String name;
54      private final int bauds;
55      private final DataBits dataBits;
56      private final StopBits stopBits;
57      private final Parity parity;
58      private final FlowControl flowControl;
59  
60      /**
61       * Create an address for a serial communication, associating a serial interface and
62       * various serial signal carcteristics.
63       * @param name name of the device, COM1 COM2 for Windows, /dev/ttyS0 for Unix
64       * @param bauds baud rate for the communication
65       * @param dataBits number of data bits per bytes
66       * @param stopBits number of stop bits
67       * @param parity parity used
68       * @param flowControl flow control used
69       */
70      public SerialAddress(String name, int bauds, DataBits dataBits,
71              StopBits stopBits, Parity parity, FlowControl flowControl) {
72          if (name == null) {
73              throw new NullPointerException("name");
74          }
75          name = name.trim();
76          if (name.length() == 0) {
77              throw new IllegalArgumentException("Empty name.");
78          }
79          if (bauds <= 0) {
80              throw new IllegalArgumentException("bauds: " + bauds);
81          }
82          if (dataBits == null) {
83              throw new NullPointerException("dataBits");
84          }
85          if (stopBits == null) {
86              throw new NullPointerException("stopBits");
87          }
88          if (parity == null) {
89              throw new NullPointerException("parity");
90          }
91          if (flowControl == null) {
92              throw new NullPointerException("flowControl");
93          }
94          
95          this.name = name;
96          this.bauds = bauds;
97          this.dataBits = dataBits;
98          this.stopBits = stopBits;
99          this.parity = parity;
100         this.flowControl = flowControl;
101     }
102 
103     /**
104      * Bauds rate for the communication.
105      * @return the bauds (bits per seconds) for this serial link
106      */
107     public int getBauds() {
108         return bauds;
109     }
110 
111     /**
112      * Number of data bits for each communicated bytes.
113      * @return the data bits
114      */
115     public DataBits getDataBits() {
116         return dataBits;
117     }
118 
119     /**
120      * The flow control policie used for this communication.
121      * @return the flow control
122      */
123     public FlowControl getFlowControl() {
124         return flowControl;
125     }
126 
127     /**
128      * The name of the device. Can be COM1, COM2, /dev/ttyS0, /dev/ttyUSB1, etc..
129      * @return name
130      */
131     public String getName() {
132         return name;
133     }
134 
135     /**
136      * The parity check for this communication.
137      * @return parity type
138      */
139     public Parity getParity() {
140         return parity;
141     }
142 
143     /**
144      * Number of stop bits used.
145      * @return stop bits number
146      */
147     public StopBits getStopBits() {
148         return stopBits;
149     }
150 
151     /**
152      * Convert this serial address to a human readable string.
153      */
154     @Override
155     public String toString() {
156         return name + " (bauds: " + bauds + ", dataBits: " + dataBits
157                 + ", stopBits: " + stopBits + ", parity: " + parity
158                 + ", flowControl: " + flowControl + ")";
159     }
160 
161     int getDataBitsForRXTX() {
162         switch (dataBits) {
163         case DATABITS_5:
164             return SerialPort.DATABITS_5;
165         case DATABITS_6:
166             return SerialPort.DATABITS_6;
167         case DATABITS_7:
168             return SerialPort.DATABITS_7;
169         case DATABITS_8:
170             return SerialPort.DATABITS_8;
171         }
172         throw new InvalidParameterException("broken databits");
173     }
174 
175     int getStopBitsForRXTX() {
176         switch (stopBits) {
177         case BITS_1:
178             return SerialPort.STOPBITS_1;
179         case BITS_1_5:
180             return SerialPort.STOPBITS_1_5;
181         case BITS_2:
182             return SerialPort.STOPBITS_2;
183         }
184         throw new InvalidParameterException("broken stopbits");
185     }
186 
187     int getParityForRXTX() {
188         switch (parity) {
189         case EVEN:
190             return SerialPort.PARITY_EVEN;
191         case MARK:
192             return SerialPort.PARITY_MARK;
193         case NONE:
194             return SerialPort.PARITY_NONE;
195         case ODD:
196             return SerialPort.PARITY_ODD;
197         case SPACE:
198             return SerialPort.PARITY_SPACE;
199         }
200         throw new InvalidParameterException("broken parity");
201     }
202 
203     int getFLowControlForRXTX() {
204         switch (flowControl) {
205         case NONE:
206             return SerialPort.FLOWCONTROL_NONE;
207         case RTSCTS_IN:
208             return SerialPort.FLOWCONTROL_RTSCTS_IN;
209         case RTSCTS_OUT:
210             return SerialPort.FLOWCONTROL_RTSCTS_OUT;
211         case XONXOFF_IN:
212             return SerialPort.FLOWCONTROL_XONXOFF_IN;
213         case XONXOFF_OUT:
214             return SerialPort.FLOWCONTROL_XONXOFF_OUT;
215         }
216         throw new InvalidParameterException("broken stopbits");
217     }
218 }