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
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
63
64
65
66
67
68
69
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
106
107
108 public int getBauds() {
109 return bauds;
110 }
111
112
113
114
115
116 public DataBits getDataBits() {
117 return dataBits;
118 }
119
120
121
122
123
124 public FlowControl getFlowControl() {
125 return flowControl;
126 }
127
128
129
130
131
132 public String getName() {
133 return name;
134 }
135
136
137
138
139
140 public Parity getParity() {
141 return parity;
142 }
143
144
145
146
147
148 public StopBits getStopBits() {
149 return stopBits;
150 }
151
152
153
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 }