root / trunk / code / projects / calibration_platform / server / cal_sta_server / SimpleSerial.java @ 1946
History | View | Annotate | Download (1.38 KB)
1 |
import java.io.*; |
---|---|
2 |
import java.util.Enumeration; |
3 |
import gnu.io.*; |
4 |
|
5 |
import java.lang.Character; |
6 |
|
7 |
public class SimpleSerial { |
8 |
|
9 |
public static void main(String[] args) { |
10 |
|
11 |
String port_path = "/dev/ttyUSB0"; |
12 |
/*
|
13 |
FileInputStream port_stream;
|
14 |
|
15 |
try {
|
16 |
port_stream = new FileInputStream(port_path);
|
17 |
} catch (IOException e) {
|
18 |
System.out.print(e);
|
19 |
}
|
20 |
|
21 |
port_stream.getFD()
|
22 |
*/
|
23 |
|
24 |
|
25 |
Enumeration ports = CommPortIdentifier.getPortIdentifiers();
|
26 |
CommPortIdentifier port = null, arduino = null; |
27 |
|
28 |
SerialPort ser = null;
|
29 |
InputStream inSt = null; |
30 |
|
31 |
while (ports.hasMoreElements()) {
|
32 |
|
33 |
port = (CommPortIdentifier)ports.nextElement(); |
34 |
|
35 |
System.out.println(port.getName());
|
36 |
|
37 |
if (port.getName().equals("/dev/arduino")) { |
38 |
arduino = port; |
39 |
} |
40 |
|
41 |
} |
42 |
|
43 |
System.out.println(arduino.getName());
|
44 |
|
45 |
try{
|
46 |
ser = (SerialPort) arduino.open("cal_sta_server", 2000); |
47 |
} catch (PortInUseException e) {System.out.print(e);} |
48 |
|
49 |
try {
|
50 |
inSt = ser.getInputStream(); |
51 |
} catch (IOException e) {System.out.print(e);} |
52 |
|
53 |
try {
|
54 |
ser.setSerialPortParams(19200, SerialPort.DATABITS_8,
|
55 |
SerialPort.STOPBITS_1, |
56 |
SerialPort.PARITY_NONE); |
57 |
} catch (UnsupportedCommOperationException e) {}
|
58 |
|
59 |
while (true) { |
60 |
|
61 |
try {
|
62 |
if (inSt.available() > 0) { |
63 |
System.out.print((char)inSt.read()); |
64 |
} |
65 |
} catch (IOException e) {System.out.print(e);} |
66 |
|
67 |
} |
68 |
|
69 |
} |
70 |
|
71 |
} |