PDA

View Full Version : Điều khiển LPT - JAVA


rocket80
26-08-2011, 11:27 PM
Hiện em đã hàn 8 cái đèn led theo sơ đồ dưới đây
http://i200.photobucket.com/albums/aa181/onlylove776610/article1_003.jpg
Em muốn bật 8 cái sáng lên hoặc tắt đi bằng JAVA sử dụng rxtx
Đây là đoạn code em test thử

import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.ParallelPort;
import gnu.io.PortInUseException;

import java.io.*;


public class ParallelCommunication {

private static OutputStream outputStream;;
private static ParallelPort parallelPort;
private static CommPortIdentifier port;
public static final String PARALLEL_PORT = "LPT1";
public static final String[] PORT_TYPE = { "Serial Port", "Parallel Port" };
// these commands are specific for my printer around the text

public static void main(String[] args) {

System.out.println("Started test....");

try {
// get the parallel port connected to the printer
port = CommPortIdentifier.getPortIdentifier(PARALLEL_PORT );
System.out.println("\nport.portType = " + port.getPortType());
System.out.println("port type = "
+ PORT_TYPE[port.getPortType() - 1]);
System.out.println("port.name = " + port.getName());

// open the parallel port -- open(App name, timeout)
parallelPort = (ParallelPort) port.open("CommTest", 20);
outputStream = parallelPort.getOutputStream();
System.out.println("Write...");

outputStream.write(127);

System.out.println("Flush...");
outputStream.flush();
System.out.println("Close...");
outputStream.close();

} catch (NoSuchPortException nspe) {
System.out.println("\nPrinter Port LPT1 not found : "
+ "NoSuchPortException.\nException:\n" + nspe + "\n");
} catch (PortInUseException piue) {
System.out.println("\nPrinter Port LPT1 is in use : "
+ "PortInUseException.\nException:\n" + piue + "\n");
}

catch (IOException ioe) {
System.out.println("\nPrinter Port LPT1 failed to write : "
+ "IOException.\nException:\n" + ioe + "\n");
} catch (Exception e) {
System.out
.println("\nFailed to open Printer Port LPT1 with exeception : "
+ e + "\n");
} finally {
if (port != null && port.isCurrentlyOwned()) {
parallelPort.close();
}

System.out.println("Closed all resources.\n");
}
}
}

Em xin hỏi là ở chỗ outputStream.write(); phải gởi ra kí tự nào thì mới bật và tắt dc đèn led