root / trunk / code / projects / calibration_platform / server / cal_sta_server / SimpleRead.java @ 1946
History | View | Annotate | Download (2.45 KB)
1 |
import gnu.io.*; |
---|---|
2 |
|
3 |
public class SimpleRead { |
4 |
|
5 |
static CommPortIdentifier portId;
|
6 |
static Enumeration portList; |
7 |
InputStream inputStream;
|
8 |
SerialPort serialPort; |
9 |
Thread readThread;
|
10 |
|
11 |
public static void main(String[] args) { |
12 |
boolean portFound = false; |
13 |
String defaultPort = "/dev/ttyUSB0"; |
14 |
|
15 |
if (args.length > 0) { |
16 |
defaultPort = args[0];
|
17 |
} |
18 |
|
19 |
portList = CommPortIdentifier.getPortIdentifiers(); |
20 |
|
21 |
while (portList.hasMoreElements()) {
|
22 |
portId = (CommPortIdentifier) portList.nextElement(); |
23 |
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
|
24 |
if (portId.getName().equals(defaultPort)) {
|
25 |
System.out.println("Found port: "+defaultPort); |
26 |
portFound = true;
|
27 |
SimpleRead reader = new SimpleRead();
|
28 |
} |
29 |
} |
30 |
} |
31 |
if (!portFound) {
|
32 |
System.out.println("port " + defaultPort + " not found."); |
33 |
} |
34 |
|
35 |
Opens |
36 |
} |
37 |
|
38 |
/**
|
39 |
* Constructor declaration
|
40 |
*
|
41 |
*
|
42 |
* @see
|
43 |
*/
|
44 |
public SimpleRead() {
|
45 |
try {
|
46 |
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000); |
47 |
} catch (PortInUseException e) {}
|
48 |
|
49 |
try {
|
50 |
inputStream = serialPort.getInputStream(); |
51 |
} catch (IOException e) {} |
52 |
|
53 |
try {
|
54 |
serialPort.addEventListener(this);
|
55 |
} catch (TooManyListenersException e) {} |
56 |
|
57 |
serialPort.notifyOnDataAvailable(true);
|
58 |
|
59 |
try {
|
60 |
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
|
61 |
SerialPort.STOPBITS_1, |
62 |
SerialPort.PARITY_NONE); |
63 |
} catch (UnsupportedCommOperationException e) {}
|
64 |
|
65 |
readThread = new Thread(this); |
66 |
|
67 |
readThread.start(); |
68 |
} |
69 |
|
70 |
/**
|
71 |
* Method declaration
|
72 |
*
|
73 |
*
|
74 |
* @see
|
75 |
*/
|
76 |
public void run() { |
77 |
try {
|
78 |
Thread.sleep(20000); |
79 |
} catch (InterruptedException e) {} |
80 |
} |
81 |
|
82 |
/**
|
83 |
* Method declaration
|
84 |
*
|
85 |
*
|
86 |
* @param event
|
87 |
*
|
88 |
* @see
|
89 |
*/
|
90 |
public void serialEvent(SerialPortEvent event) { |
91 |
switch (event.getEventType()) {
|
92 |
|
93 |
case SerialPortEvent.BI:
|
94 |
|
95 |
case SerialPortEvent.OE:
|
96 |
|
97 |
case SerialPortEvent.FE:
|
98 |
|
99 |
case SerialPortEvent.PE:
|
100 |
|
101 |
case SerialPortEvent.CD:
|
102 |
|
103 |
case SerialPortEvent.CTS:
|
104 |
|
105 |
case SerialPortEvent.DSR:
|
106 |
|
107 |
case SerialPortEvent.RI:
|
108 |
|
109 |
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
|
110 |
break;
|
111 |
|
112 |
case SerialPortEvent.DATA_AVAILABLE:
|
113 |
byte[] readBuffer = new byte[20]; |
114 |
|
115 |
try {
|
116 |
while (inputStream.available() > 0) { |
117 |
int numBytes = inputStream.read(readBuffer);
|
118 |
} |
119 |
|
120 |
System.out.print(new String(readBuffer)); |
121 |
} catch (IOException e) {} |
122 |
|
123 |
break;
|
124 |
} |
125 |
} |
126 |
|
127 |
} |