-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcom_port.java
executable file
·46 lines (43 loc) · 1.38 KB
/
com_port.java
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
import javax.comm.*;
//import com.sun.comm.Win32Driver;
import java.util.*;
public class com_port {
public static void main(String args[]) throws Exception {
// List portNames
Enumeration ports = CommPortIdentifier.getPortIdentifiers();
while (ports.hasMoreElements()) {
CommPortIdentifier portId = (CommPortIdentifier)ports.nextElement();
String s = getPortInfo(portId);
System.out.println(s);
}
// test open-close methods on each port
ports = CommPortIdentifier.getPortIdentifiers();
while (ports.hasMoreElements()) {
CommPortIdentifier portId = (CommPortIdentifier)ports.nextElement();
CommPort port = null;
try {
System.out.print("open " + portId.getName());
port = portId.open(portId.getName(), 2000);
port.close();
System.out.println("...closed");
} catch (Exception ex) {
ex.printStackTrace();
if (port != null)
try { port.close(); } catch (Exception e) { }
}
}
}
private static String getPortInfo(CommPortIdentifier portid) {
StringBuffer sb = new StringBuffer();
sb.append(portid.getName());
sb.append(", ");
sb.append("portType: ");
if (portid.getPortType() == CommPortIdentifier.PORT_SERIAL)
sb.append("serial");
else if (portid.getPortType() == CommPortIdentifier.PORT_PARALLEL)
sb.append("parallel");
else
sb.append("unknown");
return sb.toString();
}
}