-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerialParameters.java
More file actions
371 lines (335 loc) · 8.83 KB
/
SerialParameters.java
File metadata and controls
371 lines (335 loc) · 8.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import javax.comm.*;
/**
A class that stores parameters for serial ports.
*/
public class SerialParameters {
private String m_sPortName;
private int m_iBaudRate;
private int m_iFlowControlIn;
private int m_iFlowControlOut;
private int m_iDatabits;
private int m_iStopbits;
private int m_iParity;
/**
Default constructer. Sets parameters to no port, 9600 baud, no flow
control, 8 data bits, 1 stop bit, no parity.
*/
public SerialParameters () {
this("",
9600,
SerialPort.FLOWCONTROL_NONE,
SerialPort.FLOWCONTROL_NONE,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE );
}
/**
Paramaterized constructer.
@param portName The name of the port.
@param baudRate The baud rate.
@param flowControlIn Type of flow control for receiving.
@param flowControlOut Type of flow control for sending.
@param databits The number of data bits.
@param stopbits The number of stop bits.
@param parity The type of parity.
*/
public SerialParameters(String m_sPortName,
int m_iBaudRate,
int m_iFlowControlIn,
int m_iFlowControlOut,
int m_iDatabits,
int m_iStopbits,
int m_iParity) {
this.m_sPortName = m_sPortName;
this.m_iBaudRate = m_iBaudRate;
this.m_iFlowControlIn = m_iFlowControlIn;
this.m_iFlowControlOut = m_iFlowControlOut;
this.m_iDatabits = m_iDatabits;
this.m_iStopbits = m_iStopbits;
this.m_iParity = m_iParity;
}
/**
Sets port name.
@param portName New port name.
*/
public void setPortName(String m_sPortName) {
this.m_sPortName = m_sPortName;
}
/**
Gets port name.
@return Current port name.
*/
public String getPortName() {
return m_sPortName;
}
/**
Sets baud rate.
@param baudRate New baud rate.
*/
public void setBaudRate(int m_iBaudRate) {
this.m_iBaudRate = m_iBaudRate;
}
/**
Sets baud rate.
@param baudRate New baud rate.
*/
public void setBaudRate(String m_iBaudRate) {
this.m_iBaudRate = Integer.parseInt(m_iBaudRate);
}
/**
Gets baud rate as an <code>int</code>.
@return Current baud rate.
*/
public int getBaudRate() {
return m_iBaudRate;
}
/**
Gets baud rate as a <code>String</code>.
@return Current baud rate.
*/
public String getBaudRateString() {
return Integer.toString(m_iBaudRate);
}
/**
Sets flow control for reading.
@param flowControlIn New flow control for reading type.
*/
public void setFlowControlIn(int m_iFlowControlIn) {
this.m_iFlowControlIn = m_iFlowControlIn;
}
/**
Sets flow control for reading.
@param flowControlIn New flow control for reading type.
*/
public void setFlowControlIn(String m_iFlowControlIn) {
this.m_iFlowControlIn = stringToFlow(m_iFlowControlIn);
}
/**
Gets flow control for reading as an <code>int</code>.
@return Current flow control type.
*/
public int getFlowControlIn() {
return m_iFlowControlIn;
}
/**
Gets flow control for reading as a <code>String</code>.
@return Current flow control type.
*/
public String getFlowControlInString() {
return flowToString(m_iFlowControlIn);
}
/**
Sets flow control for writing.
@param flowControlIn New flow control for writing type.
*/
public void setFlowControlOut(int m_iFlowControlOut) {
this.m_iFlowControlOut = m_iFlowControlOut;
}
/**
Sets flow control for writing.
@param flowControlIn New flow control for writing type.
*/
public void setFlowControlOut(String m_iFlowControlOut) {
this.m_iFlowControlOut = stringToFlow(m_iFlowControlOut);
}
/**
Gets flow control for writing as an <code>int</code>.
@return Current flow control type.
*/
public int getFlowControlOut() {
return m_iFlowControlOut;
}
/**
Gets flow control for writing as a <code>String</code>.
@return Current flow control type.
*/
public String getFlowControlOutString() {
return flowToString(m_iFlowControlOut);
}
/**
Sets data bits.
@param databits New data bits setting.
*/
public void setDatabits(int m_iDatabits) {
this.m_iDatabits = m_iDatabits;
}
/**
Sets data bits.
@param databits New data bits setting.
*/
public void setDatabits(String m_iDatabits) {
if (m_iDatabits.equals("5")) {
this.m_iDatabits = SerialPort.DATABITS_5;
}
if (m_iDatabits.equals("6")) {
this.m_iDatabits = SerialPort.DATABITS_6;
}
if (m_iDatabits.equals("7")) {
this.m_iDatabits = SerialPort.DATABITS_7;
}
if (m_iDatabits.equals("8")) {
this.m_iDatabits = SerialPort.DATABITS_8;
}
}
/**
Gets data bits as an <code>int</code>.
@return Current data bits setting.
*/
public int getDatabits() {
return m_iDatabits;
}
/**
Gets data bits as a <code>String</code>.
@return Current data bits setting.
*/
public String getDatabitsString() {
switch(m_iDatabits) {
case SerialPort.DATABITS_5:
return "5";
case SerialPort.DATABITS_6:
return "6";
case SerialPort.DATABITS_7:
return "7";
case SerialPort.DATABITS_8:
return "8";
default:
return "8";
}
}
/**
Sets stop bits.
@param stopbits New stop bits setting.
*/
public void setStopbits(int m_iStopbits) {
this.m_iStopbits = m_iStopbits;
}
/**
Sets stop bits.
@param stopbits New stop bits setting.
*/
public void setStopbits(String m_iStopbits) {
if (m_iStopbits.equals("1")) {
this.m_iStopbits = SerialPort.STOPBITS_1;
}
if (m_iStopbits.equals("1.5")) {
this.m_iStopbits = SerialPort.STOPBITS_1_5;
}
if (m_iStopbits.equals("2")) {
this.m_iStopbits = SerialPort.STOPBITS_2;
}
}
/**
Gets stop bits setting as an <code>int</code>.
@return Current stop bits setting.
*/
public int getStopbits() {
return m_iStopbits;
}
/**
Gets stop bits setting as a <code>String</code>.
@return Current stop bits setting.
*/
public String getStopbitsString() {
switch(m_iStopbits) {
case SerialPort.STOPBITS_1:
return "1";
case SerialPort.STOPBITS_1_5:
return "1.5";
case SerialPort.STOPBITS_2:
return "2";
default:
return "1";
}
}
/**
Sets parity setting.
@param parity New parity setting.
*/
public void setParity(int m_iParity) {
this.m_iParity = m_iParity;
}
/**
Sets parity setting.
@param parity New parity setting.
*/
public void setParity(String m_iParity) {
if (m_iParity.equals("None")) {
this.m_iParity = SerialPort.PARITY_NONE;
}
if (m_iParity.equals("Even")) {
this.m_iParity = SerialPort.PARITY_EVEN;
}
if (m_iParity.equals("Odd")) {
this.m_iParity = SerialPort.PARITY_ODD;
}
}
/**
Gets parity setting as an <code>int</code>.
@return Current parity setting.
*/
public int getParity() {
return m_iParity;
}
/**
Gets parity setting as a <code>String</code>.
@return Current parity setting.
*/
public String getParityString() {
switch(m_iParity) {
case SerialPort.PARITY_NONE:
return "None";
case SerialPort.PARITY_EVEN:
return "Even";
case SerialPort.PARITY_ODD:
return "Odd";
default:
return "None";
}
}
/**
Converts a <code>String</code> describing a flow control type to an
<code>int</code> type defined in <code>SerialPort</code>.
@param flowControl A <code>string</code> describing a flow control type.
@return An <code>int</code> describing a flow control type.
*/
private int stringToFlow(String l_sFlowControl) {
if (l_sFlowControl.equals("None")) {
return SerialPort.FLOWCONTROL_NONE;
}
if (l_sFlowControl.equals("Xon/Xoff Out")) {
return SerialPort.FLOWCONTROL_XONXOFF_OUT;
}
if (l_sFlowControl.equals("Xon/Xoff In")) {
return SerialPort.FLOWCONTROL_XONXOFF_IN;
}
if (l_sFlowControl.equals("RTS/CTS In")) {
return SerialPort.FLOWCONTROL_RTSCTS_IN;
}
if (l_sFlowControl.equals("RTS/CTS Out")) {
return SerialPort.FLOWCONTROL_RTSCTS_OUT;
}
return SerialPort.FLOWCONTROL_NONE;
}
/**
Converts an <code>int</code> describing a flow control type to a
<code>String</code> describing a flow control type.
@param flowControl An <code>int</code> describing a flow control type.
@return A <code>String</code> describing a flow control type.
*/
String flowToString(int l_iFlowControl) {
switch(l_iFlowControl) {
case SerialPort.FLOWCONTROL_NONE:
return "None";
case SerialPort.FLOWCONTROL_XONXOFF_OUT:
return "Xon/Xoff Out";
case SerialPort.FLOWCONTROL_XONXOFF_IN:
return "Xon/Xoff In";
case SerialPort.FLOWCONTROL_RTSCTS_IN:
return "RTS/CTS In";
case SerialPort.FLOWCONTROL_RTSCTS_OUT:
return "RTS/CTS Out";
default:
return "None";
}
}
}