Source code: jflight/tools/SerialParameters.java
1 /* @(#)SerialParameters.java 1.5 98/07/17 SMI
2 *
3 * Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved.
4 *
5 * Sun grants you ("Licensee") a non-exclusive, royalty free, license
6 * to use, modify and redistribute this software in source and binary
7 * code form, provided that i) this copyright notice and license appear
8 * on all copies of the software; and ii) Licensee does not utilize the
9 * software in a manner which is disparaging to Sun.
10 *
11 * This software is provided "AS IS," without a warranty of any kind.
12 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
13 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
14 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
15 * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
16 * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THE
17 * SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS
18 * BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
19 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES,
20 * HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING
21 * OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
23 *
24 * This software is not designed or intended for use in on-line control
25 * of aircraft, air traffic, aircraft navigation or aircraft
26 * communications; or in the design, construction, operation or
27 * maintenance of any nuclear facility. Licensee represents and
28 * warrants that it will not use or redistribute the Software for such
29 * purposes.
30 */
31
32 package jflight.tools;
33
34 import javax.comm.*;
35
36 /**
37 A class that stores parameters for serial ports.
38 */
39 public class SerialParameters {
40
41 private String portName;
42 private int baudRate;
43 private int flowControlIn;
44 private int flowControlOut;
45 private int databits;
46 private int stopbits;
47 private int parity;
48
49 /**
50 Default constructer. Sets parameters to no port, 9600 baud, no flow
51 control, 8 data bits, 1 stop bit, no parity.
52 */
53 public SerialParameters () {
54 this("COM1",
55 9600,
56 SerialPort.FLOWCONTROL_NONE,
57 SerialPort.FLOWCONTROL_NONE,
58 SerialPort.DATABITS_8,
59 SerialPort.STOPBITS_1,
60 SerialPort.PARITY_NONE );
61
62 }
63
64 /**
65 Paramaterized constructer.
66
67 @param portName The name of the port.
68 @param baudRate The baud rate.
69 @param flowControlIn Type of flow control for receiving.
70 @param flowControlOut Type of flow control for sending.
71 @param databits The number of data bits.
72 @param stopbits The number of stop bits.
73 @param parity The type of parity.
74 */
75 public SerialParameters(String portName,
76 int baudRate,
77 int flowControlIn,
78 int flowControlOut,
79 int databits,
80 int stopbits,
81 int parity) {
82
83 this.portName = portName;
84 this.baudRate = baudRate;
85 this.flowControlIn = flowControlIn;
86 this.flowControlOut = flowControlOut;
87 this.databits = databits;
88 this.stopbits = stopbits;
89 this.parity = parity;
90 }
91
92 /**
93 Sets port name.
94 @param portName New port name.
95 */
96 public void setPortName(String portName) {
97 this.portName = portName;
98 }
99
100 /**
101 Gets port name.
102 @return Current port name.
103 */
104 public String getPortName() {
105 return portName;
106 }
107
108 /**
109 Sets baud rate.
110 @param baudRate New baud rate.
111 */
112 public void setBaudRate(int baudRate) {
113 this.baudRate = baudRate;
114 }
115
116 /**
117 Sets baud rate.
118 @param baudRate New baud rate.
119 */
120 public void setBaudRate(String baudRate) {
121 this.baudRate = Integer.parseInt(baudRate);
122 }
123
124 /**
125 Gets baud rate as an <code>int</code>.
126 @return Current baud rate.
127 */
128 public int getBaudRate() {
129 return baudRate;
130 }
131
132 /**
133 Gets baud rate as a <code>String</code>.
134 @return Current baud rate.
135 */
136 public String getBaudRateString() {
137 return Integer.toString(baudRate);
138 }
139
140 /**
141 Sets flow control for reading.
142 @param flowControlIn New flow control for reading type.
143 */
144 public void setFlowControlIn(int flowControlIn) {
145 this.flowControlIn = flowControlIn;
146 }
147
148 /**
149 Sets flow control for reading.
150 @param flowControlIn New flow control for reading type.
151 */
152 public void setFlowControlIn(String flowControlIn) {
153 this.flowControlIn = stringToFlow(flowControlIn);
154 }
155
156 /**
157 Gets flow control for reading as an <code>int</code>.
158 @return Current flow control type.
159 */
160 public int getFlowControlIn() {
161 return flowControlIn;
162 }
163
164 /**
165 Gets flow control for reading as a <code>String</code>.
166 @return Current flow control type.
167 */
168 public String getFlowControlInString() {
169 return flowToString(flowControlIn);
170 }
171
172 /**
173 Sets flow control for writing.
174 @param flowControlIn New flow control for writing type.
175 */
176 public void setFlowControlOut(int flowControlOut) {
177 this.flowControlOut = flowControlOut;
178 }
179
180 /**
181 Sets flow control for writing.
182 @param flowControlIn New flow control for writing type.
183 */
184 public void setFlowControlOut(String flowControlOut) {
185 this.flowControlOut = stringToFlow(flowControlOut);
186 }
187
188 /**
189 Gets flow control for writing as an <code>int</code>.
190 @return Current flow control type.
191 */
192 public int getFlowControlOut() {
193 return flowControlOut;
194 }
195
196 /**
197 Gets flow control for writing as a <code>String</code>.
198 @return Current flow control type.
199 */
200 public String getFlowControlOutString() {
201 return flowToString(flowControlOut);
202 }
203
204 /**
205 Sets data bits.
206 @param databits New data bits setting.
207 */
208 public void setDatabits(int databits) {
209 this.databits = databits;
210 }
211
212 /**
213 Sets data bits.
214 @param databits New data bits setting.
215 */
216 public void setDatabits(String databits) {
217 if (databits.equals("5")) {
218 this.databits = SerialPort.DATABITS_5;
219 }
220 if (databits.equals("6")) {
221 this.databits = SerialPort.DATABITS_6;
222 }
223 if (databits.equals("7")) {
224 this.databits = SerialPort.DATABITS_7;
225 }
226 if (databits.equals("8")) {
227 this.databits = SerialPort.DATABITS_8;
228 }
229 }
230
231 /**
232 Gets data bits as an <code>int</code>.
233 @return Current data bits setting.
234 */
235 public int getDatabits() {
236 return databits;
237 }
238
239 /**
240 Gets data bits as a <code>String</code>.
241 @return Current data bits setting.
242 */
243 public String getDatabitsString() {
244 switch(databits) {
245 case SerialPort.DATABITS_5:
246 return "5";
247 case SerialPort.DATABITS_6:
248 return "6";
249 case SerialPort.DATABITS_7:
250 return "7";
251 case SerialPort.DATABITS_8:
252 return "8";
253 default:
254 return "8";
255 }
256 }
257
258 /**
259 Sets stop bits.
260 @param stopbits New stop bits setting.
261 */
262 public void setStopbits(int stopbits) {
263 this.stopbits = stopbits;
264 }
265
266 /**
267 Sets stop bits.
268 @param stopbits New stop bits setting.
269 */
270 public void setStopbits(String stopbits) {
271 if (stopbits.equals("1")) {
272 this.stopbits = SerialPort.STOPBITS_1;
273 }
274 if (stopbits.equals("1.5")) {
275 this.stopbits = SerialPort.STOPBITS_1_5;
276 }
277 if (stopbits.equals("2")) {
278 this.stopbits = SerialPort.STOPBITS_2;
279 }
280 }
281
282 /**
283 Gets stop bits setting as an <code>int</code>.
284 @return Current stop bits setting.
285 */
286 public int getStopbits() {
287 return stopbits;
288 }
289
290 /**
291 Gets stop bits setting as a <code>String</code>.
292 @return Current stop bits setting.
293 */
294 public String getStopbitsString() {
295 switch(stopbits) {
296 case SerialPort.STOPBITS_1:
297 return "1";
298 case SerialPort.STOPBITS_1_5:
299 return "1.5";
300 case SerialPort.STOPBITS_2:
301 return "2";
302 default:
303 return "1";
304 }
305 }
306
307 /**
308 Sets parity setting.
309 @param parity New parity setting.
310 */
311 public void setParity(int parity) {
312 this.parity = parity;
313 }
314
315 /**
316 Sets parity setting.
317 @param parity New parity setting.
318 */
319 public void setParity(String parity) {
320 if (parity.equals("None")) {
321 this.parity = SerialPort.PARITY_NONE;
322 }
323 if (parity.equals("Even")) {
324 this.parity = SerialPort.PARITY_EVEN;
325 }
326 if (parity.equals("Odd")) {
327 this.parity = SerialPort.PARITY_ODD;
328 }
329 }
330
331 /**
332 Gets parity setting as an <code>int</code>.
333 @return Current parity setting.
334 */
335 public int getParity() {
336 return parity;
337 }
338
339 /**
340 Gets parity setting as a <code>String</code>.
341 @return Current parity setting.
342 */
343 public String getParityString() {
344 switch(parity) {
345 case SerialPort.PARITY_NONE:
346 return "None";
347 case SerialPort.PARITY_EVEN:
348 return "Even";
349 case SerialPort.PARITY_ODD:
350 return "Odd";
351 default:
352 return "None";
353 }
354 }
355
356 /**
357 Converts a <code>String</code> describing a flow control type to an
358 <code>int</code> type defined in <code>SerialPort</code>.
359 @param flowControl A <code>string</code> describing a flow control type.
360 @return An <code>int</code> describing a flow control type.
361 */
362 private int stringToFlow(String flowControl) {
363 if (flowControl.equals("None")) {
364 return SerialPort.FLOWCONTROL_NONE;
365 }
366 if (flowControl.equals("Xon/Xoff Out")) {
367 return SerialPort.FLOWCONTROL_XONXOFF_OUT;
368 }
369 if (flowControl.equals("Xon/Xoff In")) {
370 return SerialPort.FLOWCONTROL_XONXOFF_IN;
371 }
372 if (flowControl.equals("RTS/CTS In")) {
373 return SerialPort.FLOWCONTROL_RTSCTS_IN;
374 }
375 if (flowControl.equals("RTS/CTS Out")) {
376 return SerialPort.FLOWCONTROL_RTSCTS_OUT;
377 }
378 return SerialPort.FLOWCONTROL_NONE;
379 }
380
381 /**
382 Converts an <code>int</code> describing a flow control type to a
383 <code>String</code> describing a flow control type.
384 @param flowControl An <code>int</code> describing a flow control type.
385 @return A <code>String</code> describing a flow control type.
386 */
387 String flowToString(int flowControl) {
388 switch(flowControl) {
389 case SerialPort.FLOWCONTROL_NONE:
390 return "None";
391 case SerialPort.FLOWCONTROL_XONXOFF_OUT:
392 return "Xon/Xoff Out";
393 case SerialPort.FLOWCONTROL_XONXOFF_IN:
394 return "Xon/Xoff In";
395 case SerialPort.FLOWCONTROL_RTSCTS_IN:
396 return "RTS/CTS In";
397 case SerialPort.FLOWCONTROL_RTSCTS_OUT:
398 return "RTS/CTS Out";
399 default:
400 return "None";
401 }
402 }
403 }