23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

public static void main(String[] args) {<br />

}<br />

}<br />

}<br />

<strong>Java</strong> I/O<br />

Enumeration thePorts = CommPortIdentifier.getPortIdentifiers();<br />

while (thePorts.hasMoreElements()) {<br />

CommPortIdentifier com = (CommPortIdentifier) thePorts.nextElement();<br />

System.out.print(com.getName());<br />

}<br />

switch(com.getPortType()) {<br />

case CommPortIdentifier.PORT_SERIAL:<br />

System.out.println(", a serial port: ");<br />

break;<br />

case CommPortIdentifier.PORT_PARALLEL:<br />

System.out.println(", a parallel port: ");<br />

break;<br />

default:<br />

// important since other types of ports like USB<br />

// and firewire are expected to be added in the future<br />

System.out.println(" , a port of unknown type: ");<br />

break;<br />

}<br />

try {<br />

CommPort thePort = com.open("Port Tester", 20);<br />

testProperties(thePort);<br />

thePort.close();<br />

}<br />

catch (PortInUseException e) {<br />

System.out.println("Port in use, can't test properties");<br />

}<br />

System.out.println();<br />

public static void testProperties(CommPort thePort) {<br />

try {<br />

thePort.enableReceiveThreshold(10);<br />

System.out.println("Receive threshold supported");<br />

}<br />

catch (UnsupportedCommOperationException e) {<br />

System.out.println("Receive threshold not supported");<br />

}<br />

try {<br />

thePort.enableReceiveTimeout(10);<br />

System.out.println("Receive timeout not supported");<br />

}<br />

catch (UnsupportedCommOperationException e) {<br />

System.out.println("Receive framing not supported");<br />

}<br />

try {<br />

thePort.enableReceiveFraming(10);<br />

System.out.println("Receive framing supported");<br />

}<br />

catch (UnsupportedCommOperationException e) {<br />

System.out.println("Receive framing not supported");<br />

}<br />

442

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!