PIC Vietnam

Go Back   PIC Vietnam > Truyền thông > Giao tiếp cổng COM và LPT

Tài trợ cho PIC Vietnam
Trang chủ Đăng Kí Hỏi/Ðáp Thành Viên Lịch Bài Trong Ngày Vi điều khiển

Giao tiếp cổng COM và LPT RS232, RS485 và LPT là những giao tiếp cơ bản và kinh điển khi mới học về vi điều khiển...

 
 
Ðiều Chỉnh Xếp Bài
Prev Previous Post   Next Post Next
Old 26-08-2011, 11:27 PM   #1
rocket80
Nhập môn đệ tử
 
Tham gia ngày: Jun 2011
Bài gửi: 1
:
Điều khiển LPT - JAVA

Hiện em đã hàn 8 cái đèn led theo sơ đồ dưới đây
http://i200.photobucket.com/albums/a...ticle1_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
rocket80 vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
 


Quyền Sử Dụng Ở Diễn Ðàn
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is Mở
Smilies đang Mở
[IMG] đang Mở
HTML đang Tắt

Chuyển đến


Múi giờ GMT. Hiện tại là 11:26 AM.


Được sáng lập bởi Đoàn Hiệp
Powered by vBulletin®
Page copy protected against web site content infringement by Copyscape
Copyright © PIC Vietnam