[java]代码库
import java.io.*;
import java.util.*;
import javax.comm.*;
public class SimpleLPTPort {
Enumeration ports;
CommPortIdentifier portId;
ParallelPort LPTPort;
protected OutputStream out;
String outPrintStr = "When an \n\rinput method \n\ris activated, ";
public SimpleLPTPort() {
checkLPTPort();
}
public void checkLPTPort() {
ports = CommPortIdentifier.getPortIdentifiers();
if (ports == null) {
System.out.println("No comm ports found!");
System.exit(0);
}
while (ports.hasMoreElements()) {
// Get the specific port
portId = (CommPortIdentifier)ports.nextElement();
// Is this a parallel port?
if (portId.getPortType() == CommPortIdentifier.PORT_PARALLEL) {
// Is the port in use?
if (portId.isCurrentlyOwned()) {
System.out.println("Detected "
+ portId.getName()
+ " in use by "
+ portId.getCurrentOwner());
}
// Try to open the port.
try {
try {
//for(int i = 0; i <=2; i++) {
LPTPort = (ParallelPort)portId.open("ParallelPort", 2000);
System.out.println(LPTPort.getName() + " a LPT port is opened.");
if (LPTPort == null) {
System.out.println("Error opening LPT port "
+ LPTPort.getName());
}
// Get the output stream
try
{
out = LPTPort.getOutputStream();
}
catch (IOException e)
{
System.out.println("Cannot open output stream");
}
out.write(outPrintStr.getBytes());
System.out.println(outPrintStr.getBytes());
LPTPort.close();
System.out.println(LPTPort.getName() + " is closing.");
//}
System.out.println("Output stream complete.");
//LPTPort.close();
System.exit(0);
} catch (IOException ex) {
System.out.println("Output stream fail.");
System.out.println(LPTPort.getName()
+ ": Cannot write to output stream");
LPTPort.close();
System.exit(0);
}
} catch (PortInUseException e) {
System.out.println("Queueing open for "
+ portId.getName()
+ ": port in use by "
+ e.currentOwner);
}
}
}
}
public static void main(String[] args) {
new SimpleLPTPort();
}
}