1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
30
31
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
62
63
64
65
66
67
68
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
105
106
107 public int getBauds() {
108 return bauds;
109 }
110
111
112
113
114
115 public DataBits getDataBits() {
116 return dataBits;
117 }
118
119
120
121
122
123 public FlowControl getFlowControl() {
124 return flowControl;
125 }
126
127
128
129
130
131 public String getName() {
132 return name;
133 }
134
135
136
137
138
139 public Parity getParity() {
140 return parity;
141 }
142
143
144
145
146
147 public StopBits getStopBits() {
148 return stopBits;
149 }
150
151
152
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 }